过去几周我一直在制作一个迷宫游戏,并在3天前完成了编码。我一直在测试游戏中的任何错误,一切似乎都没问题,但是我遇到的一个主要问题是,如果我想在游戏的一次运行中玩同样的游戏,游戏就会崩溃。例如,我玩1级和2级,如果我在主菜单上再次选择其中任何一个,则游戏会出现空白屏幕并崩溃。 我已将错误源于我的三个函数,其中包括:
void position(int lvl) {
int x, y;
if (lvl == 1) {
x = startingPointX(lvl, y);
lvl1[x][y] = me;
}
if (lvl == 2) {
x = startingPointX(lvl, y);
lvl2[x][y] = me;
}
if (lvl == 3) {
x = startingPointX(lvl, y);
lvl3[x][y] = me;
}
if (lvl == 4) {
x = startingPointX(lvl, y);
lvl4[x][y] = me;
}
if (lvl == 5) {
x = startingPointX(lvl, y);
lvl5[x][y] = me;
}
if (lvl == 6) {
x = startingPointX(lvl, y);
lvl6[x][y] = me;
}
}
这是第二个功能:
int positionFinder(int lvl, int &y) {
int xCoord;
if (lvl == 1) {
/*This is for level 1*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl1[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 2) {
/*This is for level 2*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl2[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 3) {
/*This is for level 3*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl3[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 4) {
/*This is for the solution of level 1*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl4[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 5) {
/*This is for the solution of level 2*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl5[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 6) {
/*This is for the solution of level 3*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl6[i][j] == '@') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
return 0;
}
第三个功能是:
int startingPointX(int lvl, int &y) {
int xCoord;
if (lvl == 1) {
/*This is for level 1*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl1[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 2) {
/*This is for level 2*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl2[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 3) {
/*This is for level 3*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl3[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 4) {
/*This is for the solution of level 1*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl4[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 5) {
/*This is for the solution of level 2*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl5[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
if (lvl == 6) {
/*This is for the solution of level 3*/
for (int i = 0; i != 20; ++i) {
for (int j = 0; j != 20; ++j) {
if (lvl6[i][j] == 'X') {
xCoord = i;
y = j;
return xCoord;
}
}
}
}
return 0;
}
崩溃的原因是当等级运行时它会在int startingPointX中搜索X,然后在无效位置游戏搜索我,这是'@',因此如果我开始第二级,那么在关卡中不再是'X'。函数int startingPointX返回0,它不会设置y,给y一个随机值。因此,行lvl1 [x] [y];在无效位置会让游戏做随机事情,因为y有一个随机值,这可能超出范围,导致崩溃。
我已经尝试了很多方法来改变函数的工作方式,但每次它最终都会崩溃。感谢帮助,谢谢
以下是编译和运行的完整代码: https://drive.google.com/open?id=0B2lLG0MNZMxdT3kteTBtR0puWlE 我使用的是Windows Visual Studio 2015
答案 0 :(得分:0)
我已经提出了多种可以尝试实施的潜在解决方案:
我的第一个想法是在调用每个级别时硬编码me
的起始位置,而不是搜索' X'。您对应用程序进行编码的方式,还要求在完成关卡时,将me
的当前位置更改回空白区域,或者在结束时仍然会有@再次调用level。
另一个快速解决方案是存储起始位置坐标并将存储的坐标设置回' X'当你完成一个关卡时,再次将@更改回一个空格。
您是否尝试将每个级别设为const字符数组,并将它们复制到另一个char数组中,例如' CurrentLevel'?
您可以使用memcpy
来实现此目的,例如:
const char lvl1[20][20] = (your array);
char currentLevel[20][20] = {0}; //all initialised to 0
稍后在您的代码中,当您想要创建所需的级别时:
//Copy the contents of lvl1 into currentLevel, up to the size of the array
memcpy(currentLevel, lvl1, sizeof(currentLvel));
使用这种方法,您可以重新创建每个级别,包括' X' 您必须对代码进行更改以适应此解决方案,但是请参考currentLevel而不是1 - 6级。