【发布时间】:2020-01-21 19:34:53
【问题描述】:
我一直在开发一款地牢爬行游戏,但在我的二维数组中移动玩家坐标时遇到了问题。目前,在选择移动时,移动将传递到函数中,但位置不会重新分配。我已经尝试过通过我的参考和价值。
目前编写的程序让玩家移动,但我认为它实际上并没有被传递到 updateMove 函数中。我尝试使用函数 cout
我已经添加了我的主要功能和相关功能:
int main()
{
// initialize random #
srand(static_cast<int>(time(NULL)));
do // play again loop
{
// define variables
bool win, lose;
char playerMove;
char dungeon[MAX_ROW][MAX_COL];
int cash;
displayInstructions(); // display instructions
initializeDungeon(dungeon); // initialize dungeon
do
{
setTraps(dungeon); // set traps
displayDungeon(dungeon); // display dungeon
playerMove = getMove(playerMove);
// check for valid move
// reassign player to new position
updateMove(playerMove, dungeon);
//update map now that move is legit
if (!win && !lose)
{
updateDungeon();
}
} while (!win && !lose);
} while (repeat());
}
void updateMove(char & playerMove, char dungeon[MAX_ROW][MAX_COL])
{
int px, py;
int dx = 0, dy = 0;
if(playerMove == UP && px != 0)
{
dx--;
}
else if (playerMove == DOWN && px != 7)
{
dx++;
}
else if (playerMove == LEFT && py != 0)
{
dy--;
}
else if (playerMove == RIGHT && py != 8)
{
dy++;
}
if(dx != 0 || dy != 0)
{
dungeon[px][py] = SPACE; // I updated this to be an 'empty' space based on my variables
px += dx;
py += dy;
dungeon[px][py] = PLAY;
}
} ````
【问题讨论】:
-
你的问题是什么??
-
没有通过这个函数重新分配位置。我想知道我是否遗漏了什么?据我所知,此举已传递到函数中,但似乎对它没有任何作用
-
您将新位置设置为 PLAY(无论是什么),但您从未更改 '@' 的旧位置。
-
@MadisonFronabarger 请编辑您的问题以添加说明。不要只是发表评论。
-
分而治之。备份你的代码,然后破解你的方式来制作一个具有相同不当行为的非常小的程序。当您的程序只是您要查找的错误时,很容易找到错误。您可能仍然不知道为什么它是一个错误,但您发现了它并且处于提出针对性问题的非常好的位置。使用minimal reproducible example 获取灵感。