Advertisement
Guest User

Untitled

a guest
Mar 17th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public enum Direction {
  2.     NORTH(0,-1),
  3.     NORTHEAST(1,-1),
  4.     EAST(1,0),
  5.     SOUTHEAST(1,1),
  6.     SOUTH(0,1),
  7.     SOUTHWEST(-1,1),
  8.     WEST(-1,0),
  9.     NORTHWEST(-1,-1);
  10.    
  11.     int x;
  12.     int y;
  13.    
  14.     Direction(int x, int y)
  15.     {
  16.         this.x = x;
  17.         this.y = y;
  18.     }
  19.    
  20.     public Direction getOpposite()
  21.     {
  22.         return Direction.values()[(this.ordinal()+4)%8]
  23.     }
  24. }
  25.  
  26. ...
  27.  
  28. public bool hasCharInDirection(char[][] minefield, char target, int x, int y, Direction dir)
  29. {
  30.     int newx = x + dir.x;
  31.     int newy = y + dir.y;
  32.     if (0 <= newy && newy < minefield.length
  33.      && 0 <= newx && newx < minefield[0].length)
  34.     {
  35.         return minefield[newy][newx] == target;
  36.     }
  37.     return false;
  38. }
  39.  
  40. ...
  41.  
  42. int mineCount = 0;
  43. for (Direction dir : Direction.values())
  44. {
  45.     mineCount += hasCharInDirection(minefield, '*', x, y, dir) ? 1 : 0;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement