本文介绍了为什么如果我吃第二颗星,第一颗星已经消失了?我该怎么办才能吃随机星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include<windows.h>
#include <conio.h>

using namespace std;

int main()
{
    int posx = 1;
    int posy = 0;
    char wall = '#';
    char eat1 = '*';
    char eat2 = '*';
    char eat3 = '*';
    char space = ' ';
    char goal = 'G';
    char gate = '|';
    int star=3;
    int p1=1, p2=1, p3=1;
        while (true){
    char map [10] [10] {    //0     1    2     3     4     5   6     7   8      9
                            {wall, ' ', wall, wall,wall, wall,wall, wall,wall,wall}, /*0*/
                            {wall,' ', wall,' ', ' ',' ',' ', ' ', ' ',wall},        /*1*/
                            {wall,' ', wall,' ',wall,wall,wall,wall, ' ', wall},    /*2*/
                             {wall,' ',wall,' ',wall,' ',' ',' ',' ',wall},         /*3*/
                             {wall,' ',' ',' ',wall,wall,' ', wall,eat1 ,wall},     /*4*/
                             {wall,wall,wall,wall,wall,' ',' ',wall,wall,wall},     /*5*/
                             {wall,eat3,wall,' ',' ',' ',' ', ' ',eat2,wall },      /*6*/
                             {wall,' ',wall,' ',wall,' ',wall,wall,wall,wall},      /*7*/
                             {wall,' ',' ',' ',wall,' ',gate,' ',goal,wall},          /*8*/
                             {wall,wall,wall,wall,wall,wall,wall,wall,wall,wall}    /*9*/

                     };

                char player = 'P';

                cout<<"Star Remain : "<<star<<endl;

                     map[posy][posx] = player;


                     for (int i=0; i<=9;i++)
                     {
                         for (int j=0; j<=9;j++)
                         {
                             cout<<map[i][j];

                         }
                     cout<<endl;}



    //action
    if(posx != 8 || posy != 8)
    {char control = getch();
    switch (control)
    {case 's':
    if (map [posy+1] [posx] != wall)
    {posy++;}
   break;

   case 'w':
    if (map [posy-1] [posx] != wall && posy >0)
    {posy--;}
   break;

   case 'a':
   if (map [posy] [posx-1] != wall)
   {posx--;}
   break;

   case 'd':
    if (map [posy] [posx+1] != wall && map [posy] [posx+1] != '|')
    {
        posx++;
    }
    break;
    }

    if (map [posy] [posx] == map [4][8]  && eat1 != space)
    {

        star = star-1;
        eat1 = space;
    }
    else if (map [posy] [posx] == map [6] [8]  && eat2 !=space)

    {
        star = star-1;
        eat2 = space;
    }
    else if (map [posy] [posx] == map [6] [1] && eat3 !=space)

    {
        star = star-1;
        eat3 = space;
    }

    if (star == 0)

    {
        gate = space;
    }
    }
    else cout<<"you win";



        system ("cls");}




    return 0;
}

推荐答案

if (map[posy][posx] == '*')
{
    star--; // similar to star = star - 1
    map[posy][posx] = space;
}


if (posy==4 && posx == 8 && eat1 != space)



另一个问题你的代码,如果你需要20颗星,你还需要20 如果,并且由于星号位置是硬编码的,你无法处理随机位置。



诀窍是直接使用 map 并直接清除地图上吃过的星星。

解决方案1中的代码处理任意数量的星星和任何位置(随机)。


Another problem with your code, if you need 20 stars, you also need 20 if, and since the stars positions are hard coded, you can't handle random positions.

The trick is to use directly map and clear eaten stars on the map directly.
The code in solution 1 handle any number of stars and any position (random).


这篇关于为什么如果我吃第二颗星,第一颗星已经消失了?我该怎么办才能吃随机星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 17:42