Advertisement
Shabbyshab

RIP MazeSolver

Apr 13th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. /**
  2. * Rishabh Sud
  3. * Maze Solver
  4. */
  5. import java.io.*;
  6. import java.util.*;
  7. public class MazeSolver {
  8. public ArrayList <String> maize = new ArrayList<>();
  9. public Stack <Position> SValue = new Stack<>();
  10. public ArrayList <Position> olmaize = new ArrayList();
  11. /**
  12. * Uses a constructor to start the process of creating a maze
  13. */
  14. public MazeSolver (){
  15. Mazebuilder();
  16. }
  17. public void Mazebuilder() {
  18. try {
  19. Scanner file = new Scanner ( new BufferedReader( new FileReader("maze.txt")));
  20. while (file.hasNextLine()) {
  21. maize.add(file.nextLine());
  22. }
  23. file.close();
  24. }
  25. catch(Exception e){
  26. System.out.print(e.getMessage());
  27. e.printStackTrace();
  28. }
  29. for( int x = 0; x < maize.size(); x++) {
  30. for (int y; y < maize.get(x).length(); y++) {
  31. if( maize.get(x).substring(y, y+1).equals("@")) {
  32. SValue.push(new Position(x,y)); //Finds the Start of the Maze and pushes the location onto the stack
  33. System.out.println("Your Starting position is" + x + y);
  34. Algo(x,y);
  35. }
  36. }
  37. }
  38. }
  39. public static void main (String [] args) {
  40. new MazeSolver();
  41. }
  42. //Major Algorithim for the project
  43. public void Algo(int x, int y) {
  44. if ((maize.get(SValue.peek().getXCoordinate()).substring(x,y)).equals("$")) {
  45. done(x,y);
  46. }
  47. Up(x,y);
  48. Down(x,y);
  49. Left(x,y);
  50. Right(x,y);
  51. }
  52. /*
  53. * Method used to check whether you can go UP
  54. */
  55. public void Up(int up1, int up2) {
  56. if(up2 != 0){ //Checks for Wall
  57. if (maize.get(SValue.peek().getXCoordinate()).substring(up1,up2+1).equals(".")
  58. || ((SValue.peek().getXCoordinate()).substring(up1,up2+1).equals("$"))) {
  59. if (check(up1,up2+1) != false) {
  60. Algo(up1,up2+1);
  61. }
  62. }
  63. }
  64. else {
  65. Down(up1, up2);
  66. }
  67. }
  68. /*
  69. * Method used to check whether you can go UP
  70. */
  71. public void Down(int down1, int down2) {
  72. if(down2 < maize.size()){ //Checks for bottom of Maze
  73. if (maize.get(SValue.peek().getXCoordinate()).substring(down1,down2-1).equals(".")
  74. || (maize.get(SValue.peek().getXCoordinate()).substring(down1,down2-1).equals("$"))) {
  75. if ( check(down1,down2-1) != false) {
  76. Algo(down1,down2-1);
  77. }
  78. }
  79. }
  80. else {
  81. Left(down1, down2);
  82. }
  83. }
  84. /*
  85. * Method used to check whether you can go UP
  86. */
  87. public void Left(int left1, int left2) {
  88. if (maize.get(SValue.peek().getXCoordinate()).substring(left1-1,left2).equals(".")
  89. || ((maize.get(SValue.peek().getXCoordinate()).substring(left1-1,left2).equals("$")))) {
  90. if ( check(left1-1,left2) != false) {
  91. Algo(left1-1,left2);
  92. }
  93. }
  94. else if(check(left1-1,left2) != false) {
  95. Right(left1, left2);
  96. }
  97. }
  98. /*
  99. * Method used to check whether you can go Right
  100. */
  101. public void Right(int right1, int right2) {
  102. if ((maize.get(SValue.peek().getXCoordinate()).substring(right1+1,right2).equals(".")
  103. || (maize.get(SValue.peek().getXCoordinate()).substring(right1+1,right2).equals("$")))) {
  104. if(check(right1+1, right2) != false){
  105. Algo(right1+1,right2);
  106. }
  107. }
  108. else {
  109. SValue.pop(new Position(right1,right2));
  110. error(right1,right2);
  111. }
  112. }
  113. //Error if no points are available to move
  114. public void error(int e1, int e2) {
  115. if (SValue.peek() == null) {
  116. System.out.println("HOUSTON WE HAVE AN ERROR!, Maze Cannote be solved given the present Algorithims and without going backwards");
  117. }
  118. }
  119. //Finishes the program
  120. public void done(int done1, int done2) {
  121. System.out.println("We done boyz");
  122. return;
  123. }
  124. // Used to check whether program has been there, or "backtracking"
  125. //Puts the Coordinates into the stack
  126. public boolean check(int check1, int check2) {
  127. boolean hasbeen = false;
  128. for( int a = 0; a < olmaize.size(); a++) {
  129. if(olmaize.get(a).equals(new Position (check1,check2))){
  130. hasbeen = true;
  131. break;
  132. }
  133. if(!hasbeen) {
  134. SValue.push(new Position(check1,check2));
  135. System.out.println("Cordinates" + check1 + "," + check2);
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141. public class Position{
  142. public int downL;
  143. public int upL;
  144. public Position (int x, int y) {
  145. downL = x;
  146. upL = y;
  147. }
  148. public int getXCoordinate(){
  149. return downL;
  150. }
  151. public boolean equals(Object obj){
  152. if(obj == this){
  153. return true;
  154. }
  155. if(obj instanceof Position){
  156. Position location = (Position) obj;
  157. return (downL == Position.getXCoordinate());
  158. }
  159. return false;
  160. }
  161. }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement