Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Player{
- private Direction oldDirection = Direction.RIGHT; // Starting value
- public void play(Explorer explorer){
- Direction[] directions = {Direction.DOWN, Direction.RIGHT, Direction.UP, Direction.LEFT};
- Direction tryLastDirection = oppositeDirection(oldDirection); // Try to prevent walking back and forth
- for (Direction dir:directions) {
- boolean dirWall = explorer.getSpace(dir).isWall();
- if (!dirWall && dir != tryLastDirection) {
- explorer.walk(dir);
- oldDirection = dir;
- break;
- }
- }
- explorer.walk(tryLastDirection);
- }
- private Direction oppositeDirection (Direction direction) {
- if (direction == Direction.DOWN) {
- return Direction.UP;
- } else if (direction == Direction.UP) {
- return Direction.DOWN;
- } else if (direction == Direction.RIGHT) {
- return Direction.LEFT;
- } else { // if (direction == Direction.LEFT) {
- return Direction.RIGHT;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement