Using recursion in order to get my player to the goal.
I really want to know something though.
In this area of my code:
/////////////////////////Y <= tYy?
Why does ‘y’ need to be ‘<=’ and ‘x’ is just ‘<’?
public boolean runMaze()
{
boolean done = moveTowardsTarget(player.getX(), player.getY(), goal.getX(), goal.getY());
System.out.println("\n" + player.getX() + " " + player.getY());
System.out.println(goal.getX() + " " + goal.getY());
return done;
}
private boolean moveTowardsTarget(int x, int y, int tX, int tY)
{
display();
int xTx = (x - tX);
int tXx = (tX - x);
int yTy = (y - tY);
int tYy = (tY - y);
if(xTx < tXx && x > -1 && x < 3)
{
playPosLastX = player.getX();
playPosLastY = player.getY();
movePlayerToTile(x++, y);
return moveTowardsTarget(x, y, tX, tY);
}
/////////////////////////Y <= tYy?
else if(yTy <= tYy && y > -1 && y < 3)
{
playPosLastX = player.getX();
playPosLastY = player.getY();
movePlayerToTile(x, y++);
return moveTowardsTarget(x, y, tX, tY);
}
else
{
return true;
}
}
private void movePlayerToTile(int x, int y)
{
if(directionIsValid(x, y))
{
player.setLocation(x, y);
tiles[playPosLastX][playPosLastY] = Entities.FLOOR;
tiles[player.getX()][player.getY()] = player;
}
}
private boolean directionIsValid(int x, int y)
{
if(x < 0 || y < 0 || x > 2 || y > 2)
return false;
else if(!tiles[x][y].isPassable())
return false;
return true;
}
private void display()
{
System.out.println("\n");
for(int y=0; y < 3; y++)
{
for(int x=0; x < 3; x++)
{
if((x % 2) == 0 && x != 0)
System.out.println(tiles[x][y].getValue());
else
System.out.print(tiles[x][y].getValue());
}
}
}