// The Chain Problem package ai.search; import java.util.Vector; import java.awt.*; import javax.swing.*; public class ChainProblem extends Problem { ChainProblem() { super(null); initialState = initialChainState(); } public boolean goalReached(Object state) { ChainState cs = (ChainState)state; if ((cs.openLinks==0) && (cs.chains.size()==1)) { Chain c = (Chain)cs.chains.elementAt(0); return (c.numLinks==12 && c.loop); } return false; } // Return a list of StateActionPairs. The action can be either // "close straight" or "close loop" followed by the chain number, or // "open" followed by the chain number and the link number or // "join" followed by the two chain numbers. public Vector successors(Object state) { Vector succ = new Vector(); // Complete the code here return succ; } public boolean equalState(Object state1, Object state2) { ChainState cs1 = (ChainState)state1; ChainState cs2 = (ChainState)state2; if (cs1.openLinks==cs2.openLinks && cs1.chains.size()==cs2.chains.size()) { for (int i=0; i 1) if (linkNum==0 || linkNum==c1.numLinks-1 || c1.loop) insertChain(newcs, new Chain(c1.numLinks-1,false)); else { // result is two chains insertChain(newcs, new Chain(linkNum,false)); insertChain(newcs, new Chain(c1.numLinks-linkNum-1,false)); } return newcs; } // Return the state resulting from closing a link on // the specified chain ChainState close(ChainState cs, int chainNum, boolean loop) { // Create a new ChainState without that chain ChainState newcs = new ChainState(); newcs.openLinks = cs.openLinks - 1; for (int i=0; i c1.numLinks){ cs.chains.insertElementAt(c,i); return; } } cs.chains.insertElementAt(c,cs.chains.size()); } ChainState initialChainState() { ChainState cs = new ChainState(); cs.chains.addElement(new Chain(3,false)); cs.chains.addElement(new Chain(3,false)); cs.chains.addElement(new Chain(3,false)); cs.chains.addElement(new Chain(3,false)); return cs; } } // The state says how many open links there are and gives a list of chains class ChainState { int openLinks; Vector chains; ChainState() { openLinks = 0; chains = new Vector(); } } // A chain consists of a number of links plus a boolean stating whether linked // into a loop or straight. class Chain { int numLinks; boolean loop; Chain(int n, boolean lp) { numLinks = n; loop = lp; } }