Пример #1
0
        private void ParseMazeFile(string mazeFilePath)
        {
            StreamReader sr   = new StreamReader(mazeFilePath);
            int          x    = 0;
            int          y    = 0;
            string       line = string.Empty;

            while ((line = sr.ReadLine()) != null)
            {
                foreach (char c in line)
                {
                    Debug.Write(c);
                    MazeTile mt = new MazeTile(x, y, Maze.ParseTile("wall", c));
                    if (Maze.ParseTile("start", c))
                    {
                        Start = mt;
                    }
                    if (Maze.ParseTile("finish", c))
                    {
                        Finish = mt;
                    }
                    Layout.Add(mt);
                    x++;
                }
                y++;
                x = 0;
                Debug.Write("\n");
            }
        }
Пример #2
0
        public MazeTile GetTile(int X, int Y)
        {
            MazeTile mt = new MazeTile();

            mt = Layout.Single(t => t.X == X && t.Y == Y);
            return(mt);
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        public bool Solve(Maze theMaze)
        {
            InitVisitedList(theMaze);

            this.CurrentTile = theMaze.Start;
            this._Exit = IKnowExitCoordinates ? theMaze.Finish : null;
            this.Move(theMaze);

            return true;
        }
Пример #5
0
        public bool Solve(Maze theMaze)
        {
            InitVisitedList(theMaze);

            this.CurrentTile = theMaze.Start;
            this._Exit       = IKnowExitCoordinates ? theMaze.Finish : null;
            this.Move(theMaze);

            return(true);
        }
Пример #6
0
 private bool MoveToNextLocation(MazeTile Next)
 {
     if (!Next.isValid)
     {
         return(false);
     }
     PreviousTile = CurrentTile;
     CurrentTile  = Next;
     _Visited[CurrentTile]++;
     return(true);
 }
Пример #7
0
        public Maze(string mazeFilePath)
        {
            Layout = new List <MazeTile>();
            Start  = new MazeTile();
            Finish = new MazeTile();

            ParseMazeFile(mazeFilePath);

            if (!this.Start.isValid || !this.Finish.isValid)
            {
                throw new Exception("Maze must contain a Start and Finish tile.");
            }
        }
Пример #8
0
        public Maze(string mazeFilePath)
        {
            Layout = new List<MazeTile>();
            Start = new MazeTile();
            Finish = new MazeTile();

            ParseMazeFile(mazeFilePath);

            if (!this.Start.isValid || !this.Finish.isValid )
            {
                throw new Exception("Maze must contain a Start and Finish tile.");
            }
        }
Пример #9
0
        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);
            }
        }
Пример #10
0
        /// <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;
        }
Пример #11
0
        private void ParseMazeFile(string mazeFilePath)
        {
            StreamReader sr = new StreamReader(mazeFilePath);
            int x = 0;
            int y = 0;
            string line = string.Empty;

            while ((line = sr.ReadLine()) != null)
            {
                foreach (char c in line)
                {
                    Debug.Write(c);
                    MazeTile mt = new MazeTile(x, y, Maze.ParseTile("wall", c));
                    if (Maze.ParseTile("start", c)) { Start = mt; }
                    if (Maze.ParseTile("finish", c)) { Finish = mt; }
                    Layout.Add(mt);
                    x++;
                }
                y++;
                x = 0;
                Debug.Write("\n");
            }
        }
Пример #12
0
 private bool MoveToNextLocation(MazeTile Next)
 {
     if (!Next.isValid) { return false; }
     PreviousTile = CurrentTile;
     CurrentTile = Next;
     _Visited[CurrentTile]++;
     return true;
 }
Пример #13
0
 public MazeTile GetTile(int X, int Y)
 {
     MazeTile mt = new MazeTile();
     mt = Layout.Single(t => t.X == X && t.Y == Y);
     return mt;
 }