private void SetNewPosition(Vector2 newPosition) { // Check if we are not going out the map fist if (newPosition.x < 0 || newPosition.y < 0 || newPosition.x > Columns - 1 || newPosition.y > Rows - 1) { return; } // Check for the right or left wall switch ((int)Position.x - (int)newPosition.x) { case -1: if (MazeCells.ElementAt((int)(Position.x + Position.y * Columns)).Value.GetWallStatus(Cell.CellWalls.RightWall)) { return; } break; case 1: if (MazeCells.ElementAt((int)(Position.x + Position.y * Columns)).Value.GetWallStatus(Cell.CellWalls.LeftWall)) { return; } break; } // Check for the top or bottom wall switch ((int)Position.y - (int)newPosition.y) { case -1: if (MazeCells.ElementAt((int)(Position.x + Position.y * Columns)).Value.GetWallStatus(Cell.CellWalls.TopWall)) { return; } break; case 1: if (MazeCells.ElementAt((int)(Position.x + Position.y * Columns)).Value.GetWallStatus(Cell.CellWalls.BottomWall)) { return; } break; } Position = newPosition; transform.position = new Vector2(newPosition.x * Width + (Width / 2), newPosition.y * Height + (Height / 2)); // Show the menu when we reach the end of the maze if (Position == new Vector2(Columns - 1, 0)) { MazeManager.ShowMenu(); } nextMove = Time.time + moveInterval; }
/// <summary> /// The ChooseRandomCell method is called to choose a random cell in the maze. /// </summary> /// <returns></returns> private MazeCell ChooseRandomCell() { try { // Select a random cell to start. int cellIndex = _randomNumberGenerator.Next(Constants.MazeHeight * Constants.MazeWidth); if (IsCellIndexValid(cellIndex)) { return(MazeCells.ElementAt(cellIndex)); } else { throw new Exception("Unable to choose a randmom cell."); } } catch (Exception ex) { throw new Exception("Maze.ChooseRandomCell(): " + ex.ToString()); } }
public GameObject GetRandomAvailableNeighbour() { List <GameObject> neighbours = new List <GameObject>(); // Check if the position is available first and then add it Cell top = GetIndex(Position.x, Position.y + 1) != -1 ? MazeCells.ElementAt(GetIndex(Position.x, Position.y + 1)).Value : null; Cell right = GetIndex(Position.x + 1, Position.y) != -1 ? MazeCells.ElementAt(GetIndex(Position.x + 1, Position.y)).Value : null; Cell bottom = GetIndex(Position.x, Position.y - 1) != -1 ? MazeCells.ElementAt(GetIndex(Position.x, Position.y - 1)).Value : null; Cell left = GetIndex(Position.x - 1, Position.y) != -1 ? MazeCells.ElementAt(GetIndex(Position.x - 1, Position.y)).Value : null; // If the neighbours are available and not visited add them to our neighbours list if (top && !top.IsVisited) { neighbours.Add(top.gameObject); } if (right && !right.IsVisited) { neighbours.Add(right.gameObject); } if (bottom && !bottom.IsVisited) { neighbours.Add(bottom.gameObject); } if (left && !left.IsVisited) { neighbours.Add(left.gameObject); } // Choose one at random if we have neighbours if (neighbours.Count > 0) { return(neighbours[Random.Range(0, neighbours.Count)]); } else { return(null); } }
public void GenerateGrid() { // Nested for-loop to fill the screen with cells for (int y = 0; y < MazeInput.Instance.MazeRows; y++) { for (int x = 0; x < MazeInput.Instance.MazeColumns; x++) { // Make the cell and set the properties GameObject cell = Instantiate(MazeInput.Instance.CellPrefab); // We set the parent first because we want to set the position relative to the canvas cell.transform.SetParent(MazeInput.Instance.MazeCanvas.transform); // Set the position with a small offset because the pivot point of the cell is in the center cell.transform.position = new Vector2(x * CellWidth + (CellWidth / 2), y * CellHeight + (CellHeight / 2)); cell.transform.localScale = new Vector2(CellWidth, CellHeight); cell.name = "Cell: " + (x + (y * MazeInput.Instance.MazeColumns)); // Set the index of the cell Cell cellScript = cell.GetComponent <Cell>(); cellScript.Position = new Vector2(x, y); cellScript.MazeRows = MazeInput.Instance.MazeRows; cellScript.MazeColumns = MazeInput.Instance.MazeColumns; // Set the cell colors cellScript.BackgroundColor = MazeInput.Instance.CellBackgroundColor; cellScript.WallColor = MazeInput.Instance.CellWallColor; cellScript.HighlightColor = MazeInput.Instance.CellHighlightColor; cellScript.VisitedColor = MazeInput.Instance.CellVisitedColor; previousCells.Add(cell); MazeCells.Add(cell, cellScript); } } // We do this at the end of generating the maze just so we have a start and end from the top left cell and bottom right cell MazeCells.ElementAt((MazeInput.Instance.MazeRows - 1) * MazeInput.Instance.MazeColumns).Value.RemoveWall(Cell.CellWalls.LeftWall); MazeCells.ElementAt(MazeInput.Instance.MazeColumns - 1).Value.RemoveWall(Cell.CellWalls.RightWall); }
/// <summary> /// The ChooseRandomCell method is called to choose a random cell in the maze. /// This random cell can not be on the edge of the maze, as the maze edges are all walls. /// </summary> /// <returns></returns> private MazeCell ChooseRandomCell() { try { // X and Y indexes must not be on the edges - a offset of 2 is used to ensure this. int cellIndexX = _randomNumberGenerator.Next(2, MazeWidthHeightCells - 2); int cellIndexY = _randomNumberGenerator.Next(2, MazeWidthHeightCells - 2); if (IsCellIndexValid(cellIndexX * cellIndexY)) { return(MazeCells.ElementAt(cellIndexX * cellIndexY)); } else { throw new Exception("Unable to choose a random cell."); } } catch (Exception ex) { throw new Exception("Maze.ChooseRandomCell(): " + ex.ToString()); } }