/**
 * HW1 Fall 2010
 * AI CSC 440
 * SearchSolver main method
 */

import java.util.*; 
 
public class SearchSolver {

    // A main tester method to show you it works.  
    // Here is where you implement the Search algorith discussed in class 
    // with open and closed lists, etc.
    public static void main(String[] args) {

        // Replace CupsNode with your own SearchNode implementation
        SearchNode initial = new CupsNode(args);

        // Create the open list of nodes to be searched
        ArrayList<SearchNode> open = new ArrayList<SearchNode>();
        open.add(initial);
        
        // Create the closed list of nodes already searched
        ArrayList<SearchNode> closed = new ArrayList<SearchNode>();

        SearchNode current = null;
        boolean found = false;
        int count = 0;
        
        // While there are still nodes on the open list and
        // we have not found a goal node, keep searching
        while (open.size() > 0 && !found) {

            // Take the a node from the open list, place 
            // it on the closed list
            current = open.remove(0);
            closed.add(current);
            count++;

            // When you see a goal, stop
            if (current.isGoal()) {
                found = true;
            } else {

                // Otherwise, find the children
                SearchNode[] kids = current.generateChildren();
                for (int i = 0; i < kids.length; i++) {
    
                    // Put them on the open list if they are new
                    if (!(open.contains(kids[i]) || closed.contains(kids[i]))) {
                        open.add(kids[i]);
                    }
                }
            }
        }
        
        // Display the results to the user
        System.out.println("Searched through " + count + " solutions.");
        if (found) {
            System.out.println("Solution Found!!!");
            current.printPath();
        } else {
            System.out.println("No solution found, sorry");
        }
    }
}