// Finate State Machine import java.util.*; public class FSM implements Cloneable { private ArrayList nodes; private int playhead; public FSM(int numStates){ nodes = new ArrayList(); for (int i = 0; i 0.5), (int)(Math.random() * numStates-1), (int)(Math.random() * numStates-1)); addNode(n); } } // Mutating Methods public void addNode(FSMNode newNode){ nodes.add(newNode); playhead = 0; } public FSMNode getNode(int i){ if (i=0) return nodes.get(i); return null; } public FSM makeChild(){ FSM child = (FSM)this.clone(); for (int i = 0; i<5; i++){ int toChange = (int)(Math.random()*nodes.size()); int changeTo = (int)(Math.random()*nodes.size()); FSMNode oldNode = this.nodes.get(toChange); child.nodes.set(toChange, new FSMNode(oldNode.value, oldNode.trueNode, oldNode.falseNode)); if(Math.random() > 0.5) child.nodes.get(toChange).trueNode = changeTo; else child.nodes.get(toChange).falseNode = changeTo; } return child; } public Object clone(){ try { FSM cloned = (FSM)super.clone(); return cloned; } catch (CloneNotSupportedException e) {return null; } } // Usage Methods public boolean doStep(boolean input){ if (input) playhead = nodes.get(playhead).trueNode; else playhead = nodes.get(playhead).falseNode; return nodes.get(playhead).value; } // Getter Methods public int numNodes(){ return nodes.size(); } public String toString(){ String ret = ""; ret += "{"; for(int i = 0; i < nodes.size(); i++){ ret += nodes.get(i) + ", "; } ret +="}"; return ret; } }