Advertisement
Shabbyshab

Maze Runner/Scnaner

Mar 18th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.97 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. public class MazeSolver {
  4. public ArrayList <String> maize = new ArrayList<>();
  5. public Stack <Position> SValue = new Stack<>();
  6. public ArrayList <Position> olmaize = new ArrayList();
  7. public void Mazebuilder() {
  8. try {
  9. Scanner file = new Scanner ( new BufferedReader( new FileReader("maze.txt")));
  10. while (file.hasNextLine()) {
  11. maize.add(file.nextLine());
  12. }
  13. file.close();
  14. }
  15. catch(Exception e){
  16. System.out.print(e.getMessage());
  17. e.printStackTrace();
  18. }
  19. for( int x = 0; x < maize.size(); x++) {
  20. for (int y; y < maize.get(x).length; y++) {
  21. if( maize.get(x).substring(y, y+1).equals("@")) {
  22. SValue.push(new Position(x,y)); //Finds the Start of the Maze and pushes the location onto the stack
  23. System.out.println("Your Starting position is" + x + y);
  24. Algo(x,y);
  25. }
  26. }
  27. }
  28. }
  29. /**
  30. * Uses a constructor to start the process of creating a maze
  31. */
  32. public static void main ( String [] args ) {
  33. new Mazebuilder ();
  34. }
  35. public void Algo(int x, int y) {
  36. SValue.push(new Position(x,y));
  37. System.out.println("Cordinates" + x + "," + y);
  38. Up(x,y);
  39. Down(x,y);
  40. Left(x,y);
  41. Right(x,y);
  42. }
  43. /*
  44. * Method used to check whether you can go UP
  45. */
  46. public void Up(int up1, int up2) {
  47. if(up2 != 0){ //Checks for Wall
  48. SValue.push(new Position(up1,up2));
  49. }
  50. if (SValue.substring(up1,up2+1).equals(".")
  51. || SValue.substring(up1,up2+1).equals("$")) {
  52. Algo(up1,up2+1);
  53. }
  54. else {
  55. SValue.pop(Position(up1,up2));
  56. Down(up1, up2);
  57. }
  58. }
  59. /*
  60. * Method used to check whether you can go UP
  61. */
  62. public void Down(int down1, int down2) {
  63. if(down2 < XVal){ //Checks for bottom of Maze
  64. SValue.push(new Position(down1,down2));
  65. }
  66. if (SValue.substring(down1,down2-1).equals(".")
  67. || SValue.substring(down1,down2-1).equals("$")) {
  68. Algo(down1,down2-1);
  69. }
  70. else {
  71. SValue.pop(Position(down1,down2));
  72. Left(down1, down2);
  73. }
  74. }
  75. /*
  76. * Method used to check whether you can go UP
  77. */
  78. public void Left(int left1, int left2) {
  79. SValue.push(new Position(left1,left2));
  80. if (SValue.substring(left1-1,left2).equals(".")
  81. || SValue.substring(left1-1,left2).equals("$")) {
  82. Algo(left1-1,left2);
  83. }
  84. else {
  85. SValue.pop(Position(left1,left2));
  86. Right(left1, left2);
  87. }
  88. }
  89. /*
  90. * Method used to check whether you can go Right
  91. */
  92. public void Right(int right1, int right2) {
  93. SValue.push(new Position(right1,right2));
  94. if (SValue.substring(right1+1,right2).equals(".")
  95. || SValue.substring(right1+1,right2).equals("$")) {
  96. Algo(right1+1,right2);
  97. }
  98. else {
  99. SValue.pop(Position(right1,right2));
  100. error(right1, right2);
  101. }
  102. }
  103. public void error(int e1, int e2) {
  104. System.out.println("HOUSTON WE HAVE AN ERROR!");
  105. }
  106. public boolean check(int done1, int done2) {
  107. boolean hasbeen = false;
  108. for( int a = 0; a < olmaize.size(); a++) {
  109. if(olmaize.get(a).equals(new Position (done1,done2))){
  110. hasbeen = true;
  111. break;
  112. }
  113. if(!hasbeen) {
  114. olmaize.add(new Position(done1, done2));
  115. return true;
  116. }
  117. }
  118. }
  119. public class Position{
  120. public int downL;
  121. public int upL;
  122. public Position (int downL, int upL) {
  123.  
  124. }
  125. }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement