コード例 #1
0
        public static string[,] SolveLabyrinth(string[,] labyrinth)
        {
            var solvedLabyrinth = (string[,])labyrinth.Clone();

            int rows = solvedLabyrinth.GetLength(0);
            int cols = solvedLabyrinth.GetLength(1);

            var startPostitions = GetCells(solvedLabyrinth, "*");
            var cells = new Queue<Cell>(startPostitions);
            while (cells.Count > 0)
            {
                var currentCell = cells.Dequeue();

                int stepNumber;
                string nextSymbol = int.TryParse(currentCell.Symbol, out stepNumber) ? (stepNumber + 1).ToString() : "1";

                if (0 < currentCell.Row && solvedLabyrinth[currentCell.Row - 1, currentCell.Col] == "0")
                {
                    var upCell = new Cell(currentCell.Row - 1, currentCell.Col, nextSymbol);
                    WriteCellInLabyrinth(upCell, solvedLabyrinth);
                    cells.Enqueue(upCell);
                }

                if (currentCell.Row < rows - 1 && solvedLabyrinth[currentCell.Row + 1, currentCell.Col] == "0")
                {
                    var downCell = new Cell(currentCell.Row + 1, currentCell.Col, nextSymbol);
                    WriteCellInLabyrinth(downCell, solvedLabyrinth);
                    cells.Enqueue(downCell);
                }

                if (0 < currentCell.Col && solvedLabyrinth[currentCell.Row, currentCell.Col - 1] == "0")
                {
                    var leftCell = new Cell(currentCell.Row, currentCell.Col - 1, nextSymbol);
                    WriteCellInLabyrinth(leftCell, solvedLabyrinth);
                    cells.Enqueue(leftCell);
                }

                if (currentCell.Col < cols - 1 && solvedLabyrinth[currentCell.Row, currentCell.Col + 1] == "0")
                {
                    var rightCell = new Cell(currentCell.Row, currentCell.Col + 1, nextSymbol);
                    WriteCellInLabyrinth(rightCell, solvedLabyrinth);
                    cells.Enqueue(rightCell);
                }
            }

            var unreachableCells = GetCells(solvedLabyrinth, "0");
            foreach (var cell in unreachableCells)
            {
                solvedLabyrinth[cell.Row, cell.Col] = "u";
            }

            return solvedLabyrinth;
        }
コード例 #2
0
        private bool ExitFound(Cell cell)
        {
            bool exitFound = false;
            if (cell.Row == LABYRINTH_SIZE - 1 ||

                cell.Col == LABYRINTH_SIZE - 1 ||
                cell.Row == 0 ||
                cell.Col == 0)
            {
                exitFound = true;
            }

            return exitFound;
        }
コード例 #3
0
        public Labyrinth(PlayerPosition startPosition, Cell[,] board)
        {
            if (startPosition == null)
            {
                throw new ArgumentNullException("The start position cannot be null");
            }

            if (board == null)
            {
                throw new ArgumentNullException("The board cannot be null");
            }

            this.Position = startPosition;
            this.board = board;
        }
コード例 #4
0
        public bool TryMove(Cell cell, Direction direction)
        {
            int newRow;
            int newCol;
            FindNewCellCoordinates(cell.Row, cell.Col, direction,
                out newRow, out newCol);

            if (newRow < 0 || newCol < 0 ||
                newRow >= labyrinth.GetLength(0) || newCol >= labyrinth.GetLength(1))
            {
                return false;
            }

            if (!labyrinth[newRow, newCol].IsEmpty())
            {

                return false;
            }

            this.labyrinth[newRow, newCol].ValueChar = '*';
            this.labyrinth[cell.Row, cell.Col].ValueChar = '-';
            this.currentCell = labyrinth[newRow, newCol];
            return true;
        }
コード例 #5
0
        public object Clone()
        {
            Cell cloned = new Cell(this.Row, this.Column, this.Value);

            return cloned;
        }
コード例 #6
0
 public Labyrinth(Random rand)
 {
     GenerateLabyrinth(rand);
     currentCell = labyrinth[LabyrintStartRow, LabyrintStartRow];
 }
コード例 #7
0
        private void premestvane(Cell cell, Direction direction,
            Queue<Cell> cellsOrder, HashSet<Cell> visitedCells)
        {
            int newRow;
            int newCol;
            FindNewCellCoordinates(cell.Row, cell.Col, direction,
                out newRow, out newCol);

            if (newRow < 0 || newCol < 0 ||
                newRow >= labyrinth.GetLength(0) || newCol >= labyrinth.GetLength(1))

            {
                return;
            }

            if (visitedCells.Contains(labyrinth[newRow,newCol]))
            {
                return;
            }

            if (labyrinth[newRow, newCol].IsEmpty())
            {
                cellsOrder.Enqueue(labyrinth[newRow, newCol]);
            }
        }
