Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1.  
  2. public class Maze {
  3.  
  4. private MazeRunnerStack path;
  5. private Boolean solved;
  6. private static int WIDTH;
  7. private static int HEIGHT;
  8. private static char[][] mazeInfo;
  9. private char[][] position ;
  10. private static int maxCount;
  11.  
  12. public Maze(char[][] mazeInfo) {
  13.  
  14. mazeInfo[0][0] = 'L';
  15. mazeInfo[0][1] = '.';
  16. mazeInfo[0][2] = '|';
  17. mazeInfo[1][0] = 'L';
  18. mazeInfo[1][1] = '_';
  19. mazeInfo[1][2] = '_';
  20.  
  21. this.solved=false;
  22. this.maxCount=mazeInfo.length*mazeInfo[0].length*4;
  23.  
  24.  
  25. };
  26.  
  27. public void setStart(int row, int col) {
  28. position=new char[2][3];
  29. for (int i = 0; i < mazeInfo.length; i++) {
  30. for (int j = 0; j < mazeInfo[i].length; j++) {
  31. position[i][j] = 'z';
  32. }
  33. }
  34. this.position[row][col] = 'S';
  35. };
  36.  
  37. public void setFinish(int row, int col) {
  38. this.position[row][col] = 'F';
  39. };
  40.  
  41. public void displayMaze() {
  42.  
  43. if (true) { // change to !solved
  44. int count = 0;
  45. System.out.println("+---+---+---+");
  46. for (int i = 0; i < mazeInfo.length; i++) {
  47. int[] temp = new int[3];
  48. for (int j = 0; j < mazeInfo[i].length; j++) {
  49. // System.out.print(j);
  50. if (mazeInfo[i][j] == 'L' || mazeInfo[i][j] == '.' || mazeInfo[i][j] == '_'
  51. || mazeInfo[i][j] == '|') {
  52. char ch = mazeInfo[i][j];
  53.  
  54. switch (ch) {
  55. case 'L': // 1
  56.  
  57. if (position[i][j] == 'S')
  58. System.out.print("| S ");
  59. else if (position[i][j] == 'F')
  60. System.out.print("| F ");
  61. else
  62. System.out.print("| ");
  63. temp[j] = 1;
  64. break;
  65. case '.':// 2
  66. if (position[i][j] == 'S')
  67. System.out.print(" S ");
  68. else if (position[i][j] == 'F')
  69. System.out.print(" F ");
  70. else
  71. System.out.print(" ");
  72. temp[j] = 2;
  73. break;
  74. case '|': // 3
  75. if (position[i][j] == 'S')
  76. System.out.print("| S ");
  77. else if (position[i][j] == 'F')
  78. System.out.print("| F ");
  79. else
  80. System.out.print("| ");
  81. temp[j] = 3;
  82. break;
  83. case '_': // 4
  84. if (position[i][j] == 'S')
  85. System.out.print(" S ");
  86. else if (position[i][j] == 'F')
  87. System.out.print(" F ");
  88. else
  89. System.out.print(" ");
  90. temp[j] = 4;
  91. break;
  92.  
  93. }
  94.  
  95. }
  96.  
  97. }
  98. if (count % 2 == 0) //the last char of each line ,
  99. System.out.print("|");
  100. else if (count % 2 == 1)
  101. System.out.print("+");
  102. System.out.println();
  103.  
  104. for (int k = 0; k < temp.length; k++) {
  105.  
  106. if (temp[k] == 1 || temp[k] == 4)
  107. System.out.print("+---");
  108. else if (temp[k] == 2 || temp[k] == 3)
  109. System.out.print("+ ");
  110. }
  111. if (count % 2 == 0) //the last char of the next line
  112. System.out.print("+");
  113. else if (count % 2 == 1)
  114. System.out.print("|");
  115. System.out.println();
  116.  
  117. }
  118. count++;
  119. }
  120. };
  121.  
  122. public void solveMaze() {
  123. int attempts=0;
  124. path = new MazeRunnerStack();
  125. char direction = 'E'; // Can be N, E, S, W to represent North West East and South
  126. Position currPosition;
  127. for(int i=0;i<position.length;i++) {
  128. for(int j=0;j<position[i].length;j++) {
  129. if(position[i][j]=='S') {
  130. currPosition=new Position(i,j);
  131. path.push(currPosition);
  132. break;
  133. }
  134. //path.push(item);
  135. }
  136. }
  137. for(int i=0;i<position.length;i++) {
  138. for(int j=0;j<position[i].length;j++) {
  139. while(!solved) {
  140. if(attempts==maxCount)
  141. break;
  142. if(position[i][j]=='F') {
  143. this.solved=true;
  144. break;
  145. }
  146. switch(direction) {
  147.  
  148. case 'n': //north
  149. case 'N':
  150. if(mazeInfo[i-1]!=null) {
  151. direction='e';
  152. }else if(mazeInfo[i-1][j]=='L'||mazeInfo[i-1][j]=='_')
  153. direction='e';
  154. else {
  155. currPosition=new Position(i-1,j);
  156. path.push(currPosition);
  157. currPosition=path.peek();
  158. System.out.print(currPosition);
  159.  
  160. }
  161. break;
  162.  
  163. case 'e': //east
  164. case 'E':
  165. if(mazeInfo[i][j+1]=='L'||mazeInfo[i][j+1]=='|') {
  166. direction='s';
  167. }else {
  168. currPosition=new Position(i,j+1);
  169. path.push(currPosition);
  170.  
  171.  
  172.  
  173. }
  174. break;
  175.  
  176. case 's': //south
  177. case 'S':
  178. if(mazeInfo[i+1]!=null)
  179. direction='w';
  180. else if(mazeInfo[i][j]=='L'||mazeInfo[i][j]=='_') {
  181. direction='w';
  182. }else {
  183. currPosition=new Position(i+1,j);
  184. path.push(currPosition);
  185.  
  186.  
  187. }
  188. break;
  189.  
  190. case 'w': //west
  191. case 'W':
  192. if(mazeInfo[i][j]=='L'||mazeInfo[i][j]=='|')
  193. direction='n';
  194. else {
  195. currPosition=new Position(i,j-1);
  196. path.push(currPosition);
  197. currPosition=path.peek();
  198. System.out.print(currPosition);
  199.  
  200. }
  201. break;
  202.  
  203. }
  204. attempts++;
  205. }
  206. }
  207. }
  208.  
  209.  
  210. };
  211.  
  212. public static void main(String[] args) {
  213. mazeInfo=new char[WIDTH][HEIGHT];
  214. Maze test = new Maze(mazeInfo);
  215. test.setStart(0, 0);
  216. test.setFinish(0, 2);
  217. test.displayMaze();
  218. test.solveMaze();
  219. System.out.println("Solution is:");
  220. test.displayMaze();
  221. System.out.print("Path is:");
  222.  
  223.  
  224. }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement