Advertisement
6666969

MazeSolver Part 3

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