/* The method solve searches for a solution. The Printwriter argument is used to specify where the output should be directed. NOTE: The editing was done in the GameState class. I changed it to allow me to pass back an array of gamestates that were in the correct format and they had already been formatted to remove illegal moves. */ public void solve( PrintWriter output) { // takes the output format as an arguement, so that it can be output to the // textfile. unexpanded.add(rootNode); // adds the current rootnode Node object to the unexpanded arraylist. while (unexpanded.size() > 0) { // loop whilst the arraylist holding the unexpanded nodes is populated Node n = unexpanded.get(0); // Node object n equals the first member of the unexpanded arraylist expanded.add(n); // adds the node n to the exppanded arraylist unexpanded.remove( 0); // removes the current node n, which is also the first object,from the unexpanded // arraylist if (n.state.isGoal()) { // if the node n matches the goal state, (if the boards are the same) reportSolution( n, output); // the solution is reported using the reportsolution method where the node and // output are supplied return; // breaks the while loop as the solution has been found } else { // otherwise ArrayList<GameState> moveList = n.state .possibleMoves(); // move list array initiated and populated with the possible moves // from state n for (GameState gs : moveList) { // for each state in possible moves, use the temporary GameState var gs if ((Node.findNodeWithState(unexpanded, gs) == null) && // if the current gamestate is not in the unexpanded arraylist and... (Node.findNodeWithState(expanded, gs) == null)) { // the current gamestate is not in the expanded arraylist (nothing is // returned from the function findNodeWithState) int newCost = n.getCost() + 1; // returns the cost of exploring that node, and saves is as int newCost Node newNode = new Node( gs, n, newCost); // newNode is creates as type Node, which consists of the gamestate // from moveList, the unexpanded node and the cost of exploration unexpanded.add(newNode); // this newNode is added to the unexpanded list } } } } output.println("No solution found"); // outputs no solution found if nothing is found. }