/* * Created on Mar 31, 2004 * */ package eightPuzzle; import java.util.Hashtable; /** * @author kgajos * * Data structure to keep track of previously visited nodes */ public class ClosedList { // storing the nodes; the mapping is from boards to boards // -- it is weird but it was the fastest way to not only check // if an element exists in the closed list but also pull out // the old instance protected Hashtable nodes = new Hashtable(); /** * Creates an instance of the ClosedList */ public ClosedList() {} public void add(Board board) { nodes.put(board, board); } /** * Checks if the closed list contains a board that looks just like * the one passed as an argument * * @param board reference board * @return true if an identical board exists; false otherwise */ public boolean contains(Board board) { return nodes.contains(board); } /** * returns the cost estimate for the stored instance of this * board configuration * * @param board a board configuration to be checked * @return the stored cost estimate or -1 if the board state * is not in the closed list */ public int getCostFor(Board board) { if (contains(board)) return ((Board)nodes.get(board)).getCostEstimate(); return -1; } /** * Removes the board (or one that looks like it) from the closed * list * * @param board board to be removed */ public void remove(Board board) { nodes.remove(board); } }