static void Main(string[] args) { DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); Maze maze = new Maze(di.Parent.Parent.FullName + @"\trickMaze.maz"); PathFinder pf = new PathFinder(); pf.Moved += DrawOnMove; pf.Solve(maze); }
public bool Solve(Maze theMaze) { InitVisitedList(theMaze); this.CurrentTile = theMaze.Start; this._Exit = IKnowExitCoordinates ? theMaze.Finish : null; this.Move(theMaze); return true; }
/// <summary> /// Move to an ajacent tile following this priority: /// - You have no other option /// - You have visited it less than another option /// - It is closer to the exit than an equally visited option /// - Randomly /// </summary> /// <param name="theMaze"></param> /// <returns></returns> private MazeTile ChooseNextLocation(Maze theMaze) { var nextTile = new MazeTile(); var choices = this.LookAround(theMaze); choices.RemoveAll(c => c.isWall); //Stuck - return an invalid tile to kill the maze. if (choices.Count == 0) { return nextTile; } //Dead End - kill this tile if (choices.Count == 1) { CurrentTile.isWall = true; return choices[0]; } FilterByLeastVisited(choices); FilterByLeastDistance(choices); nextTile = ChooseRandom(choices); return nextTile; }
private void Move(Maze theMaze) { //From Current Location MazeTile Next = this.ChooseNextLocation(theMaze); if (!this.MoveToNextLocation(Next)) { throw new Exception("No path exists between start and finish."); } if (Moved != null){ Moved(this, new EventArgs()); } if(!CurrentTile.Equals(theMaze.Finish)){ this.Move(theMaze); } }
/// <summary> /// Get the ajacent tiles of the current location /// </summary> /// <param name="theMaze"></param> /// <returns>List of MazeTiles</returns> private List<MazeTile> LookAround(Maze theMaze) { var North = theMaze.GetTile(CurrentTile.X, CurrentTile.Y + (int)Direction.North); var South = theMaze.GetTile(CurrentTile.X, CurrentTile.Y + (int)Direction.South); var East = theMaze.GetTile(CurrentTile.X + (int)Direction.East, CurrentTile.Y); var West = theMaze.GetTile(CurrentTile.X + (int)Direction.West, CurrentTile.Y); return new List<MazeTile>() { North, South, East, West }; }
private void InitVisitedList(Maze theMaze) { foreach (MazeTile mt in theMaze.Layout) { _Visited.Add(mt, 0); } }
public PathFinderTests() { DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); this.Maze = new Maze(di.Parent.Parent.FullName + @"\trickMaze.maz"); Assert.IsNotNull(this.Maze); }
public void Create_Maze_From_File() { DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory); Maze maze = new Maze(di.Parent.Parent.FullName + @"\testMaze.maz"); Assert.IsNotNull(maze); }