Guest User

Untitled

a guest
Apr 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. enum Dir {
  2. NORTH, SOUTH, EAST, WEST
  3. }
  4.  
  5. class Cell {
  6. public Map<Dir, Cell> neighbors = Collections
  7. .synchronizedMap(new EnumMap<Dir, Cell>(Dir.class));
  8. public Map<Dir, Boolean> walls = Collections
  9. .synchronizedMap(new EnumMap<Dir, Boolean>(Dir.class));
  10.  
  11. public boolean Visited;
  12.  
  13. public Cell() {
  14. Visited = false;
  15. for (Dir direction : Dir.values()) {
  16. walls.put(direction, true);
  17. }
  18. }
  19.  
  20. // Randomly select an unvisited neighbor and tear down the walls
  21. // between this cell and that neighbor.
  22. public Cell removeRandomWall() {
  23. List<Dir> unvisitedDirections = new ArrayList<Dir>();
  24. for (Dir direction : neighbors.keySet()) {
  25. if (!neighbors.get(direction).Visited)
  26. unvisitedDirections.add(direction);
  27. }
  28.  
  29. Random randGen = new Random();
  30. Dir randDir = unvisitedDirections.get(randGen
  31. .nextInt(unvisitedDirections.size()));
  32. Cell randomNeighbor = neighbors.get(randDir);
  33.  
  34. // Tear down wall in this cell
  35. walls.put(randDir, false);
  36. // Tear down opposite wall in neighbor cell
  37. randomNeighbor.walls.put(randDir, false); // <--- instead of randDir, it needs to be it's opposite.
  38.  
  39. return randomNeighbor;
  40. }
  41. }
  42.  
  43. enum Dir {
  44. NORTH,
  45. SOUTH,
  46. EAST,
  47. WEST;
  48.  
  49. public Dir opposite() {
  50. switch(this) {
  51. case NORTH: return Dir.SOUTH;
  52. case SOUTH: return Dir.NORTH;
  53. case EAST: return Dir.WEST;
  54. case WEST: return Dir.EAST;
  55. default: throw new IllegalStateException("This should never happen: " + this + " has no opposite.");
  56. }
  57. }
  58. }
  59.  
  60. randomNeighbor.walls.put(randDir.opposite(), false);
  61.  
  62. public enum Dir {
  63. NORTH { @Override public Dir opposite() { return SOUTH; }},
  64. EAST { @Override public Dir opposite() { return WEST; }},
  65. SOUTH { @Override public Dir opposite() { return NORTH; }},
  66. WEST { @Override public Dir opposite() { return EAST; }},
  67. ;
  68.  
  69. abstract public Dir opposite();
  70. }
  71.  
  72. public enum Direction
  73. {
  74. NORTH,EAST,SOUTH,WEST;
  75.  
  76. public Direction opposite()
  77. {
  78. return Direction.values()[ (this.ordinal() + 2) & 3 ];
  79. }
  80. }
Add Comment
Please, Sign In to add comment