gpt4 book ai didi

java - 为什么我的方法不能正常工作?

转载 作者:行者123 更新时间:2023-12-01 10:49:27 25 4
gpt4 key购买 nike

我正在创建一个游戏,其中有一个玩家对象 Hek 一些敌人对象,游戏世界是一个沼泽。基本上,沼泽可以被认为是 4x4 网格,但沼泽大小可以通过用户输入来改变(即 5x5、6x6、7x7、8x8)。

在我正在编写的规范中,详细介绍了 Hek 如何移动。 Hek 只能移动到相邻的方格,因此基于最小 4x4 网格,他一次可以选择的最多方格是 8。他移动到的方格是随机计算的,使用 随机()

下面是我编写的模拟此代码的代码,但它似乎并没有真正起作用。我不知道我的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 循环中的代码甚至不执行?谢谢!

最佳答案

该 block :

    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)

关于java - 为什么我的方法不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34001693/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com