Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.94 KB | None | 0 0
  1. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  2. [1, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  3. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  4. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  5. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  6. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  7. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  8. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  9. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  10. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  11. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  12. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  13. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  14. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  15. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  16. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  17. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  18. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  19. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  20. [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 1]
  21. [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
  22.  
  23. public class Main {
  24. public static void main(String[] args)
  25. {
  26. Scanner reader = new Scanner(System.in);
  27. System.out.print("Enter maze file location: ");
  28. //String fileLocation = reader.nextLine();
  29. reader.close();
  30.  
  31. ArrayList<String> mazeRaw = new ArrayList<String>();
  32. File file = new File("C:\Users\Towarzysz\Desktop\Gentrack Maze Technical Test\Samples\sparse_medium.txt");
  33.  
  34. try
  35. {
  36. String st;
  37. BufferedReader br = new BufferedReader(new FileReader(file));
  38. while ((st = br.readLine()) != null)
  39. mazeRaw.add(st);
  40. br.close();
  41. }
  42. catch (IOException e)
  43. {
  44. System.out.println("Error: " + e);
  45. }
  46.  
  47. Maze testMaze = createMaze(mazeRaw);
  48. int startY = testMaze.returnStartingPosition()[0];
  49. int startX = testMaze.returnStartingPosition()[1];
  50.  
  51. System.out.println(testMaze.returnStartingPosition());
  52. Node p = solveMaze(testMaze, startY, startX);
  53.  
  54. System.out.print(testMaze.maze2D[0].length);
  55.  
  56. String[][] results = new String[testMaze.maze2D.length][testMaze.maze2D[0].length];
  57. for(int i = 0; i < testMaze.maze2D.length; i++){
  58. for(int j = 0; j < testMaze.maze2D[i].length; j++){
  59. results[i][j] = "";
  60. }
  61. }
  62.  
  63. for (int i = 0; i < testMaze.getMaze2D().length; i++) {
  64. for (int j = 0; j < testMaze.getMaze2D()[i].length; j++) {
  65. switch(testMaze.maze2D[i][j])
  66. {
  67. case 0 :
  68. results[i][j] = " ";
  69. break;
  70. case 1 :
  71. results[i][j] = "#";
  72. break;
  73. case -1 :
  74. results[i][j] = " ";
  75. break;
  76. case 9 :
  77. results[i][j] = "E";
  78. break;
  79. }
  80. }
  81. }
  82.  
  83. try
  84. {
  85. while(p.getParent() != null) {
  86. p = p.getParent();
  87. if(testMaze.maze2D[p.x][p.y] == 9)
  88. continue;
  89. results[p.x][p.y] = "X";
  90. System.out.println("x" + p.x + " " + "y" + p.y);
  91. }
  92. results[startY][startX] = "S";
  93. }
  94. catch(java.lang.NullPointerException e)
  95. {
  96. System.out.println("Maze not solvable");
  97. }
  98.  
  99. for (int i = 0; i < results.length; i++) {
  100. for (int j = 0; j < results[i].length; j++) {
  101. System.out.print(results[i][j]);
  102. }
  103. System.out.println();
  104. }
  105. }
  106.  
  107. static Maze createMaze(ArrayList<String> n)
  108. {
  109. Maze testMaze = new Maze(n);
  110. System.out.print(testMaze.toString());
  111. testMaze.drawMaze();
  112. return testMaze;
  113. }
  114.  
  115.  
  116. //public static Queue<Node> q = new LinkedList<Node>();
  117. static Stack q = new Stack();
  118.  
  119. public static Node solveMaze(Maze maze, int x, int y)
  120. {
  121. q.push(new Node(x,y, null));
  122. while(!q.isEmpty()) {
  123. Node p = (Node) q.pop();
  124.  
  125. if (maze.getMaze2D()[p.x][p.y] == 9) {
  126. System.out.print(p.x + " " + p.y);
  127. System.out.println("Exit is reached!");
  128. return p;
  129. }
  130.  
  131. if(isBorder(maze, p.x, p.y, p) != null)
  132. {
  133. q.push(isBorder(maze, p.x, p.y, p));
  134. System.out.println("WALL " + p.toString());
  135. }
  136.  
  137. if(isFree(maze, p.x+1,p.y)) {
  138. maze.getMaze2D()[p.x][p.y] = -1;
  139. Node nextP = new Node(p.x+1,p.y, p);
  140. q.push(nextP);
  141. }
  142.  
  143. if(isFree(maze, p.x-1,p.y)) {
  144. maze.getMaze2D()[p.x][p.y] = -1;
  145. Node nextP = new Node(p.x-1,p.y, p);
  146. q.push(nextP);
  147. }
  148.  
  149. if(isFree(maze, p.x,p.y+1)) {
  150. maze.getMaze2D()[p.x][p.y] = -1;
  151. Node nextP = new Node(p.x,p.y+1, p);
  152. q.push(nextP);
  153. }
  154.  
  155. if(isFree(maze, p.x,p.y-1)) {
  156. maze.getMaze2D()[p.x][p.y] = -1;
  157. Node nextP = new Node(p.x,p.y-1, p);
  158. q.push(nextP);
  159. }
  160.  
  161. }
  162. return null;
  163. }
  164.  
  165.  
  166. public static Node isBorder(Maze maze, int x, int y, Node parent)
  167. {
  168. Node nextNode;
  169. if(x == 0 && maze.getMaze2D()[x][y] != 1)
  170. {
  171. if(maze.getMaze2D()[maze.getMaze2D().length - 1][y] != 1)
  172. return nextNode = new Node(maze.getMaze2D().length - 1, y, parent);
  173. }
  174. if(x == maze.getMaze2D().length - 1 && maze.getMaze2D()[x][y] != 1)
  175. {
  176. if(maze.getMaze2D()[0][y] != 1)
  177. return nextNode = new Node(0, y, parent);
  178. }
  179.  
  180. if(y == 0 && maze.getMaze2D()[x][y] != 1)
  181. {
  182. if(maze.getMaze2D()[x][maze.getMaze2D().length - 1] != 1)
  183. return nextNode = new Node(x, maze.getMaze2D().length - 1, parent);
  184. }
  185. if(y == maze.getMaze2D()[x].length - 1 && maze.getMaze2D()[x][y] != 1)
  186. {
  187. if(maze.getMaze2D()[x][0] != 1)
  188. return nextNode = new Node(x, 0, parent);
  189. }
  190. return null;
  191. }
  192.  
  193. public static boolean isFree(Maze maze, int x, int y) {
  194. if((x >= 0 && x < maze.getMaze2D().length) && (y >= 0 && y < maze.getMaze2D()[x].length) && (maze.getMaze2D()[x][y] == 0 || maze.getMaze2D()[x][y] == 9)) {
  195. return true;
  196. }
  197. return false;
  198. }
  199.  
  200.  
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement