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

时间:2015-11-30 15:08:08

标签: java oop if-statement while-loop nested

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

在规范中,我正在使用,提供有关Hek如何移动的详细信息。 Hek只能移动到相邻的正方形,因此他可以根据最小4x4网格一次选择的最大正方形为8.他进入的平方随机计算,使用{{1} }。

下面是我为编写此代码而编写的代码,但它似乎并没有真正起作用。我不知道我的while循环是否被错误地使用了?

Random()

任何帮助都将非常感激,正在发生的错误是 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; } } } } ,有时内部while循环中的代码甚至不执行?谢谢!

1 个答案:

答案 0 :(得分:1)

这个区块:

    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)