public static int manhattanDistanceHeuristicPlusReversalPenalty(Node node) { int tileCount = 0; for (int i = 0; i < node.state.state.Count; i++) { for (int j = 0; j < node.state.state[i].Count; j++) { if (!node.state.state[i][j].val.Equals(goalState[i][j]) && !node.state.state[i][j].val.Equals("0")) { int[] goalStateIndex = determineGoalStateIndex(node.state.state[i][j].val); tileCount += (Math.Abs(goalStateIndex[0] - i)) + (Math.Abs(goalStateIndex[1] - j)); if (j < 2) { if ((node.state.state[i][j].val.Equals(goalState[i][j + 1]) && node.state.state[i][j + 1].val.Equals(goalState[i][j]))) { tileCount = tileCount + 2; } } if (i < 2) { if ((node.state.state[i][j].val.Equals(goalState[i + 1][j]) && node.state.state[i + 1][j].val.Equals(goalState[i][j]))) { tileCount = tileCount + 2; } } } } } return tileCount; }
public void printMoves(Node goal) { if (goal.parent != null) { printMoves(goal.parent); } results.Add(goal); }
public Node(State state, Node parent, String action) { this.state = state; this.parent = parent; this.action = action; gValue = GoalValue.value(this); hValue = (state != null) ? Heuristics.heuristic(this) : 0; fValue = gValue + hValue; }
public Node(State state) { this.state = state; this.parent = null; this.action = null; gValue = GoalValue.zeroGoalValue(this); hValue = (state != null) ? Heuristics.heuristic(this) : 0; fValue = gValue + hValue; }
public static int misplacedTileHeuristic(Node node) { int tileCount = 0; for (int i = 0; i < goalState.Length; i++) { for (int j = 0; j < goalState[i].Length; j++) { if (!node.state.state[i][j].val.Equals(goalState[i][j]) && !node.state.state[i][j].val.Equals("0")) { tileCount++; } } } return tileCount; }
public void setCurrentNode(Node newNode) { int count = 0; for (int i = 0; i < newNode.state.state.Count; i++) { for (int j = 0; j < newNode.state.state[i].Count; j++) { if (!newNode.state.state[i][j].val.Equals("0")) { ((Button)blockContainer.Children[count++]).DataContext = newNode.state.state[i][j]; } } } currentNode = newNode; }
public static int manhattanDistanceHeuristic(Node node) { int count = 0; int tileCount = 0; for (int i = 0; i < node.state.state.Count; i++) { for (int j = 0; j < node.state.state[i].Count; j++) { if (!node.state.state[i][j].val.Equals(goalState[i][j]) && !node.state.state[i][j].val.Equals("0")) { int[] goalStateIndex = determineGoalStateIndex(node.state.state[i][j].val); tileCount += (Math.Abs(goalStateIndex[0] - i)) + (Math.Abs(goalStateIndex[1] - j)); } count++; } } return tileCount; }
private String stringifyState(Node node) { String myState = ""; int count = 1; foreach (List<Square> s in node.state.state) { foreach (Square sq in s) { myState += sq.val + " "; if (count == 3) { myState += "\r"; count = 0; } count++; } } return myState; }
public static int zeroHeuristic(Node node) { return 0; }
private Boolean nodeCompare(Node node, Node node1) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (node.state.state[i][j].val != node1.state.state[i][j].val) { return false; } } } return true; }
private Boolean isItInSet(Node node, List<Node> set) { if (set.Count == 0) return false; foreach (Node n in set) { if (nodeCompare(n, node)) { return true; } } return false; }
private void initializePuzzleState() { List<List<Square>> values = new List<List<Square>>(); int count = 0; for(int i = 0; i < 3; i++) { List<Square> temp = new List<Square>(); for (int j = 0; j < 3; j++) { if (count > 7) { Square tempSquare = new Square(i, j, (0).ToString()); temp.Add(tempSquare); } else { Square tempSquare = new Square(i, j, (count + 1).ToString()); temp.Add(tempSquare); ((Button)blockContainer.Children[count++]).DataContext = tempSquare; } } values.Add(temp); } currentNode = new Node(new State(values, "")); initializeStats(); }
public static int zeroGoalValue(Node node) { return 0; }
public static int goalValue(Node node) { return node.parent.gValue + 1; }
public int CustomCompare(Node one, Node two) { return one.fValue.CompareTo(two.fValue); }
public void setAsRootNode() { this.parent = null; this.action = ""; this.gValue = 0; }