public string Dump(MazeGrid maze, Point currentPosition = null)
        {
            var sb = new StringBuilder();

            for (int rowIndex = 0; rowIndex < maze.Grid.Length; rowIndex++)
            {
                var row = maze.Grid[rowIndex];

                for (int columnIndex = 0; columnIndex < row.Length; columnIndex++)
                {
                    string pointMarker = row[columnIndex] ? "." : "#";
                    if (maze.StartPosition.X == columnIndex && maze.StartPosition.Y == rowIndex)
                    {
                        pointMarker = "S";
                    }
                    if (maze.Finish.X == columnIndex && maze.Finish.Y == rowIndex)
                    {
                        pointMarker = "F";
                    }
                    if (currentPosition?.X == columnIndex && currentPosition.Y == rowIndex)
                    {
                        pointMarker = "O";
                    }
                    sb.Append(pointMarker);
                }

                sb.AppendLine();
            }

            return(sb.ToString());
        }
Beispiel #2
0
 public MazeWalker(MazeGrid mazeGrid, IWalkerStateDumper stateDumper)
 {
     _mMazeGrid      = mazeGrid;
     _stateDumper    = stateDumper;
     CurrentPosition = _mMazeGrid.StartPosition;
     Direction       = Orientation.South;
 }
Beispiel #3
0
        private void Run(string mazeFilePath)
        {
//            MazeGrid maze = GetMaze(mazeFilePath);
//            MazeGrid maze = GetMaze2(mazeFilePath);
            MazeGrid maze = GetMaze3(mazeFilePath);

            IWalkerStateDumper dumper = new BlandWalkerStateDumper();
//            IWalkerStateDumper dumper = new FancyWalkerStateDumper(MazeVisualDumper, maze);

            var entity = new MazeWalker(maze, dumper);

            Console.Write(MazeVisualDumper.Dump(maze));

//            bool mazeSolved = MazeSolver.SolveMaze(entity, maze.Finish);
            // TODO: Delete
            bool mazeSolved = AdvancedMazeSolver.SolveMaze(new MazeWalkerAdapter(entity), maze.Finish);

            Console.WriteLine(mazeSolved ? "Reached end of maze! :)" : "Failed to reach end of maze. :(");
            Console.ReadKey();
        }
Beispiel #4
0
        private void Run(string mazeFilePath)
        {
            var   lines  = new StreamReader(new FileStream(mazeFilePath, FileMode.Open)).ReadToEnd().Replace(" ", "").Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            Point start  = null;
            Point finish = null;

            var grid       = new bool[lines.Length][];
            int currentRow = 0;

            foreach (var line in lines)
            {
                grid[currentRow] = new bool[line.Length];
                int currentCol = 0;

                foreach (var point in line)
                {
                    switch (point)
                    {
                    case '#':
                        grid[currentRow][currentCol] = false;
                        break;

                    case '.':
                        grid[currentRow][currentCol] = true;
                        break;

                    case 'S':
                        grid[currentRow][currentCol] = true;
                        start = new Point(currentCol, currentRow);
                        break;

                    case 'F':
                        grid[currentRow][currentCol] = true;
                        finish = new Point(currentCol, currentRow);
                        break;

                    default:
                        throw new Exception("Maze input string contains invalid characters");
                    }

                    currentCol++;
                }

                currentRow++;
            }

            if (start == null)
            {
                throw new Exception("Maze should have a start position set.");
            }
            if (finish == null)
            {
                throw new Exception("Maze should have a finish position set.");
            }

            var maze   = new MazeGrid(grid, start, finish);
            var entity = new DumbMazeWalker(maze);

            bool endOfMazeReached = false;

            while (!endOfMazeReached)
            {
                var couldMoveForward = entity.MoveForward();

                if (!couldMoveForward)
                {
                    entity.TurnRight();
                }
                else
                {
                    if (entity.CanSeeLeftTurning())
                    {
                        entity.TurnLeft();
                    }
                }

                endOfMazeReached = maze.AtFinish(entity);
                Console.WriteLine(entity.CurrentPosition);
            }

            Console.WriteLine("Reached end of maze! :)");
        }
 public FancyWalkerStateDumper(MazeVisualDumper mazeDumper, MazeGrid maze)
 {
     _mazeDumper = mazeDumper;
     _maze       = maze;
 }
Beispiel #6
0
 public DumbMazeWalker(MazeGrid mazeGrid)
 {
     m_MazeGrid      = mazeGrid;
     CurrentPosition = m_MazeGrid.StartPosition;
     m_direc         = Orientation.South;
 }
Beispiel #7
0
        private static MazeGrid GetMaze(string mazeFilePath)
        {
            // todo: handle \n newline characters instead of Environment.NewLine when you download a zip file
            var lines = new StreamReader(new FileStream(mazeFilePath, FileMode.Open)).ReadToEnd().Replace(" ", "")
                        .Split(new[] { Environment.NewLine }, StringSplitOptions.None);
            Point start  = null;
            Point finish = null;

            var grid       = new bool[lines.Length][];
            int currentRow = 0;

            foreach (var line in lines)
            {
                grid[currentRow] = new bool[line.Length];
                int currentCol = 0;

                foreach (var point in line)
                {
                    switch (point)
                    {
                    case '#':
                        grid[currentRow][currentCol] = false;
                        break;

                    case '.':
                        grid[currentRow][currentCol] = true;
                        break;

                    case 'S':
                        grid[currentRow][currentCol] = true;
                        start = new Point(currentCol, currentRow);
                        break;

                    case 'F':
                        grid[currentRow][currentCol] = true;
                        finish = new Point(currentCol, currentRow);
                        break;

                    default:
                        throw new Exception("Maze input string contains invalid characters");
                    }

                    currentCol++;
                }

                currentRow++;
            }

            if (start == null)
            {
                throw new Exception("Maze should have a start position set.");
            }
            if (finish == null)
            {
                throw new Exception("Maze should have a finish position set.");
            }

            var maze = new MazeGrid(grid, start, finish);

            return(maze);
        }
Beispiel #8
0
 public DumbMazeWalker(MazeGrid mazeGrid)
 {
     _mMazeGrid      = mazeGrid;
     CurrentPosition = _mMazeGrid.StartPosition;
     _mDirec         = Orientation.South;
 }