/**
*	Node in a state-space search
*   variable costs case
*   Must implement goalP, get_Successors, same_State, node_toString
*   node has local cost & global cost now
*/


import java.util.*;

public  class Search_Node {
  
  private Search_State state;
  //change from search2
  private int Cost;
  private int globalCost;
  private int localCost;
  
  //change from search1
  private Search_Node parent; // the parent node

  
  /**
  * constructor
  * @param s a Search_State
  * @param lc local cost of getting to this node from its predecessor
  */

  public Search_Node(Search_State s, int lc){
    state= (Search_State) s;
    localCost=lc;  //change from search2
  }

 /**
  * accessor for state
  */

  public Search_State get_State(){
    return state;
  }
  
   /**
  * accessor for parent
  */
  
  public Search_Node get_Parent(){
    return parent;
  }
  
  /**
  * mutator for parent
  */
  public void set_Parent(Search_Node n){
    parent=n;
  }
  
  /**
  * mutator for localcost
  *
  */
  
  public void setlocalCost(int lc){
    localCost=lc;
  }
  
  /**
  * mutator for localcost
  *
  */
  
  public int getlocalCost(){
    return localCost;
  }

  /**
  * mutator for localcost
  *
  */
  public void setglobalCost(int lc){
    globalCost=lc;
  }
  
  /**
  * acccessor for globalcost
  *
  */
  public int getglobalCost(){
    return globalCost;
  }
  
  // must implement goalP, get_Successors, same_State, node_toString
    
  /** 
  * goalP takes a Search_Node & returns true if it's a goal
  * @param searcher the current search
  */
   
  public  boolean goalP(Search searcher){
    return state.goalP(searcher);
  }

  /**
  * get_Successors for this node
  * @param searcher the current search
  * @return ArrayList of successor nodes
  */
  
  public ArrayList get_Successors(Search searcher){
    ArrayList slis = state.get_Successors(searcher);
    ArrayList nlis= new ArrayList();
    for (Iterator it = slis.iterator();it.hasNext();){
      Search_State suc_state = (Search_State) it.next();
      //change from search2
      Search_Node n = new Search_Node(suc_state, suc_state.getlocalCost());
      nlis.add(n);
    }
    return nlis;
  }
   
  /**
  * same_State - does another node have same state as this one?
  * @param n2 the other node
  */
     
  public boolean same_State(Search_Node n2){
    return state.same_State(n2.get_State());
  }
    
  public  String toString(){
  	String parent_state;
  	if (parent==null) parent_state="null"; else parent_state=parent.get_State().toString();
    return "node with state ("+state.toString()+") parent state ("+parent_state+") local cost ("+localCost+") global cost ("+globalCost+")";
  }
    
    
}
