我正在创建一个游戏,其中有一个玩家对象Hek
一些敌人对象,而游戏世界却是一片沼泽。基本上,可以将沼泽视为4x4的网格,但是可以通过用户输入来更改沼泽的大小(即5x5、6x6、7x7、8x8)。
在规范中,我正在努力工作,提供有关Hek
如何移动的详细信息。 Hek
只能移动到相邻的正方形,因此基于最少4x4网格,他一次可以选择的最大正方形是8。使用Random()
随机计算出要移动到的正方形。
以下是我编写的用于模拟此代码的代码,但它似乎并没有真正起作用。我不知道我的while循环使用不正确吗?
public void moveHek(Hek hek) {
boolean moveMade = false;
while (moveMade != true) {
Random rand = new Random();
int newMove = rand.nextInt(8) + 1;
hek.setMove(newMove);
int x = hek.getMove();
int prevXPos = hek.getXPosition();
int prevYPos = hek.getYPosition();
if (x == 1) {
while(prevXPos > 0 && prevYPos > 0) { //this if while checks that Hek's position isn't located within the top row
hek.setPosition(prevXPos - 1, prevYPos - 1); //and he isn't positioned within the first column of the map
int newX = hek.getXPosition(); //and then moves Hek into the position that is diagonally left upwards to him.
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 2) {
while (prevXPos > 0) { //this if while checks that Hek's position isn't located within the top row and then
hek.setPosition(prevXPos - 1, prevYPos); //moves Hek into the position that is directly above him.
int newX = hek.getXPosition();
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 3) {
while (prevXPos > 0 && prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the top row
hek.setPosition(prevXPos - 1, prevYPos + 1); //and he isn't positioned in the last column of the map, then moves him into the
int newX = hek.getXPosition(); //position that is diagonally right upwards to him.
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 4) {
while (prevYPos > 0) { //this if while checks that Hek's position isn't located within the first column of the map and then
hek.setPosition(prevXPos, prevYPos + 1); //moves him into the position that is directly left.
int newX = hek.getXPosition();
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 5) {
while (prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last column of the map and then
hek.setPosition(prevXPos, prevYPos - 1); //moves him into the position that is directly right.
int newX = hek.getXPosition();
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 6) {
while (prevXPos < gameMap.length && prevYPos > 0) { //this if while checks that Hek's position isn't located within the last row of the map and
hek.setPosition(prevXPos + 1, prevYPos - 1); //that his position isn't located in the first column, then moves him into the position
int newX = hek.getXPosition(); //that is diagonally left downwards to him.
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 7) {
while (prevXPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last row of the map and then moves
hek.setPosition(prevXPos + 1, prevYPos); //him into the position directly below him.
int newX = hek.getXPosition();
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
} else if(x == 8) {
while (prevXPos < gameMap.length && prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last row
hek.setPosition(prevXPos + 1, prevYPos + 1); //and the last column of the map, then it moves him into the position that is diagonally
int newX = hek.getXPosition(); //right downwards to him.
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
}
}
}
任何帮助将不胜感激,正在发生的错误是
java.lang.ArrayIndexOutOfBoundsException: -1
,有时内部while循环中的代码甚至无法执行?谢谢! 最佳答案
该块:
while (prevYPos < gameMap.length) { //this if while checks that Hek's position isn't located within the last column of the map and then
hek.setPosition(prevXPos, prevYPos - 1); //moves him into the position that is directly right.
int newX = hek.getXPosition();
int newY = hek.getYPosition();
this.addHek(hek, newX, newY);
moveMade = true;
}
将
y
位置设置为prevYPos-1
而不检查prevYPos
大于零。另外,如果您尝试避免超出数组范围的顶部,则需要检查
(prevYPos + 1 < gameMap.length)
而不是(prevYPos < gameMap.length)