Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package nl.ru.ai.exercise5;
- import static nl.ru.ai.exercise5.Maze.*;
- import java.util.ArrayList;
- //Flauwrens de Bruin 1002199 && Roel van der Heijden 4822641
- //Added displayRoute() and showMap() because why not
- public class Exercise5
- {
- public static void main(String[] arguments)
- {
- ArrayList<Candidate> candidates=new ArrayList<Candidate>();
- solve(candidates,0);
- }
- /**
- * Uses breadth first search to find the shortest path from Miltank to Azumarill
- *
- * @param candidates
- * @param c
- */
- private static void solve(ArrayList<Candidate> candidates, int c)
- {
- assert candidates!=null:"ArrayList needs to be initialized";
- assert c>=0:"cannot be negative";
- candidates.add(new Candidate(new Attempt(1,7),0));
- boolean found=false;
- while(c<candidates.size()&!found)
- {
- int row=candidates.get(c).attempt.row;
- int col=candidates.get(c).attempt.col;
- if(hasRabbit(row,col))
- {
- showSolution(candidates,c);
- displayRoute(candidates,c);
- found=true;
- } else
- {
- checkPath(candidates,c,row-1,col);
- checkPath(candidates,c,row+1,col);
- checkPath(candidates,c,row,col-1);
- checkPath(candidates,c,row,col+1);
- c++;
- }
- }
- }
- /**
- * Checks if a path/direction is new and if it's valid
- * Adds a new/valid path to the candidates ArrayList
- *
- * @param candidates
- * @param c
- * @param row
- * @param col
- */
- private static void checkPath(ArrayList<Candidate> candidates, int c, int row, int col)
- {
- assert candidates!=null:"ArrayList needs to be initialized";
- assert c>=0:"cannot be negative";
- assert row>=0&&row<maze.length: "row out of bounds";
- assert col>=0&&col<maze[0].length: "column out of bounds";
- if(!hasWall(row,col)&&!hasVisited(row,col))
- candidates.add(new Candidate(new Attempt(row,col),c));
- visited(row,col);
- }
- /**
- * Shows the coordinates of a found route
- *
- * @param candidates
- * @param c
- */
- private static void showSolution(ArrayList<Candidate> candidates, int c)
- {
- assert candidates!=null:"ArrayList needs to be initialized";
- assert c>=0:"cannot be negative";
- int row=candidates.get(c).attempt.row;
- int col=candidates.get(c).attempt.col;
- if(c>0)
- showSolution(candidates,candidates.get(c).parentCandidate);
- System.out.println("row: "+row+" col: "+col);
- }
- /**
- * Adds the found route in the maze array
- * runs showMap
- *
- * @param candidates
- * @param c
- */
- private static void displayRoute(ArrayList<Candidate> candidates, int c)
- {
- assert candidates!=null:"ArrayList needs to be initialized";
- assert maze!=null:"array needs to be initialized";
- while(c>0)
- {
- int row=candidates.get(c).attempt.row;
- int col=candidates.get(c).attempt.col;
- maze[row][col]='o';
- c=candidates.get(c).parentCandidate;
- }
- showMap();
- }
- /**
- * Shows the map stored in the maze array
- */
- private static void showMap()
- {
- assert maze!=null:"array needs to be initialized";
- for(int i=0;i<maze.length;i++)
- {
- System.out.println();
- for(int j=0;j<maze[0].length;j++)
- {
- if(i==1&&j==1)
- System.out.print("R ");
- else if(i==1&&j==7)
- System.out.print("C ");
- else if(maze[i][j]=='x')
- System.out.print("[]");
- else if(maze[i][j]=='o')
- System.out.print("o ");
- else
- System.out.print(" ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement