Exemplo n.º 1
0
        public List <Orientation> WhereCanIGo(int i, int j)
        {
            List <Orientation> orients = new List <Orientation>();

            if (i - 2 >= 0 && !MazeMatrix[i - 2, j].IsUsed && MazeMatrix[i - 2, j].Value == CellType.Empty)
            {
                orients.Add(Orientation.Up);
            }

            if (j + 2 < MazeMatrix.GetLength(1) && !MazeMatrix[i, j + 2].IsUsed && MazeMatrix[i, j + 2].Value == CellType.Empty)
            {
                orients.Add(Orientation.Right);
            }

            if (i + 2 < MazeMatrix.GetLength(0) && !MazeMatrix[i + 2, j].IsUsed && MazeMatrix[i + 2, j].Value == CellType.Empty)
            {
                orients.Add(Orientation.Down);
            }

            if (j - 2 >= 0 && !MazeMatrix[i, j - 2].IsUsed && MazeMatrix[i, j - 2].Value == CellType.Empty)
            {
                orients.Add(Orientation.Left);
            }


            return(orients);
        }
    private void TryToMove(Vector2 actualPos, Vector2 movement, out Vector2 newPos, bool allowMoveOverPath)
    {
        Vector2 initialPos = actualPos;

        newPos = actualPos + movement;
        //Verify that new pos is inside matrix
        if ((newPos.x > -1 && newPos.x < MazeMatrix.GetLength(0)) && (newPos.y > 0 && newPos.y < MazeMatrix.GetLength(1)))
        {
            //verify that newPos is a wall
            if (MazeMatrix[(int)newPos.x, (int)newPos.y] == (int)MazeElement.Wall)
            {
                newPos = initialPos;
            }

            if (!allowMoveOverPath)
            {
                if (pathMemory[(int)newPos.x, (int)newPos.y] == (int)MazeElement.Path)
                {
                    newPos = initialPos;
                }
            }
        }
        else
        {
            newPos = initialPos;
        }
    }
Exemplo n.º 3
0
 public void FillMaze()
 {
     for (int i = 0; i < MazeMatrix.GetLength(0); i++)
     {
         for (int j = 0; j < MazeMatrix.GetLength(1); j++)
         {
             if (i % 2 == 1 && j % 2 == 1)
             {
                 MazeMatrix[i, j] = new Cell(i, j, CellType.Empty);
             }
             else
             {
                 MazeMatrix[i, j] = new Cell(i, j, CellType.Wall);
             }
         }
     }
 }
Exemplo n.º 4
0
 public void ResetMaze()
 {
     for (int i = 0; i < MazeMatrix.GetLength(0); i++)
     {
         for (int j = 0; j < MazeMatrix.GetLength(1); j++)
         {
             if (i % 2 == 1 && j % 2 == 1)
             {
                 MazeMatrix[i, j].SetValues(i, j, CellType.Empty);
             }
             else
             {
                 MazeMatrix[i, j].SetValues(i, j, CellType.Wall);
             }
         }
     }
 }
Exemplo n.º 5
0
        /// <summary>
        /// Loads the input file, to create the maze.
        /// </summary>
        public void LoadMaze(string mazeFile)
        {
            try
            {
                using (StreamReader sr = new StreamReader(mazeFile))
                {
                    // Leave the first 4 lines off adding to map, map info on them.
                    int mapHeightIndex = -4;

                    while (!sr.EndOfStream)
                    {
                        string line = sr.ReadLine();

                        if (mapHeightIndex >= 0)
                        {
                            char[] lineChars = line.ToCharArray();

                            MazeMatrix.Add(new List <Tile>());

                            for (int i = 0; i < lineChars.Length; i++)
                            {
                                char character = lineChars[i];

                                if (character == WALL || character == HORIZONTAL_WALL ||
                                    character == VERTICAL_WALL || character == TOP_LEFT_WALL ||
                                    character == TOP_RIGHT_WALL || character == BOTTOM_LEFT_WALL ||
                                    character == BOTTOM_RIGHT_WALL || character == INTERSECTION_TOP_WALL ||
                                    character == INTERSECTION_LEFT_WALL || character == INTERSECTION_RIGHT_WALL ||
                                    character == INTERSECTION_BOTTOM_WALL || character == INTERSECTION_FOURWAY)
                                {
                                    MazeMatrix[mapHeightIndex].Add(new Tile(character.ToString(), false));
                                }
                                else if (character == START_POSITION)
                                {
                                    StartPosition = new int[] { mapHeightIndex, i };
                                    MazeMatrix[mapHeightIndex].Add(new Tile());
                                }
                                else if (character == EXIT)
                                {
                                    MazeMatrix[mapHeightIndex].Add(
                                        new Tile(character.ToString(), true, true));
                                }
                                else
                                {
                                    MazeMatrix[mapHeightIndex].Add(new Tile());
                                }
                            }
                        }
                        else
                        {
                            if (mapHeightIndex == -4)
                            {
                                MazeName = line.Substring(11);
                            }
                            else if (mapHeightIndex == -3)
                            {
                                MazeLength = int.Parse(line.Substring(12));
                            }
                            else if (mapHeightIndex == -2)
                            {
                                MazeHeight = int.Parse(line.Substring(12));
                            }
                        }

                        mapHeightIndex++;
                    }
                }
            }
            catch (IOException)
            {
                Console.WriteLine("Error reading the maze.");
            }
        }