Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.93 KB | None | 0 0
  1. class Player{
  2.    
  3.     private Direction oldDirection = Direction.RIGHT;       // Starting value
  4.     public void play(Explorer explorer){
  5.        
  6.         Direction[] directions = {Direction.DOWN, Direction.RIGHT, Direction.UP, Direction.LEFT};
  7.         Direction tryLastDirection = oppositeDirection(oldDirection);   // Try to prevent walking back and forth
  8.        
  9.         for (Direction dir:directions) {
  10.             boolean dirWall = explorer.getSpace(dir).isWall();
  11.            
  12.             if (!dirWall && dir != tryLastDirection) {
  13.                 explorer.walk(dir);
  14.                 oldDirection = dir;
  15.                 break;
  16.             }
  17.         }
  18.         explorer.walk(tryLastDirection);
  19.     }
  20.    
  21.     private Direction oppositeDirection (Direction direction) {
  22.         if (direction == Direction.DOWN) {
  23.             return Direction.UP;
  24.         } else if (direction == Direction.UP) {
  25.             return Direction.DOWN;
  26.         } else if (direction == Direction.RIGHT) {
  27.             return Direction.LEFT;
  28.         } else { // if (direction == Direction.LEFT) {
  29.             return  Direction.RIGHT;
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement