Guest User

Untitled

a guest
Feb 24th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. **
  2. * @author Lewis and Chase
  3. *
  4. * Represents a maze of characters. The goal is to get from the
  5. * top left corner to the bottom right, following a path of 1’s.
  6. */
  7. import jss2.*;
  8. public class Maze
  9. {
  10. /**
  11. * constant to represent tried paths
  12. */
  13. private final int TRIED = 3;
  14. /**
  15. * constant to represent the final path
  16. */
  17. private final int PATH = 7;
  18. /**
  19. * two dimensional array representing the grid
  20. */
  21. private int [][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1},
  22. {1,0,0,1,1,0,1,1,1,1,0,0,1},
  23. {1,1,1,1,1,0,1,0,1,0,1,0,0},
  24. {0,0,0,0,1,1,1,0,1,0,1,1,1},
  25. {1,1,1,0,1,1,1,0,1,0,1,1,1},
  26. {1,0,1,0,0,0,0,1,1,1,0,0,1},
  27. {1,0,1,1,1,1,1,1,0,1,1,1,1},
  28. {1,0,0,0,0,0,0,0,0,0,0,0,0},
  29. {1,1,1,1,1,1,1,1,1,1,1,1,1} };
  30. /**
  31. * push a new attempted move onto the stack
  32. * @param x represents x coordinate
  33. * @param y represents y coordinate
  34. * @param stack the working stack of moves within the grid
  35. * @return StackADT<Position> stack of moves within the grid
  36. */
  37. private StackADT<Position> push_new_pos(int x, int y,
  38. StackADT<Position> stack)
  39. {
  40. Position npos = new Position();
  41. npos.setx(x);
  42. npos.sety(y);
  43. if (valid(npos.getx(),npos.gety()))
  44. stack.push(npos);
  45. return stack;
  46. }
  47.  
  48. /**
  49. * Attempts to iteratively traverse the maze. It inserts special
  50. * characters indicating locations that have been tried and that
  51. * eventually become part of the solution. This method uses a
  52. * stack to keep track of the possible moves that could be made.
  53. * @return boolean returns true if the maze is successfully traversed
  54. */
  55. public boolean traverse ()
  56. {
  57. boolean done = false;
  58. Position pos = new Position();
  59. Object dispose;
  60. StackADT<Position> stack = new LinkedStack<Position> ();
  61. stack.push(pos);
  62. while (!(done))
  63. {
  64. pos = stack.pop();
  65. grid[pos.getx()][pos.gety()] = TRIED; // this cell has been tried
  66. if (pos.getx() == grid.length-1 && pos.gety() == grid[0].length-1)
  67. done = true; // the maze is solved
  68. else
  69. {
  70. stack = push_new_pos(pos.getx(),pos.gety() - 1, stack);
  71. stack = push_new_pos(pos.getx(),pos.gety() + 1, stack);
  72. stack = push_new_pos(pos.getx() - 1,pos.gety(), stack);
  73. stack = push_new_pos(pos.getx() + 1,pos.gety(), stack);
  74. }
  75. }
  76. return done;
  77. }
  78. /**
  79. * Determines if a specific location is valid.
  80. * @param row int representing y coordinate
  81. * @param column int representing x coordinate
  82. * @return boolean true if the given coordinate is a valid move
  83. */
  84. private boolean valid (int row, int column)
  85. {
  86. boolean result = false;
  87. /** Check if cell is in the bounds of the matrix */
  88.  
  89. if (row >= 0 && row < grid.length &&
  90. column >= 0 && column < grid[row].length)
  91. /** Check if cell is not blocked and not previously tried */
  92. if (grid[row][column] == 1)
  93. result = true;
  94. return result;
  95. }
  96. /**
  97. * Returns the maze as a string.
  98. * @return String representation of the maze grid
  99. */
  100. public String toString ()
  101. {
  102. String result = "\n";
  103. for (int row=0; row < grid.length; row++)
  104. {
  105. for (int column=0; column < grid[row].length; column++)
  106. result += grid[row][column] + "";
  107. result += "\n";
  108. }
  109. return result;
  110. }
  111. }
  112. ////////////////NYKLASSE///////////
  113. /**
  114. * @author Lewis and Chase
  115. *
  116. * Demonstrates a simulation of recursion using a stack.
  117. */
  118. public class MazeSearch
  119. {
  120. /**
  121. * Creates a new maze, prints its original form, attempts to
  122. * solve it, and prints out its final form.
  123. * @param args array of Strings
  124. */
  125. public static void main (String[] args)
  126. {
  127. Maze labyrinth = new Maze();
  128. System.out.println (labyrinth);
  129. if (labyrinth.traverse ())
  130. System.out.println (“The maze was successfully traversed!”);
  131. else
  132. System.out.println (“There is no possible path.”);
  133. System.out.println (labyrinth);
  134. }
  135. }
Add Comment
Please, Sign In to add comment