游戏中的C ++ 2d数组重复错误

时间:2016-04-20 18:48:22

标签: c++ arrays multidimensional-array

我是C ++的新手,我想弄清楚为什么当我四处走动时,我的游戏板上会出现两个“*”符号。游戏应该是关于避免巨魔(@)。但我得到重复的@和*符号,我无法弄清楚为什么。似乎问题出现在for循环之一或posXposY变量中,我通过注释掉代码段找到了这些变量:

#include <iostream>
#include <string>

using namespace std;

void ClearScreen()
{
    cout << string(100, '\n');
}

main()
{
    int size_arrx = 10;
    int size_arry = 20;
    int posX = 0;
    int posY = 0;
    int trollX = size_arrx - 1;
    int trollY = size_arry - 1;
    char a[size_arry][size_arrx];
    bool Alive = true;
    char player = '*';
    char troll = '@';
    while (Alive == true) {
        ClearScreen();
        for (int i = 0; i<size_arrx; i++)
        {
            for (int j = 0; j<size_arry; j++)
            {
                a[i][j] = 'x';
            }
        }
        for (int i = 0; i<size_arrx; i++)
        {
            for (int j = 0; j<size_arry; j++)
            {
                a[posX][posY] = player;
                a[trollX][trollY] = troll;

                cout << a[i][j];

                if (posX< 0) {
                    a[posX = 0][posY] = player;
                    cout << a[i][j];
                }
                else if (posY< 0) {
                    a[posX][posY = 0] = player;
                    cout << a[i][j];
                }
                else if (posY > size_arry - 1) {
                    a[posX][posY = size_arry - 1] = player;
                    cout << a[i][j];
                }
                else if (posX > size_arrx - 1) {
                    a[posX = size_arrx - 1][posY] = player;
                    cout << a[i][j];
                }
            }
            cout << endl;
        }
        char dir;
        cin >> dir;
        if (dir == 'w') {
            trollX++;
            posX--;
        }

        if (dir == 's') {
            trollX--;
            posX++;
        }

        if (dir == 'd') {
            trollY--;
            posY++;
        }

        if (dir == 'a') {
            trollY++;
            posY--;
        }
    }

    if ((trollX == posX) && (trollY == posY)) {
        Alive == false;
    }
}

结果如下所示。我只想要一个*。 *可以完美地移动,但是复制*跟在原始*之后,但是11 X的距离。

xxxxxxxxxx*xxxxxxxxx  <---- This is a duplicate *
*xxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxx@
xxxxxxxxx@xxxxxxxxxx <---- This is a duplicate @

如果你能帮助我,请提前致谢

for (int i=0;i<size_arrx;i++){
    for (int j=0;j<size_arry;j++){
        a[i][j]='x';
}
    }
a[posX][posY]=player;
a[trollX][trollY]=troll;
for (int i=0;i<size_arrx;i++){
    for (int j=0;j<size_arry;j++){
        cout << a[i][j];

使用此代码会产生同样的错误。我正在解释这个,因为a[i][j]='x'用X来填充a[][]的所有位置。 a[posX][posY]=player;用*(例如,可能是x 2 y 5)覆盖播放器的位置,然后通过cout << a[i][j];打印电路板。我不明白如何在那里抛出重复的符号。

1 个答案:

答案 0 :(得分:1)

让我们简化您的计划。

在while循环之外初始化板。
没有理由继续初始化它:

for (unsigned int row = 0; row < size_arry; ++row)
{
  std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row.
}

打印电路板应该很简单:

for (unsigned int row = 0; row < size_arry; ++row)
{
  for (unsigned int column = 0; column < size_arrx; ++column)
  {
    cout << a[row][column];
  }
  cout << '\n';
}

现在是人物逻辑 每个角色都有一个位置,行和列,它的位置。为了便于恢复,每个角色也应该有一个先前的位置。

struct Position
{
  unsigned int row;
  unsigned int column;
};

对于该代码感到抱歉,手指和键盘没有合作。

要将字符移动到有效的新位置,您必须恢复以前的位置:

unsigned int player_now_x;
unsigned int player_now_y;
unsigned int player_prev_x;
unsigned int player_prev_y;
//...
a[player_prev_y][player_prev_x] = 'x';
a[player_now_y][player_now_y] = player;

为了处理单个字母命令,switch语句可能更具可读性:

// Update previous position.
player_prev_x = player_now_x;
player_prev_y = player_now_y;
switch (dir)
{
  case 'd':
    if (player_now_y < size_arry)
    {
      ++player_now_y;
    }
    break;
  case 's':
    if (player_now_x < size_arrx)
    {
      ++player_now_x;
    }
    break;
// ...
  }

简化。 如果添加额外的列,则可以使用一个cout打印该板。每行的结尾列(除了最后一行)将有一个行结束字符'\ n'。最后一行的最后一列将有一个终止字符'\ 0'。

struct Board
{
  void set_player(const Position& pos, char player_token)
  {
    a[pos.x][pos.y] = player_token;
  }

  void move_player(const Position& new_position,
                   const Position& previous_position,
                   char            player_token)
  {
     set_player(previous_position, 'x');
     set_player(new_position, player_token);
  }

  void print()
  {
    std::cout << &a[0][0] << "\n";
  }

  Board()
  {
    for (unsigned int y = 0; y < size_arry; ++y)
    {
      std::fill(&a[y][0], &a[y][size_arrx], 'x');
      a[y][size_arrx - 1] = '\n';
    }
    a[size_arry - 1][size_arrx - 1] = '\0';
  }
};

//...
Board b;
Position player_now;
Position player_prev;
const char player_token = '*';

//...
switch (dir)
{
  case 'd':
    if (player_now.y < size_arry)
    {
       ++player_now.y;
    }
  //...
}
b.move_player(player_now, player_previous, player_token);

再次抱歉,对于上面的代码片段,手指会输入他们想要的内容。