I’m creating a maze program to get some more practice in Java. I have a method that moves the player, and it returns a boolean value if the move was successful (i.e. it didn’t run into a wall).
Here is the method in question:
public boolean move(Direction direction) {
if(currentLocation == (currentLocation = maze.movePlayer(this, direction))) {
return false;
} else {
return true;
}
}
Obviously, this will always return false. I was wondering if there is a way to check if currentLocation didn’t change (or is equal to the returned value of maze.movePlayer(...) and set them equal if they aren’t). I don’t know if it is possible to do this without calling the method twice or using a local variable.
I hope this makes sense!
You can use the conditional operator: