I'm creating a game which has a player object Hek
some enemy objects and the game world is a swamp. Basically the swamp can be thought of as a 4x4 grid, but the swamp size can be varied (i.e. 5x5, 6x6, 7x7, 8x8) by user input.
Within the specification, I'm working from, gives details on how Hek
can move. Hek
can only move to neighbouring squares, so the most squares he could pick from at one time based on a minimum of 4x4 grid is 8. The square in which he moves into is calculated at random, using Random()
.
Below is the code I have written to simulate this, but it doesn't seem to be working really. I don't know if my while loops are being used incorrectly?
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;
}
}
}
}
Any help would be greatly appreciated, the error that is occurring is java.lang.ArrayIndexOutOfBoundsException: -1
and sometimes the code within the inner while loops don't even execute? Thanks!
Aucun commentaire:
Enregistrer un commentaire