/// <summary> /// Records a step taken in the maze. /// </summary> /// <param name="coord">Current coordinate.</param> public void RecordStep(MazeCoordinate coord) { if (lastStep == null || !lastStep.Equals(coord)) { steps.Enqueue(coord); lastStep = coord; } }
public void TestCoordinates() { MazeCoordinate coord1 = new MazeCoordinate(1, 1); MazeCoordinate coord2 = new MazeCoordinate(1, 1); MazeCoordinate coord3 = new MazeCoordinate(2, 2); Assert.AreEqual(coord1, coord2); Assert.AreNotEqual(coord1, coord3); }
/// <summary> /// Recursively solves the maze. /// </summary> /// <param name="m">Maze to solve.</param> /// <param name="coord">Current position.</param> /// <returns>Coordinate with finish or null if the algorithm dead-ended.</returns> private MazeCoordinate RecursiveSolve(Maze m, MazeCoordinate coord) { // Indicate we've visited this position. m.GetPosition(coord).Visited = true; //...and record our step. formatter.RecordStep(coord); // If we're finished, return this coordinate. if (m.GetPosition(coord).Artifact == MazeArtifact.Finish) { return coord; } MazeCoordinate nextCoord = null; // Start looking around. for (Direction dir = Direction.Up; dir <= Direction.Down; dir++) { MazePosition next = m.PeekToDirection(coord, dir); // Can we go towards that direction? if (TryNext(next)) { // If yes, try to solve from there. nextCoord = RecursiveSolve(m, next.Coordinate); if (nextCoord != null) { // Solution reached. return nextCoord; } // Backtrack. formatter.RecordStep(coord); } } return null; }
/// <summary> /// Creates a new instance of this class. /// </summary> /// <param name="artifact">Artifact of position.</param> /// <param name="coord">Coordinates of position.</param> public MazePosition(MazeArtifact artifact, MazeCoordinate coord) { this.Artifact = artifact; this.Coordinate = coord; Visited = false; }