void Start() { MazeCell[,] grid = new MazeCell[14, 10]; Build(grid); List <Vector2> goal_position = new List <Vector2>(); goal_position.Add(new Vector2(13, 4)); goal_position.Add(new Vector2(13, 5)); GenerateMaze(grid, starting_point, goal_position); Vector3 spawn_ball_position = starting_point; for (int i = 0; i < 10; i++) { int x = Random.Range(0, grid.GetLength(0)); int y = Random.Range(0, grid.GetLength(1)); Vector3 pos = grid[x, y].transform.position; if (pos != grid[(int)starting_point.x, (int)starting_point.y].transform.position && !goal_position.Contains(pos)) { spawn_ball_position = pos; break; } } GameManager.GetInstance().SpawnBall(spawn_ball_position); Instantiate(controlled_soldier, grid[(int)starting_point.x, (int)starting_point.y].transform.position + Vector3.up * controlled_soldier.GetComponentInChildren <MeshRenderer>().bounds.extents.y, Quaternion.identity, transform); transform.parent = FindObjectOfType <GameField>().transform; }
public static void CreateMazeArray(char[,] canvasMazeArray) { MouseChecker = false; ExitChecker = false; MazeArray = new MazeCell[canvasMazeArray.GetLength(0) + 2, canvasMazeArray.GetLength(1) + 2]; MazeToBeShowed = new MazeCell[canvasMazeArray.GetLength(0), canvasMazeArray.GetLength(1)]; for (int y = 1; y < MazeArray.GetLength(0) - 1; y++) { for (int x = 1; x < MazeArray.GetLength(1) - 1; x++) { MazeArray[y, x] = new MazeCell(x, y, canvasMazeArray[y - 1, x - 1]); MazeToBeShowed[y - 1, x - 1] = new MazeCell(x - 1, y - 1, canvasMazeArray[y - 1, x - 1]); } } for (int x = 0; x < MazeArray.GetLength(1); x++) { MazeArray[0, x] = new MazeCell(x, 0, '1'); } for (int x = 0; x < MazeArray.GetLength(1); x++) { MazeArray[MazeArray.GetLength(0) - 1, x] = new MazeCell(MazeArray.GetLength(0) - 1, x, '1'); } for (int y = 1; y < MazeArray.GetLength(0) - 1; y++) { MazeArray[y, 0] = new MazeCell(0, y, '1'); MazeArray[y, MazeArray.GetLength(1) - 1] = new MazeCell(MazeArray.GetLength(0) - 1, y, '1'); } foreach (var mazeCell in MazeArray) { if (mazeCell.Character == 'm') { MouseChecker = true; } if (mazeCell.Character == 'e') { ExitChecker = true; } } if (MouseChecker && ExitChecker) { InitializeMaze(MazeArray); } else { if (!MouseChecker) { MessageBox.Show("No entrance in maze.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); } if (!ExitChecker) { MessageBox.Show("No exit in maze.", "Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation); } } }
private static MazeCell[,] GenerateGrid(int width, int height) { MazeCell[,] grid = new MazeCell[width, height]; for (int x = 0; x < grid.GetLength(0); x++) { for (int y = 0; y < grid.GetLength(1); y++) { grid[x, y] = new MazeCell(x, y); } } return(grid); }
// Siempre son al Este y Sur private ArrayList createRandomFrontier(MazeCell[,] currentSection) { ArrayList subSets = new ArrayList(); int cRows = currentSection.GetLength(0); int cCols = currentSection.GetLength(1); int traceDirection = Random.Range(0, 2); int tracePosition = -1; int randomPass = -1; // Arreglos por si no se puede alguna de las dos if (cRows == 1) { traceDirection = 1; } else if (cCols == 1) { traceDirection = 0; } // Línea horizontal if (traceDirection == 0) { // Elegimos un paso entre la frontera de forma aleatoria randomPass = Random.Range(0, cCols); // Elegimos un muro de frontera entre todos los posibles tracePosition = Random.Range(0, cRows - 1); for (int i = 0; i < cCols; i++) { if (i != randomPass) { currentSection[tracePosition, i].wallS.SetActive(true); } } // Creamos las submatrices generadas MazeCell[,] subMatrix1 = new MazeCell[tracePosition + 1, cCols]; MazeCell[,] subMatrix2 = new MazeCell[cRows - (tracePosition + 1), cCols]; for (int i = 0; i < subMatrix1.GetLength(0); i++) { for (int j = 0; j < cCols; j++) { subMatrix1[i, j] = currentSection[i, j]; } } for (int i = subMatrix1.GetLength(0); i < currentSection.GetLength(0); i++) { for (int j = 0; j < cCols; j++) { subMatrix2[i - subMatrix1.GetLength(0), j] = currentSection[i, j]; } } // Colocamos la menor primero if (subMatrix1.GetLength(0) * subMatrix1.GetLength(1) > subMatrix2.GetLength(0) * subMatrix2.GetLength(1)) { subSets.Add(subMatrix2); subSets.Add(subMatrix1); } else { subSets.Add(subMatrix1); subSets.Add(subMatrix2); } } // Línea vertical else { randomPass = Random.Range(0, cRows); tracePosition = Random.Range(0, cCols - 1); for (int i = 0; i < cRows; i++) { if (i != randomPass) { currentSection[i, tracePosition].wallE.SetActive(true); } } // Creamos las submatrices generadas MazeCell[,] subMatrix1 = new MazeCell[cRows, tracePosition + 1]; MazeCell[,] subMatrix2 = new MazeCell[cRows, cCols - (tracePosition + 1)]; for (int j = 0; j < cRows; j++) { for (int i = 0; i < subMatrix1.GetLength(1); i++) { subMatrix1[j, i] = currentSection[j, i]; } } for (int j = 0; j < cRows; j++) { for (int i = subMatrix1.GetLength(1); i < currentSection.GetLength(1); i++) { subMatrix2[j, i - subMatrix1.GetLength(1)] = currentSection[j, i]; } } // Colocamos la menor primer if (subMatrix1.GetLength(0) * subMatrix1.GetLength(1) > subMatrix2.GetLength(0) * subMatrix2.GetLength(1)) { subSets.Add(subMatrix2); subSets.Add(subMatrix1); } else { subSets.Add(subMatrix1); subSets.Add(subMatrix2); } } return(subSets); }
public MazeCell[,] GenerateMaze(int _x, int _y, int _seed) { System.Random rng = new System.Random(_seed); MazeCell[,] maze = new MazeCell[_x, _y]; for (int x = 0; x < maze.GetLength(0); x++) { for (int y = 0; y < maze.GetLength(1); y++) { maze[x, y] = new MazeCell(); } } // algorithm here Vector2Int currentPos = new Vector2Int(0, 0); int counterFilled = 0; int cellCount = maze.GetLength(0) * maze.GetLength(1); Stack <Vector2Int> backtrackStack = new Stack <Vector2Int>(); while (counterFilled < cellCount) { maze[currentPos.x, currentPos.y].visited = true; Vector2Int[] notVisitedNeighbours = GetNotVisitedNeighbours(currentPos, maze); if (notVisitedNeighbours.Length > 0) { if (notVisitedNeighbours.Length > 1) { backtrackStack.Push(currentPos); } int randomNeighbour = rng.Next(notVisitedNeighbours.Length); counterFilled++; Vector2Int nextPosition = notVisitedNeighbours[randomNeighbour]; Vector2Int direction = nextPosition - currentPos; if (direction == Vector2Int.up) { maze[currentPos.x, currentPos.y].top = true; maze[currentPos.x, currentPos.y + 1].down = true; } else if (direction == Vector2Int.down) { maze[currentPos.x, currentPos.y].down = true; maze[currentPos.x, currentPos.y - 1].top = true; } else if (direction == Vector2Int.left) { maze[currentPos.x, currentPos.y].left = true; maze[currentPos.x - 1, currentPos.y].right = true; } else if (direction == Vector2Int.right) { maze[currentPos.x, currentPos.y].right = true; maze[currentPos.x + 1, currentPos.y].left = true; } currentPos = nextPosition; } else if (backtrackStack.Count > 0) { currentPos = backtrackStack.Pop(); } else { break; } } return(maze); }