Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. public class SolveMaze
  2. {
  3. public static void main(String[] args)
  4. {
  5. Maze m=new Maze();
  6.  
  7. char facingeast;
  8. char facingnorth;
  9. char facingsouth;
  10. char facingwest;
  11. char CurrentDirectionFacing=facingeast;
  12.  
  13. while(m.goalIsNotReached()) // continue until goal is reached
  14.  
  15. {
  16.  
  17. while (CurrentDirectionFacing==facingnorth)
  18. {
  19. {
  20. if(m.wallPresent(Maze.EAST)) //if there is a wall to right check wall in front
  21. {
  22. m.wallPresent(Maze.NORTH); // check for wall in front
  23. }
  24. else
  25. {
  26. m.moveEast(); // no wall to the right.. move right
  27. CurrentDirectionFacing=facingeast;
  28. break;
  29. }
  30. }
  31.  
  32. {
  33. if(m.wallPresent(Maze.NORTH)) // check if wall in front
  34. {
  35. m.wallPresent(Maze.WEST); // wall in front. check wall to left
  36. }
  37. else
  38. {
  39. m.moveNorth(); // no wall in front move forward
  40. CurrentDirectionFacing=facingnorth;
  41. break;
  42. }
  43. }
  44.  
  45.  
  46. {
  47. if(m.wallPresent(Maze.WEST)) // check wall to left
  48. {
  49. m.moveSouth(); // wall to left means dead end, turn around go other direction
  50. CurrentDirectionFacing=facingsouth;
  51. break;
  52. }
  53. else
  54. {
  55. m.moveWest();
  56. CurrentDirectionFacing=facingwest;
  57. break;
  58. }
  59. }
  60.  
  61. }
  62.  
  63.  
  64. while (CurrentDirectionFacing==facingeast)
  65. {
  66. {
  67. if(m.wallPresent(Maze.SOUTH)) //if there is a wall to right check wall in front
  68. {
  69. m.wallPresent(Maze.EAST); // check for wall in front
  70. }
  71. else
  72. {
  73. m.moveSouth(); // no wall to the right.. move right
  74. CurrentDirectionFacing=facingsouth;
  75. break;
  76. }
  77. }
  78.  
  79. {
  80. if(m.wallPresent(Maze.EAST)) // check if wall in front
  81. {
  82. m.wallPresent(Maze.NORTH); // wall in front. check wall to left
  83. }
  84. else
  85. {
  86. m.moveEast(); // no wall in front move forward
  87. CurrentDirectionFacing=facingeast;
  88. break;
  89. }
  90. }
  91.  
  92.  
  93. {
  94. if(m.wallPresent(Maze.NORTH)) // check wall to left
  95. {
  96. m.moveWest(); // wall to left means dead end, turn around go other direction
  97. CurrentDirectionFacing=facingwest;
  98. break;
  99. }
  100. else
  101. {
  102. m.moveNorth();
  103. CurrentDirectionFacing=facingnorth;
  104. break;
  105. }
  106. }
  107.  
  108. }
  109.  
  110.  
  111. while (CurrentDirectionFacing==facingsouth)
  112. {
  113. {
  114. if(m.wallPresent(Maze.WEST)) //if there is a wall to right check wall in front
  115. {
  116. m.wallPresent(Maze.SOUTH); // check for wall in front
  117. }
  118. else
  119. {
  120. m.moveWest(); // no wall to the right.. move right
  121. CurrentDirectionFacing=facingwest;
  122. break;
  123. }
  124. }
  125.  
  126. {
  127. if(m.wallPresent(Maze.SOUTH)) // check if wall in front
  128. {
  129. m.wallPresent(Maze.EAST); // wall in front. check wall to left
  130. }
  131. else
  132. {
  133. m.moveSouth(); // no wall in front move forward
  134. CurrentDirectionFacing=facingsouth;
  135. break;
  136. }
  137. }
  138.  
  139.  
  140. {
  141. if(m.wallPresent(Maze.EAST)) // check wall to left
  142. {
  143. m.moveEast(); // wall to left means dead end, turn around go other direction
  144. CurrentDirectionFacing=facingeast;
  145. break;
  146. }
  147. else
  148. {
  149. m.moveEast();
  150. CurrentDirectionFacing=facingeast;
  151. break;
  152. }
  153. }
  154.  
  155. }
  156.  
  157.  
  158. while (CurrentDirectionFacing==facingwest)
  159. {
  160. {
  161. if(m.wallPresent(Maze.NORTH)) //if there is a wall to right check wall in front
  162. {
  163. m.wallPresent(Maze.WEST); // check for wall in front
  164. }
  165. else
  166. {
  167. m.moveNorth(); // no wall to the right.. move right
  168. CurrentDirectionFacing=facingnorth;
  169. break;
  170. }
  171. }
  172.  
  173. {
  174. if(m.wallPresent(Maze.WEST)) // check if wall in front
  175. {
  176. m.wallPresent(Maze.SOUTH); // wall in front. check wall to left
  177. }
  178. else
  179. {
  180. m.moveWest(); // no wall in front move forward
  181. CurrentDirectionFacing=facingwest;
  182. break;
  183. }
  184. }
  185.  
  186.  
  187. {
  188. if(m.wallPresent(Maze.SOUTH)) // check wall to left
  189. {
  190. m.moveEast(); // wall to left means dead end, turn around go other direction
  191. CurrentDirectionFacing=facingeast;
  192. break;
  193. }
  194. else
  195. {
  196. m.moveSouth();
  197. CurrentDirectionFacing=facingsouth;
  198. break;
  199. }
  200. }
  201.  
  202. }
  203.  
  204.  
  205. }
  206.  
  207.  
  208.  
  209. }
  210.  
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement