C++;游戏中的二维阵列复制错误 我是C++新手,我想弄清楚为什么我在游戏中玩游戏时会得到两个“*”符号。游戏应该是关于避开巨魔(@)。但是我得到了重复的@和*符号,我不知道为什么。问题似乎要么在for循环中,要么在posX或posY变量中,我通过注释代码段发现了这一点: #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; } } #包括 #包括 使用名称空间std; void ClearScreen() { 让我们简化你的程序吧
在while循环外部初始化电路板。C++;游戏中的二维阵列复制错误 我是C++新手,我想弄清楚为什么我在游戏中玩游戏时会得到两个“*”符号。游戏应该是关于避开巨魔(@)。但是我得到了重复的@和*符号,我不知道为什么。问题似乎要么在for循环中,要么在posX或posY变量中,我通过注释代码段发现了这一点: #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; } } #包括 #包括 使用名称空间std; void ClearScreen() { 让我们简化你的程序吧,c++,arrays,multidimensional-array,C++,Arrays,Multidimensional Array,在while循环外部初始化电路板。 应该没有理由继续初始化它: for (unsigned int row = 0; row < size_arry; ++row) { std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row. } 对于该代码,很抱歉,手指和键盘不配合 要将角色移动到有效的新位置,您必须恢复以前的位置: unsigned int player_now_x; unsigned int p
应该没有理由继续初始化它:
for (unsigned int row = 0; row < size_arry; ++row)
{
std::fill(&a[row][0], &a[row][size_arrx], 'x'); // Fill a row.
}
对于该代码,很抱歉,手指和键盘不配合
要将角色移动到有效的新位置,您必须恢复以前的位置:
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;
对于处理单字母命令,开关
语句可能更具可读性:
// 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;
// ...
}
再次抱歉,对于上面的代码片段,是手指在键入他们想要的内容。听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看程序偏离预期的地方。如果要执行任何操作,这是一个必不可少的工具编程。进一步阅读:请使用适当的缩进,代码读起来很糟糕。看起来您在移动字符时遇到了问题。您需要跟踪字符以前的位置,以便可以恢复。我建议您在打印电路板之前执行字符定位。
posX
和posY
d不依赖于i
或j
,也不在for
循环中进行修改。保持板打印简单且集中。无论这是否解决了当前问题,您都必须在移动后立即纠正位置以留在板上,并在重置板和后立即设置球员位置不是在显示循环期间。
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;
// 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;
// ...
}
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);