Ejemplo n.º 1
0
 /// <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;
     }
 }
Ejemplo n.º 2
0
 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);
 }
Ejemplo n.º 3
0
        /// <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;
        }
Ejemplo n.º 4
0
 /// <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;
 }