public Cell[,] Run(Cell[,] cells) { int maxXIndex = cells.GetUpperBound(0); int maxYIndex = cells.GetUpperBound(1); if (maxXIndex < 2 || maxYIndex < 2) { throw new ArgumentException("The minimum array size is 3x3."); } Cell[,] nextGenCells = new Cell[cells.GetLength(0), cells.GetLength(1)]; Array.Copy(cells, nextGenCells, cells.Length); for (int xCoord = 0; xCoord <= maxXIndex; xCoord++) { for (int yCoord = 0; yCoord <= maxYIndex; yCoord++) { bool isAlive = _lifeValidator.IsAlive(cells, cells[xCoord, yCoord]); nextGenCells[xCoord, yCoord].IsAlive = isAlive; } } return(nextGenCells); }
public void GenerateTiles(Cell[,] cells) { int rowNum = cells.GetUpperBound(0) + 1; int colNum = cells.GetUpperBound(cells.Rank - 1) + 1; for (int i = 0; i < rowNum; i++) { gridGame.RowDefinitions.Add(new RowDefinition()); } for (int j = 0; j < colNum; j++) { gridGame.ColumnDefinitions.Add(new ColumnDefinition()); } for (int i = 0; i < rowNum; i++) { for (int j = 0; j < colNum; j++) { cells[i, j] = new Cell(i, j); cells[i, j].SetValue(Grid.RowProperty, i); cells[i, j].SetValue(Grid.ColumnProperty, j); cells[i, j].Click += Cell_LeftClick; cells[i, j].MouseRightButtonDown += new MouseButtonEventHandler(Cell_RightClick); gridGame.Children.Add(cells[i, j]); } } }
public Grid(Cell[,] grid) { this._Grid = grid; _RowCount = grid.GetUpperBound(0) + 1; _ColCount = grid.GetUpperBound(1) + 1; }
/// <summary> /// Gets or Sets a Cell /// </summary> public Cell this[int RowNumber, int ColumnNumber] { get { //check to see if the cell exists if (RowNumber >= 0 && ColumnNumber >= 0 && RowNumber <= Cells.GetUpperBound(0) && ColumnNumber <= Cells.GetUpperBound(1)) { //return found cell return(Cells[RowNumber, ColumnNumber]); } else { //error - no cell found return(null); //throw new NoCellException } } set { //Check the number of Cell to exist if (RowNumber >= 0 && ColumnNumber >= 0 && RowNumber <= Cells.GetUpperBound(0) && ColumnNumber <= Cells.GetUpperBound(1)) { //set value Cells[RowNumber, ColumnNumber] = value; } else { //throw new NoCellException } } }
// Generate mesh based on GameGrid cell tileHeight public void Init(Cell[,] mapGrid) { gridSize.x = mapGrid.GetUpperBound(0); gridSize.y = mapGrid.GetUpperBound(1); GetComponent <MeshFilter>().mesh = mesh = new Mesh(); mesh.name = "Procedural Grid"; mesh.Clear(); vertices = new Vector3[(gridSize.x + 1) * (gridSize.y + 1) * 5]; triangles = new int[gridSize.x * gridSize.y * 6]; // 9 vertices per cell float cellOffset = cellSize * 0.5f; for (int i = 0, y = 0; y <= gridSize.y; y++) { for (int x = 0; x <= gridSize.x; x++, i++) { float h = mapGrid[x, y].TileHeight; int tileType = mapGrid[x, y].Type; if (h == 0 && tileType == 0) { // Water vertices[i] = new Vector3(x, h - 2f, y);//e } else if (h == 0 && tileType == 1) { // Ground level vertices[i] = new Vector3(x, h, y); } if (h == 1 && tileType == 1) { // Height 1 vertices[i] = new Vector3(x, h * 2, y); } if (h == 2 && tileType == 1) { // Height 2 vertices[i] = new Vector3(x, h * 2, y); } } } mesh.vertices = vertices; for (int ti = 0, vi = 0, y = 0; y < gridSize.y; y++, vi++) { for (int x = 0; x < gridSize.x; x++, ti += 6, vi++) { triangles[ti] = vi; triangles[ti + 3] = triangles[ti + 2] = vi + 1; triangles[ti + 4] = triangles[ti + 1] = vi + gridSize.x + 1; triangles[ti + 5] = vi + gridSize.x + 2; } } Debug.Log("Triangles length: " + triangles.Length); mesh.triangles = triangles; mesh.RecalculateNormals(); }
public int Count(Cell[,] cells, Cell cell) { int livingNeighbors = 0; int maxXIndex = cells.GetUpperBound(0); int maxYIndex = cells.GetUpperBound(1); if (maxXIndex < 2 || maxYIndex < 2) { throw new ArgumentException("The minimum array size is 3x3."); } for (int xCoord = cell.Location.X - 1; xCoord <= cell.Location.X + 1; xCoord++) { for (int yCoord = cell.Location.Y - 1; yCoord <= cell.Location.Y + 1; yCoord++) { if (xCoord == cell.Location.X && yCoord == cell.Location.Y || xCoord < 0 || xCoord > maxXIndex || yCoord < 0 || yCoord > maxYIndex || !cells[xCoord, yCoord].IsAlive) { continue; } livingNeighbors++; } } return(livingNeighbors); }
public static void DrawBoard(GameBoard gameBoard) { Cell[,] board = gameBoard.GetCells(); var width = board.GetUpperBound(0) + 1; // 0 - of first array (of x) var height = board.GetUpperBound(1) + 1; // 1 - of second array (of y) for (int columns = 0; columns < width; columns++) { //TODO add ident. numbers of fields Console.Write("+---+"); } Console.WriteLine(); for (int rows = 0; rows < height; rows++) { for (int columns = 0; columns < width; columns++) { Console.Write($"| {PrintCellState(board[columns,rows].GetCellState())} |"); } Console.WriteLine(); for (int columns = 0; columns < width; columns++) { Console.Write($"+---+"); } Console.WriteLine(); } }
/// <summary> /// Works out where ships can potentially be placed, depending on available space. /// </summary> /// <param name="cells">The cells to choose from</param> /// <param name="col">The column to look at</param> /// <param name="row">The row to look at</param> /// <param name="moveAcross">True of looking at horizontal space, otherwise false.</param> /// <returns></returns> private int GetAvailableStartingCells(Cell[,] cells, int col, int row, bool moveAcross) { // Check the bounds if (col > cells.GetUpperBound(0) || row > cells.GetUpperBound(1)) { return(0); } // Get the cell - if already assigned to ship, then don't continue. var cell = cells[col, row]; if (cell.Ship != null) { return(0); } // Recursively work out the next space, whether up or down. if (moveAcross) { return(1 + GetAvailableStartingCells(cells, ++col, row, moveAcross)); } else { return(1 + GetAvailableStartingCells(cells, col, ++row, moveAcross)); } }
public Cell[,] RotateBoardCounterClockwise(Cell[,] srcArray) { int width; int height; Cell[,] dst; width = srcArray.GetUpperBound(0) + 1; height = srcArray.GetUpperBound(1) + 1; dst = new Cell[height, width]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int newRow; int newCol; newRow = width - (col + 1); newCol = row; dst[newCol, newRow] = srcArray[col, row]; } } return(dst); }
public Cell[,] RotateBoardClockwise(Cell[,] srcArray) { int width; int height; Cell[,] dst; height = srcArray.GetUpperBound(0) + 1; width = srcArray.GetUpperBound(1) + 1; dst = new Cell[width, height]; for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { int newRow; int newCol; newRow = col; newCol = height - (row + 1); //dst[newCol, newRow] = srcArray[col, row]; dst[newRow, newCol] = srcArray[row, col]; dst[newRow, newCol]._row = newRow; dst[newRow, newCol]._col = newCol; } } return(dst); }
/// <summary> /// Generates walls in the complete labyrinth /// </summary> private List <Vector3> FillWalls() { var result = new List <Vector3>(); var wallsParrent = new GameObject { name = PARENT_WALLS }; wallsParrent.transform.SetParent(_parent); for (var x = 0; x < _cells.GetUpperBound(0); x++) { for (var z = 0; z < _cells.GetUpperBound(1); z++) { if (CheckCellInArray(x, z) && !_cells[x, z].IsBusy) { var position = SetNewPositionForWall(x, z); result.Add(position); var randIndex = Random.Range(0, _wallPrefubs.Length); var wall = Object.Instantiate(_wallPrefubs[randIndex], position, Quaternion.identity); //var wall = PrefabUtility.InstantiatePrefab (_wallPrefubs[randIndex], SceneManager.GetActiveScene()) as GameObject; wall.transform.SetPositionAndRotation(position, Quaternion.identity); wall.transform.Rotate(Vector3.up, Random.Range(0, 4) * 90f); wall.transform.SetParent(wallsParrent.transform); _cells[x, z].Position = position; _cells[x, z].IsBusy = true; } } } return(result); }
private void Awake() { emptyCellObjectPooler = new ObjectPooler(emptyCellPrefab); treeCellObjectPooler = new ObjectPooler(treeCellPrefab); burningCellObjectPooler = new ObjectPooler(burningCellPrefab); map = new Cell[width, height]; gameObjectMap = new GameObject[width, height]; InitTerrain(); var planeElevation = new Vector3(0, yPlaneElevation, 0); gamePlane = new Plane(planeElevation, planeElevation + Vector3.right, planeElevation + Vector3.forward); for (int x = 0; x <= map.GetUpperBound(0); x++) { for (int y = 0; y <= map.GetUpperBound(1); y++) { map[x, y] = Cell.Empty; ChangeGameObject(map, x, y); } } isRunning = true; if (ui != null) { myUi = ui.GetComponent <MyUi>(); } StartCoroutine(RunningAutomata()); }
public Vector2Int GetNearestCellID(Vector3 point) { //Make sure the point is in the grid point.x = Mathf.Clamp( point.x, AStarTerrain.position.x - realExtents.x, AStarTerrain.position.x + realExtents.x); point.y = Mathf.Clamp( point.y, AStarTerrain.position.y - realExtents.y, AStarTerrain.position.y + realExtents.y); point.z = Mathf.Clamp( point.z, AStarTerrain.position.z - realExtents.z, AStarTerrain.position.z + realExtents.z); var lastCell = gridCells[gridCells.GetUpperBound(0), gridCells.GetUpperBound(1)]; var shorterDistance = Vector3.SqrMagnitude( lastCell.Position - gridCells[0, 0].Position); var closestColumn = 0; for (var i = 0; i < GridSize.x + 1; i++) { var distance = Vector3.SqrMagnitude(gridCells[i, 0].Position - point); if (distance < shorterDistance) { shorterDistance = distance; closestColumn = i; } else { break; } } var closestRow = 0; for (var j = 0; j < GridSize.y; j++) { var distance = Vector3.SqrMagnitude( gridCells[closestColumn, j].Position - point); if (distance <= shorterDistance) { shorterDistance = distance; closestRow = j; } else { break; } } return(new Vector2Int { x = closestColumn, y = closestRow }); }
public static Cell GetCell(this Cell[,] grid, int col, int row) { int gridWidth = grid.GetUpperBound(0); int gridHeight = grid.GetUpperBound(1); return((col > gridWidth || col < 0 || row > gridHeight || row < 0) ? new Cell(CellType.None) : grid[col, row]); }
public void LoadContent(ContentManager content) { for (int x = 0; x < cells.GetUpperBound(0); x++) { for (int y = 0; y < cells.GetUpperBound(1); y++) { cells[x, y].LoadTexture(content, x, y); } } }
/// <summary> /// Returns 1 if map[x, y] are out of bounds of map, else returns map[x, y] /// </summary> /// <param name="map"></param> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public static int SafeMapValue(Cell[,] map, int x, int y) { if (x < 0 || x > map.GetUpperBound(0) || y < 0 || y > map.GetUpperBound(1)) { return(CellularAutomataGenerator.instance.wallThreshold); } else { return(map[x, y].value); } }
private static char PrintSafeChar(Cell[,] cells, int x, int y) { if (x > -1 && y > -1 && y < (cells.GetUpperBound(1) + 1) && x < (cells.GetUpperBound(0) + 1)) { return(cells[x, y].Print()); } else { return(OutOfBoundsChar); } }
private static char PrintSafeChar(int x, int y) { if (x > -1 && y > -1 && y < (_cells.GetUpperBound(1) + 1) && x < (_cells.GetUpperBound(0) + 1)) { return(_cells[x, y].Print()); } else { return(OutsideBoundsToken); } }
private void DebugField() { for (int z = 0; z < _field.GetUpperBound(1) + 1; z++) { string s = ""; for (int x = 0; x < _field.GetUpperBound(0) + 1; x++) { s += " " + _field[x, z]; } Debug.Log(s); } }
/// <summary> /// Returns a reference to the cell at location [row,col]. If not in bounds, throws IndexOutOfRangeException /// </summary> /// <param name="row"></param> /// <param name="col"></param> /// <returns></returns> public AbstractCell GetCell(int row, int col) { if (row >= _spreadsheet.GetLowerBound(_row_dim) && row <= _spreadsheet.GetUpperBound(_row_dim) && col >= _spreadsheet.GetLowerBound(_col_dim) && col <= _spreadsheet.GetUpperBound(_col_dim)) { return(_spreadsheet[row, col]); } else { throw new IndexOutOfRangeException(); } }
public static void Print(Cell[,] cells, CellPrintFunction cellPrintFn) { int u1 = cells.GetUpperBound(0); int u2 = cells.GetUpperBound(1); int l1 = cells.GetLowerBound(0); int l2 = cells.GetLowerBound(1); string[,] printArray = new string[u1 + 2, u2 + 2]; setPrintContent(cells, cellPrintFn, u1, u2, l1, l2, printArray); setPrintRowHeading(printArray); setPrintColHeading(printArray); printIt(printArray); }
/// <summary> /// Prints the grid in its current state, or revealing all if showAll is true /// </summary> /// <param name="cells"></param> /// <param name="showAll">Shows all cells as if all were uncovered</param> public static void Print(Cell[,] cells, bool showAll = false) { for (int y = 0; y <= cells.GetUpperBound(1); y++) { for (int x = 0; x <= cells.GetUpperBound(0); x++) { GetSprite(cells[x, y], showAll).Draw(); } Console.ResetColor(); Console.Write('\n'); } Console.ResetColor(); }
private void DisplayMaze(Cell[,] nodes) // Affiche la carte { int hgt = nodes.GetUpperBound(0) + 1; int wid = nodes.GetUpperBound(1) + 1; bm = new Bitmap( picMaze.ClientSize.Width, picMaze.ClientSize.Height); using (Graphics gr = Graphics.FromImage(bm)) { gr.SmoothingMode = SmoothingMode.AntiAlias; for (int r = 0; r < hgt; r++) { for (int c = 0; c < wid; c++) { nodes[r, c].FillRectangleWithImage(gr, herbe); if (nodes[r, c].getCellDep() == true) { nodes[r, c].FillRectangleWithImage(gr, depart); startCell = nodes[r, c]; } if (nodes[r, c].getCellEnd() == true) { SolidBrush brushend = new SolidBrush(Color.Red); nodes[r, c].FillRectangle(gr, brushend); nodes[r, c].FillRectangleWithImage(gr, fin); endCell = nodes[r, c]; } if (nodes[r, c].getTraversable() == false) { nodes[r, c].FillRectangleWithImage(gr, mur); } if (nodes[r, c].getBoue() == true) { nodes[r, c].FillRectangleWithImage(gr, boue); } if (nodes[r, c].getPiege() == true) { nodes[r, c].FillRectangleWithImage(gr, piege); } } } } // label3.Text = StraightLineDistanceTo(nodes[2, 6]).ToString(); picMaze.Image = bm; }
/* * Visualisation of what grid looks like after generation * * 0 0 0 0 0 0 0 0 0 0 * 0 0 1 1 0 1 0 0 0 0 * 0 1 0 0 1 0 1 1 1 0 * 0 1 0 1 4 0 1 1 1 0 * 0 0 1 2 2 3 1 0 0 0 * 0 0 0 0 1 0 0 0 1 1 * 0 0 0 0 0 0 0 0 0 0 * */ public static Cell[,] CheckWalkable(Cell[,] mapGrid) { for (int x = 0; x < mapGrid.GetUpperBound(0); x++) { for (int y = 0; y < mapGrid.GetUpperBound(1); y++) { if (mapGrid[x, y].Type >= 1) { mapGrid[x, y].Walkable = true; } } } return(mapGrid); }
public CellularAutomata2D(int N, int M) { cells = new Cell[N, M]; ibound = cells.GetUpperBound(0); jbound = cells.GetUpperBound(1); // getCellStateDelegate = this.getCellstate; for (int i = 0; i < ibound + 1; i++) { for (int j = 0; j < jbound + 1; j++) { cells[i, j] = new Cell(i, j, CellState.dead); } } }
/// <summary> /// Проверяет возможность совершения хода /// </summary> /// <param name="parSourceVerticalCoord">Индекс ряда исходной ячейки</param> /// <param name="parSourceHorizontalCoord">Индекс колонки исходной ячейки</param> /// <param name="parDestinationVerticalCoord">Индекс ряда ячейки назначения</param> /// <param name="parDestinationHorizontalCoord">Индекс колонки ячейки назначения</param> /// <returns>Признак возможности совершения хода</returns> public bool IsMove(int parSourceVerticalCoord, int parSourceHorizontalCoord, int parDestinationVerticalCoord, int parDestinationHorizontalCoord) { if (parDestinationHorizontalCoord < 0 || parDestinationVerticalCoord < 0 || parSourceVerticalCoord < 0 || parSourceHorizontalCoord < 0) { return(false); } if (parDestinationVerticalCoord > _cells.GetUpperBound(0) || parDestinationHorizontalCoord > _cells.GetUpperBound(1) || parSourceVerticalCoord > _cells.GetUpperBound(0) || parSourceHorizontalCoord > _cells.GetUpperBound(1)) { return(false); } if (_cells[parDestinationVerticalCoord, parDestinationHorizontalCoord] == null || _cells[parSourceVerticalCoord, parSourceHorizontalCoord] == null) { return(false); } if ((parSourceHorizontalCoord + 1 == parDestinationHorizontalCoord) && (parSourceVerticalCoord == parDestinationVerticalCoord) || (parSourceHorizontalCoord - 1 == parDestinationHorizontalCoord) && (parSourceVerticalCoord == parDestinationVerticalCoord)) { return(true); } if ((parSourceVerticalCoord + 1 == parDestinationVerticalCoord) && (parSourceHorizontalCoord == parDestinationHorizontalCoord) || (parSourceVerticalCoord - 1 == parDestinationVerticalCoord) && (parSourceHorizontalCoord == parDestinationHorizontalCoord)) { return(true); } if ((parSourceVerticalCoord % 2 == 0) && (parSourceHorizontalCoord - 1 == parDestinationHorizontalCoord || parSourceHorizontalCoord == parDestinationHorizontalCoord)) { return(true); } if ((parSourceVerticalCoord % 2 == 1) && (parSourceHorizontalCoord + 1 == parDestinationHorizontalCoord || parSourceHorizontalCoord == parDestinationHorizontalCoord)) { return(true); } return(false); }
private bool MatchState(Cell[,] state1, Cell[,] state2) { for (int i = 0; i <= state1.GetUpperBound(0); i++) { for (int j = 0; j <= state1.GetUpperBound(1); j++) { if (state1[i, j].IsLive != state2[i, j].IsLive) { return(false); } } } return(true); }
private void convertGridToList() { var boundX = grid.GetUpperBound(0); var boundY = grid.GetUpperBound(1); var count = 0; for (var y = 0; y <= boundY; y++) { for (var x = 0; x <= boundX; x++) { generatedMap.cells[count] = grid[x, y]; count++; } } }
private void CopyCellColumns(ref Cell[,] from, ref Cell[,] to, int columnIndex) { for (int rowIndex = 0; rowIndex <= from.GetUpperBound(1); ++rowIndex) { CopyCells(ref from, ref to, columnIndex, rowIndex); } }
private void InitCells(Cell[,] cells) { for (int columnIndex = 0; columnIndex <= cells.GetUpperBound(0); ++columnIndex) { InitCellColumn(cells, columnIndex); } }