为什么如果我吃第二颗星,第一颗星已经消失了?我该怎么办才能吃随机星? [英] Why If I Ate The Second Star, The 1St Star Gone 1St? Wht Should I Do So That I Can Eat Random Stars?

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

问题描述

#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 I undestand you right, you want to eat the stars. Then simply replace a star by a space inside the map when at a star:
if (map[posy][posx] == '*')
{
    star--; // similar to star = star - 1
    map[posy][posx] = space;
}


此代码 map [posy] [posx] == map [4] [8] 检查您的头寸的包含是否与 map [4] [8] 的包含相同,此代码 eat1!=空间第一颗星没有被吃掉。

问题是你的实际位置不是必需的4,8

这个代码对第一颗星是正确的。

This code map [posy] [posx] == map [4][8] check that the contain of your position is the same as the contain of map [4][8] and this code eat1 != space that first star is not eaten.
The problem is that your actual position is not necessary 4,8
This code correct for first star.
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).


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