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

Why won't my method work correctly?

我正在创建一个游戏,其中有一个玩家对象 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)