MapNode Code



import java.util.Vector;

/**
 * MapNode.java - a modification of the uninformed SearchNode
 * created by Todd Neller.
 *
 * @author Jason Bindewald
 * @version 1.0
 *

Copyright (C) 2004 Jason Bindewald

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Information about the GNU General Public License is available online at:
  http://www.gnu.org/licenses/
To receive a copy of the GNU General Public License, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.

 */
public class MapNode {
    
    /**
     * variable depth - search depth; 0 at the root
     * search node; a child node has its parent node depth + 1. */
    public int depth = 0;
  
    /**
     * variable parent - parent search node; null if and
     * only if node is the root of the search tree.  */
    public MapNode parent = null;
    
    /**
     * variable currentState - The intersection at which
     * the searcher currently resides.  Initialized to a non-existant
     * state of -1. */
    public int currentState = -1;
    
    /**
     * variable goalState - The intersection that the 
     * searcher is attempting to reach.  Initialized to a non-
     * existant state of -1.*/
    public int goalState    = -1;
    
    /**
     * variable map - a 2D int array keeping track of 
     * all of the intersections and their adjacent intersections. */
    public int[][] map;
    
    /**
     * Creates a MapNode instance and sets it to an 
     * initial search state. */
    public MapNode(int _start, int[][] _map, int _goal) {
	currentState = _start;
	map = _map;
	goalState = _goal;
    }

    /**
     * isGoal - test whether or not the current node
     * is a goal node.
     *
     * @return a boolean value */
    public boolean isGoal() {
	return currentState == goalState;
    }


    /**
     * expand - return a Vector of this node's 
     * children.
     *
     * @return a Vector of SearchNodes */
    public Vector expand() {
	Vector children = new Vector();
	
	//For every adjacent intersection that exists (i.e. is
	//not = -1) create a child with it as the current state
	//and add that child tothe children vector
	for(int i = 0; i < map[currentState].length; i++) {
	    if(map[currentState][i] != -1) {
		MapNode child = (MapNode) this.childClone();
		child.currentState = map[currentState][i];
		children.addElement(child);
	    }
	}
	return children;
    }

    /**
     * childClone - returns a clone of this node that has
     * been made a child of this node and has a depth one greater than
     * this.
     *
     * @return a MapNode value */
    public MapNode childClone() {
	MapNode child = (MapNode) clone();
	child.parent = this;
	child.depth = depth + 1;
	return child;
    }


    /**
     * clone - performs a DEEP clone (copy) of this
     * node.  That is, any change to the original/cloned node should
     * have no side-effect on the other.  
     *
     * @return an Object value */
    public Object clone() {
	MapNode mn = new MapNode(currentState, map, goalState);
	mn.parent = parent;
	mn.depth = depth;
	return mn;
    }

    /**
     * equals - whether or not two MapNode objects
     * have the same state.
     *
     * @param o an Object value - object to test for
     * equality with this
     * @return a boolean value - whether or not two
     * objects are equal */
    public boolean equals(Object o) {
	return ((o instanceof MapNode)
		&& ((MapNode)o).currentState == currentState
		&& ((MapNode)o).goalState == goalState);
    }


    /**
     * toString - string representation of state
     * 
     * @return a String value */
    public String toString() {
	return "" + currentState;
    } 
}// MapNode