public CantMoveThatWayException(PuzzleNode source, Direction aDirection) { //The puzzle in Source tried to move in the direction aDirection. //This is an illegal move (It put a tile off the edge of the puzzle!) Source = source; Direction = aDirection; }
/// <summary> /// Moving up moves the empty cell up (and the cell above it down) /// first, create the new one (the one to return) /// now, execute the changes: move the blank cell aDirection /// find the blankCell /// </summary> /// <param name="OriginalNode"></param> /// <param name="aDirection"></param> /// <returns></returns> public static PuzzleNode Move(PuzzleNode OriginalNode, Direction aDirection) { int[,] result = Clone.CloneMultiArray(OriginalNode.CurrentState); int[] blankCell = OriginalNode.BlankCell; switch (aDirection) { case Direction.Up: result[blankCell[0], blankCell[1]] = result[blankCell[0], blankCell[1] - 1]; result[blankCell[0], blankCell[1] - 1] = 0; break; case Direction.Down: result[blankCell[0], blankCell[1]] = result[blankCell[0], blankCell[1] + 1]; result[blankCell[0], blankCell[1] + 1] = 0; break; case Direction.Left: result[blankCell[0], blankCell[1]] = result[blankCell[0] - 1, blankCell[1]]; result[blankCell[0] - 1, blankCell[1]] = 0; break; case Direction.Right: result[blankCell[0], blankCell[1]] = result[blankCell[0] + 1, blankCell[1]]; result[blankCell[0] + 1, blankCell[1]] = 0; break; default: throw new CantMoveThatWayException(OriginalNode, aDirection); } return new PuzzleNode(result, aDirection); }
public RDFSStrategy(PuzzleNode goalNode) : base() { code = "RDFS"; longName = "Recursive Depth-First Search"; ClosedList = new HashSet<node>(); GoalNode = goalNode; v = new TreeVisitor(); }
public BFSStrategy(PuzzleNode goalNode) : base() { code = "BFS"; longName = "Breadth-First Search"; Frontier = new Queue<node>(); ClosedList = new HashSet<node>(); GoalNode = goalNode; v = new TreeVisitor(); }
public AStar(PuzzleNode goalNode) { code = "AS"; longName = "A* Search"; longName = "Greedy Best-First Search"; Frontier = new ConcurrentPriorityQueue<node, int>(); ClosedList = new HashSet<node>(); GoalNode = goalNode; v = new TreeVisitor(); }
public InvalidPuzzleException(PuzzleNode aState) { //This puzzle is invalid for some reason theState = aState; }
//Manhatten Distance public int HeuristicCalculate(PuzzleNode Goal, node current) { int result = 0; var goalNode = Goal as PuzzleNode; var xLength = current.Value.CurrentState.GetLength(0); var yLength = current.Value.CurrentState.GetLength(1); int numberOfTiles = (xLength * yLength); int c = 0; for (int i = 0; i < xLength; i++) { for (int j = 0; j < yLength; j++) { int currentNumber = goalNode.CurrentState[i,j]; int currentIndex = FindTileCurrentIndex(currentNumber, current); result += GetDistanceToGoalTileForIndex(result, c, currentIndex, numberOfTiles, xLength); c++; } } return result; }