コード例 #8
0
 private static void WriteCellInLabyrinth(Cell cell, string[,] labyrinth)
 {
     labyrinth[cell.Row, cell.Col] = cell.Symbol;
 }
コード例 #9
0
        public Cell[,] Clone()
        {
            Cell[,] cloned = new Cell[LabyrinthSize, LabyrinthSize];

            for (int row = 0; row < LabyrinthSize; row++)
            {
                for (int column = 0; column < LabyrinthSize; column++)
                {
                    cloned[row, column] = this.board[row, column].Clone() as Cell;
                }
            }

            return cloned;
        }
コード例 #10
0
 private void VisitCell(Cell[,] labyrinth, Queue<Cell> visitedCells, 
     int row, int column)
 {
     if (labyrinth[row, column].Value == Cell.Empty || 
         labyrinth[row, column].Value == Cell.Player)
     {
         labyrinth[row, column].Value = Cell.Block;
         visitedCells.Enqueue(labyrinth[row, column]);
     }
 }
コード例 #11
0
        public static void FillOutputLabyrinth(Cell startingCell, string[,] outputLabyrinth)
        {
            bool[,] isUsed = new bool[inputLabirinth.GetLength(0), inputLabirinth.GetLength(1)];

            var queue = new Queue<Cell>();

            queue.Enqueue(startingCell);
            isUsed[startingCell.Row, startingCell.Column] = true;
            outputLabyrinth[startingCell.Row, startingCell.Column] = "*";

            while (queue.Count > 0)
            {
                var cell = queue.Dequeue();

                // Check and enqueue left cell
                if (cell.Column > 0 && !isUsed[cell.Row, cell.Column - 1] && inputLabirinth[cell.Row, cell.Column - 1] != "X")
                {
                    var newCell = new Cell() { Column = cell.Column - 1, Row = cell.Row, Step = cell.Step + 1 };
                    queue.Enqueue(newCell);
                    isUsed[newCell.Row, newCell.Column] = true;
                    outputLabyrinth[newCell.Row, newCell.Column] = newCell.Step.ToString();
                }

                // Check and enqueue right cell
                if (cell.Column < inputLabirinth.GetLength(1) - 1 && !isUsed[cell.Row, cell.Column + 1] && inputLabirinth[cell.Row, cell.Column + 1] != "X")
                {
                    var newCell = new Cell() { Column = cell.Column + 1, Row = cell.Row, Step = cell.Step + 1 };
                    queue.Enqueue(newCell);
                    isUsed[newCell.Row, newCell.Column] = true;
                    outputLabyrinth[newCell.Row, newCell.Column] = newCell.Step.ToString();
                }

                // Check and enqueue top cell
                if (cell.Row > 0 && !isUsed[cell.Row - 1, cell.Column] && inputLabirinth[cell.Row - 1, cell.Column] != "X")
                {
                    var newCell = new Cell() { Column = cell.Column, Row = cell.Row - 1, Step = cell.Step + 1 };
                    queue.Enqueue(newCell);
                    isUsed[newCell.Row, newCell.Column] = true;
                    outputLabyrinth[newCell.Row, newCell.Column] = newCell.Step.ToString();
                }

                // Check and enqueue bottom cell
                if (cell.Row < inputLabirinth.GetLength(0) - 1 && !isUsed[cell.Row + 1, cell.Column] && inputLabirinth[cell.Row + 1, cell.Column] != "X")
                {
                    var newCell = new Cell() { Column = cell.Column, Row = cell.Row + 1, Step = cell.Step + 1 };
                    queue.Enqueue(newCell);
                    isUsed[newCell.Row, newCell.Column] = true;
                    outputLabyrinth[newCell.Row, newCell.Column] = newCell.Step.ToString();
                }
            }

            // Fill the missing cells
            for (int row = 0; row < outputLabyrinth.GetLength(0); row++)
            {
                for (int col = 0; col < outputLabyrinth.GetLength(1); col++)
                {
                    if (outputLabyrinth[row, col] == null)
                    {
                        outputLabyrinth[row, col] = "U";
                    }

                    if (inputLabirinth[row, col] == "X")
                    {
                        outputLabyrinth[row, col] = "X";
                    }
                }
            }
        }
コード例 #12
0
        public static Cell FindStartingCell()
        {
            var startingCell = new Cell()
            {
                Step = 0
            };
            var haveFoundStartingCell = false;

            for (int row = 0; row < inputLabirinth.GetLength(0); row++)
            {
                for (int col = 0; col < inputLabirinth.GetLength(1); col++)
                {
                    if (inputLabirinth[row, col] == "*")
                    {
                        startingCell.Row = row;
                        startingCell.Column = col;
                        haveFoundStartingCell = true;
                        break;
                    }
                }

                if (haveFoundStartingCell)
                {
                    break;
                }
            }

            return startingCell;
        }