Exemple #1
0
 public Cell(int row, int col, char value, Cell predecessor)
 {
     this.row = row;
     this.col = col;
     this.value = value;
     this.predecessor = predecessor;
 }
Exemple #2
0
 public bool ContainsCell(Cell cell)
 {
     if (cells.Contains(cell))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Exemple #3
0
 public Path(Cell exitCell)
 {
     cells = new List<Cell>();
     Cell current = exitCell;
     while (current != null)
     {
         cells.Add(current);
         current = current.Predecessor;
     }
     cells.Reverse();
 }
Exemple #4
0
 public Cell GetTopNeighbour(Cell current)
 {
     if (current.Row - 1 < 0)
     {
         return null;
     }
     return maze[current.Row - 1, current.Column];
 }
Exemple #5
0
 public Cell GetRightNeighbour(Cell current)
 {
     if (current.Column + 1 >= maze.GetLength(1))
     {
         return null;
     }
     return maze[current.Row, current.Column + 1];
 }
Exemple #6
0
 public Cell GetLeftNeighbour(Cell current)
 {
     if (current.Column - 1 < 0)
     {
         return null; // indicates reaching an exit
     }
     return maze[current.Row, current.Column - 1];
 }
Exemple #7
0
 public Cell GetBottomNeighbour(Cell current)
 {
     if (current.Row + 1 == maze.GetLength(0))
     {
         return null;
     }
     return maze[current.Row + 1, current.Column];
 }
Exemple #8
0
        public List<Path> FindAllPaths()
        {
            paths = new List<Path>();
            Queue<Cell> queue = new Queue<Cell>();
            queue.Enqueue(startingPos);
            while (queue.Count > 0)
            {
                Cell current = queue.Dequeue();

                Cell leftNeighbour = GetLeftNeighbour(current);
                if (leftNeighbour == null)
                {
                    Path newPath = new Path(current);
                    if (!paths.Contains(newPath))
                        paths.Add(newPath);
                }
                else if (leftNeighbour.Value != '#')
                {
                    Cell leftNeighbourUpdated = new Cell(leftNeighbour.Row, leftNeighbour.Column,
                        leftNeighbour.Value, current);
                    if (!current.HasPredecessor(leftNeighbourUpdated)) // if not already has predecessor
                    {
                        queue.Enqueue(leftNeighbourUpdated);
                    }
                }

                Cell topNeighbour = GetTopNeighbour(current);
                if (topNeighbour == null)
                {
                    Path newPath = new Path(current);
                    if (!paths.Contains(newPath))
                        paths.Add(newPath);
                }
                else if (topNeighbour.Value != '#')
                {
                    Cell topNeighbourUpdated = new Cell(topNeighbour.Row, topNeighbour.Column,
                        topNeighbour.Value, current);
                    if (!current.HasPredecessor(topNeighbourUpdated))
                    {
                        queue.Enqueue(topNeighbourUpdated);
                    }
                }

                Cell rightNeighbour = GetRightNeighbour(current);
                if (rightNeighbour == null)
                {
                    Path newPath = new Path(current);
                    if (!paths.Contains(newPath))
                        paths.Add(newPath);
                }
                else if (rightNeighbour.Value != '#')
                {
                    Cell rightNeighbourUpdated = new Cell(rightNeighbour.Row, rightNeighbour.Column,
                        rightNeighbour.Value, current);
                    if (!current.HasPredecessor(rightNeighbourUpdated))
                    {
                        queue.Enqueue(rightNeighbourUpdated);
                    }
                }

                Cell bottomNeighbour = GetBottomNeighbour(current);
                if (bottomNeighbour == null)
                {
                    Path newPath = new Path(current);
                    if (!paths.Contains(newPath))
                        paths.Add(newPath);
                }
                else if (bottomNeighbour.Value != '#')
                {
                    Cell bottomNeighbourUpdated = new Cell(bottomNeighbour.Row, bottomNeighbour.Column,
                        bottomNeighbour.Value, current);
                    if (!current.HasPredecessor(bottomNeighbourUpdated))
                    {
                        queue.Enqueue(bottomNeighbourUpdated);
                    }
                }
            }
            return paths;
        }
Exemple #9
0
 public Maze(string filePath)
 {
     if (!File.Exists(filePath))
     {
         throw new FileNotFoundException(String.Format("File \"{0}\" was not found.", filePath));
     }
     StreamReader reader = new StreamReader(filePath);
     startingPos = null;
     int size = int.Parse(reader.ReadLine()); // it's always in the first line by definition
     this.maze = new Cell[size, size];
     for (int i = 0; i < size; i++)
     {
         string line = reader.ReadLine();
         for (int p = 0; p < line.Length; p += 2)
         {
             maze[i, p / 2] = new Cell(i, p / 2, line[p], null);
             if (line[p] == '*')
             {
                 startingPos = maze[i, p / 2];
             }
         }
     }
     if (startingPos == null)
     {
         throw new Exception("No staring posiiton in the maze.");
     }
     reader.Close();
 }
Exemple #10
0
 public bool HasPredecessor(Cell other)
 {
     Path pathSoFar = new Path(this);
     if (pathSoFar.ContainsCell(other))
     {
         return true;
     }
     else
     {
         return false;
     }
 }