Advertisement
6666969

Maze Solver Part 4

Mar 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.48 KB | None | 0 0
  1. /**
  2. * Solves a maze
  3. *
  4. * @Saman Verma
  5. * @3/17/18
  6. */
  7.  
  8. import java.util.*;
  9. import java.io.*;
  10.  
  11. public class MazeSolver
  12. { public Stack<String> MazePath = new Stack <String>();
  13. public Stack<String> oldPathStack = new Stack <String>();
  14. public String Location;
  15. public int i = 0;
  16. public ArrayList <String> oldPath = new ArrayList<String>();
  17.  
  18. public ArrayList <String> maze = new ArrayList<String>();
  19. public void MazeReader(){
  20.  
  21.  
  22. try{Scanner MazeScanner = new Scanner(new BufferedReader(new FileReader("maze.txt")));
  23. while(MazeScanner.hasNextLine()){
  24. maze.add(MazeScanner.nextLine());
  25.  
  26. }}catch(Exception e){
  27. System.out.print(e.getMessage());
  28. e.printStackTrace();}
  29. for(int y = 0; y<maze.size(); y++){
  30. int j = 0;
  31. if (j != 0) {i++;}
  32. j++;
  33. for(int x = 0; x<maze.get(y).length(); x++){
  34.  
  35. String check = maze.get(y).substring(x,x+1);
  36.  
  37. if(check.equals("@")){
  38. String yCoordinate = String.valueOf(y);
  39. String xCoordinate = String.valueOf(x);
  40. Location = xCoordinate + "," + yCoordinate;
  41. MazePath.push(Location);
  42. }
  43. }
  44. }
  45.  
  46. }
  47. public static void main(String [] args){
  48. MazeSolver MazeSolver = new MazeSolver();
  49. MazeSolver.MazeReader();
  50. MazeSolver.solveTheMaze();
  51. MazeSolver.printer();
  52.  
  53. }
  54. public void Left(int x, int y){
  55.  
  56. x = Integer.parseInt(Location.substring(0,1));
  57. y = Integer.parseInt(Location.substring(2,3));
  58. x = x-1;
  59. if(x <0) {
  60. return;
  61. }
  62. boolean hasBeen = oldPlace(x, y);
  63. if(hasBeen == true){return;}
  64. String xCd = String.valueOf(x);
  65. String yCd = String.valueOf(y);
  66. if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
  67. Location = xCd + "," + yCd;
  68. MazePath.push(Location);
  69. }
  70.  
  71.  
  72. }
  73. public void Right(int x, int y){
  74. String Position = MazePath.peek();
  75. x = Integer.parseInt(Location.substring(0,1));
  76. y = Integer.parseInt(Location.substring(2,3));
  77. x = x+1;
  78. boolean hasBeen = oldPlace(x, y);
  79. if(hasBeen == true){return;}
  80. String xCd = String.valueOf(x);
  81. String yCd = String.valueOf(y);
  82. if(maze.get(y).substring(x,x+1) == null) {return;}
  83. else if(maze.get(y).substring(x,x+1) == "." || maze.get(y).substring(x,x+1) == "$"){
  84. Location = xCd + "," + yCd;
  85. MazePath.push(Location);
  86. }
  87.  
  88.  
  89.  
  90. }
  91. public void Down(int x, int y){
  92.  
  93. x = Integer.parseInt(Location.substring(0,1));
  94. y = Integer.parseInt(Location.substring(2,3));
  95. y = y-1;
  96. String xCd = String.valueOf(x);
  97. String yCd = String.valueOf(y);
  98. if(y <0) {
  99. return;
  100. }
  101. boolean hasBeen = oldPlace(x, y);
  102. if(hasBeen == true){return;}
  103. if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
  104. Location = xCd + "," + yCd;
  105. MazePath.push(Location);
  106. }
  107.  
  108. }
  109. public void Up(int x, int y){
  110.  
  111. x = Integer.parseInt(Location.substring(0,1));
  112. y = Integer.parseInt(Location.substring(2,3));
  113. y = y+1;
  114. String xCd = String.valueOf(x);
  115. String yCd = String.valueOf(y);
  116.  
  117. boolean hasBeen = oldPlace(x, y);
  118. if(hasBeen == true){return;}
  119. else if(maze.get(y).substring(x,x+1).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
  120. Location = xCd + "," + yCd;
  121. MazePath.push(Location);
  122. }
  123.  
  124.  
  125.  
  126. }
  127. public String getCurrentPosition(){
  128. String currentPosition = MazePath.peek();
  129. return currentPosition;
  130. }
  131. public Stack solveTheMaze(){
  132. int x = Integer.parseInt(Location.substring(0,1));
  133. int y = Integer.parseInt(Location.substring(2,3));
  134. oldPathStack.push(Location);
  135. while(!(maze.get(y).substring(x,x+1).equals("$"))){
  136.  
  137. boolean hasBeen = oldPlace(x, y);
  138.  
  139. Left(x, y);
  140. oldPathStack.push(Location);
  141. x = Integer.parseInt(Location.substring(0,1));
  142. y = Integer.parseInt(Location.substring(2,3));
  143.  
  144. Right(x, y);
  145. oldPathStack.push(Location);
  146. x = Integer.parseInt(Location.substring(0,1));
  147. y = Integer.parseInt(Location.substring(2,3));
  148. Down(x, y);
  149. oldPathStack.push(Location);
  150. x = Integer.parseInt(Location.substring(0,1));
  151. y = Integer.parseInt(Location.substring(2,3));
  152. Up(x, y);
  153. oldPathStack.push(Location);
  154. x = Integer.parseInt(Location.substring(0,1));
  155. y = Integer.parseInt(Location.substring(2,3));
  156.  
  157. boolean r = RightChecker(x, y);
  158. boolean l = LeftChecker(x, y);
  159. boolean u = UpChecker(x, y);
  160. boolean d = DownChecker(x, y);
  161.  
  162. while(r== false && l== false && u== false && d == false){
  163. String check = maze.get(y).substring(x,x+1);
  164.  
  165. if(check.equals("@")) {
  166.  
  167. break;
  168.  
  169. }
  170. while (hasBeen == true)
  171. {MazePath.pop();
  172. Location = MazePath.peek();
  173. x = Integer.parseInt(Location.substring(0,1));
  174. y = Integer.parseInt(Location.substring(2,3));
  175. r = RightChecker(x, y);
  176. l = LeftChecker(x, y);
  177. u = UpChecker(x, y);
  178. d = DownChecker(x, y);
  179. hasBeen = oldPlace(x, y);
  180. }
  181. }
  182.  
  183. }
  184. return MazePath;
  185. }
  186. public boolean RightChecker(int x, int y){
  187. x = Integer.parseInt(Location.substring(0,1));
  188. y = Integer.parseInt(Location.substring(2,3));
  189.  
  190. int xCheck = x +1;
  191. int yCheck = y;
  192. String xCd = String.valueOf(xCheck);
  193. String yCd = String.valueOf(yCheck);
  194. if(maze.get(yCheck).substring(xCheck).equals(".") || maze.get(y).substring(x,x+1).equals("$")){
  195. return true;
  196. }else if(maze.get(yCheck).substring(xCheck, xCheck + 1) == null){
  197. return false;
  198. }
  199. else{return false;}
  200.  
  201. }
  202. public boolean LeftChecker(int x, int y){
  203. x = Integer.parseInt(Location.substring(0,1));
  204. y = Integer.parseInt(Location.substring(2,3));
  205.  
  206. x = x -1;
  207. y = y;
  208. String xCd = String.valueOf(x);
  209. String yCd = String.valueOf(y);
  210. if(maze.get(y).substring(x, x +1).equals(".") || maze.get(y).substring(x, x+1).equals("$")){
  211. return true;
  212. }else if(maze.get(y).substring(x, x+1) == null){
  213. return false;
  214. }
  215. else{return false;}
  216.  
  217. }
  218. public boolean UpChecker(int x, int y){
  219. x = Integer.parseInt(Location.substring(0,1));
  220. y = Integer.parseInt(Location.substring(2,3));
  221.  
  222. int xCheck = x;
  223. int yCheck = y + 1;
  224. String xCd = String.valueOf(xCheck);
  225. String yCd = String.valueOf(yCheck);
  226. if(yCheck > i) {return false;}
  227. else if(maze.get(yCheck).substring(xCheck, xCheck +1).equals(".") || maze.get(yCheck).substring(xCheck,xCheck+1).equals("$")){
  228. return true;
  229. } else if(maze.get(yCheck).substring(xCheck, xCheck +1) == null){
  230. return false;
  231. }
  232. else{return false;}
  233.  
  234. }
  235. public boolean DownChecker(int x, int y){
  236. x = Integer.parseInt(Location.substring(0,1));
  237. y = Integer.parseInt(Location.substring(2,3));
  238.  
  239. int xCheck = x;
  240. int yCheck = y - 1;
  241. String xCd = String.valueOf(xCheck);
  242. String yCd = String.valueOf(yCheck);
  243. if(yCheck < 0) {return false;}
  244. else if(maze.get(yCheck).substring(xCheck, xCheck +1).equals(".") || maze.get(yCheck).substring(xCheck,xCheck+1).equals("$")){
  245. return true;
  246. } else if(maze.get(yCheck).substring(xCheck, xCheck +1) == null){
  247. return false;
  248. }
  249. else{return false;}
  250.  
  251. }
  252. public void printer(){
  253. while(MazePath.peek() != null){
  254. System.out.println(MazePath.peek());
  255. }
  256. MazePath.pop();
  257. }
  258. public boolean oldPlace(int x, int y){
  259. boolean trueornot = false;
  260. for (y = 0; y< oldPath.size(); y++){
  261.  
  262. if(oldPath.get(y).equals(MazePath.peek())){
  263. trueornot = true;
  264. }
  265.  
  266. }
  267. if(trueornot == true){
  268. return true;
  269. }
  270. else{
  271. return false;
  272. }
  273. }
  274.  
  275.  
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement