Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. package nl.ru.ai.exercise5;
  2.  
  3. import static nl.ru.ai.exercise5.Maze.*;
  4.  
  5. import java.util.ArrayList;
  6.  
  7. //Flauwrens de Bruin 1002199 && Roel van der Heijden 4822641
  8. //Added displayRoute() and showMap() because why not
  9. public class Exercise5
  10. {
  11. public static void main(String[] arguments)
  12. {
  13. ArrayList<Candidate> candidates=new ArrayList<Candidate>();
  14. solve(candidates,0);
  15. }
  16.  
  17. /**
  18. * Uses breadth first search to find the shortest path from Miltank to Azumarill
  19. *
  20. * @param candidates
  21. * @param c
  22. */
  23. private static void solve(ArrayList<Candidate> candidates, int c)
  24. {
  25. assert candidates!=null:"ArrayList needs to be initialized";
  26. assert c>=0:"cannot be negative";
  27. candidates.add(new Candidate(new Attempt(1,7),0));
  28. boolean found=false;
  29. while(c<candidates.size()&!found)
  30. {
  31. int row=candidates.get(c).attempt.row;
  32. int col=candidates.get(c).attempt.col;
  33. if(hasRabbit(row,col))
  34. {
  35. showSolution(candidates,c);
  36. displayRoute(candidates,c);
  37. found=true;
  38. } else
  39. {
  40. checkPath(candidates,c,row-1,col);
  41. checkPath(candidates,c,row+1,col);
  42. checkPath(candidates,c,row,col-1);
  43. checkPath(candidates,c,row,col+1);
  44. c++;
  45. }
  46. }
  47. }
  48.  
  49. /**
  50. * Checks if a path/direction is new and if it's valid
  51. * Adds a new/valid path to the candidates ArrayList
  52. *
  53. * @param candidates
  54. * @param c
  55. * @param row
  56. * @param col
  57. */
  58. private static void checkPath(ArrayList<Candidate> candidates, int c, int row, int col)
  59. {
  60. assert candidates!=null:"ArrayList needs to be initialized";
  61. assert c>=0:"cannot be negative";
  62. assert row>=0&&row<maze.length: "row out of bounds";
  63. assert col>=0&&col<maze[0].length: "column out of bounds";
  64.  
  65. if(!hasWall(row,col)&&!hasVisited(row,col))
  66. candidates.add(new Candidate(new Attempt(row,col),c));
  67. visited(row,col);
  68. }
  69.  
  70. /**
  71. * Shows the coordinates of a found route
  72. *
  73. * @param candidates
  74. * @param c
  75. */
  76. private static void showSolution(ArrayList<Candidate> candidates, int c)
  77. {
  78. assert candidates!=null:"ArrayList needs to be initialized";
  79. assert c>=0:"cannot be negative";
  80. int row=candidates.get(c).attempt.row;
  81. int col=candidates.get(c).attempt.col;
  82. if(c>0)
  83. showSolution(candidates,candidates.get(c).parentCandidate);
  84. System.out.println("row: "+row+" col: "+col);
  85.  
  86. }
  87.  
  88. /**
  89. * Adds the found route in the maze array
  90. * runs showMap
  91. *
  92. * @param candidates
  93. * @param c
  94. */
  95. private static void displayRoute(ArrayList<Candidate> candidates, int c)
  96. {
  97. assert candidates!=null:"ArrayList needs to be initialized";
  98. assert maze!=null:"array needs to be initialized";
  99. while(c>0)
  100. {
  101. int row=candidates.get(c).attempt.row;
  102. int col=candidates.get(c).attempt.col;
  103. maze[row][col]='o';
  104. c=candidates.get(c).parentCandidate;
  105. }
  106. showMap();
  107. }
  108.  
  109. /**
  110. * Shows the map stored in the maze array
  111. */
  112. private static void showMap()
  113. {
  114. assert maze!=null:"array needs to be initialized";
  115. for(int i=0;i<maze.length;i++)
  116. {
  117. System.out.println();
  118. for(int j=0;j<maze[0].length;j++)
  119. {
  120. if(i==1&&j==1)
  121. System.out.print("R ");
  122. else if(i==1&&j==7)
  123. System.out.print("C ");
  124. else if(maze[i][j]=='x')
  125. System.out.print("[]");
  126. else if(maze[i][j]=='o')
  127. System.out.print("o ");
  128. else
  129. System.out.print(" ");
  130. }
  131. }
  132. }
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement