private int GetTopMiddleNeighbors(Grid grid) { var livingNeighbors = 0; if (grid.GetCellAt(Position.X + 1, Position.Y).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X + 1, Position.Y + 1).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X, Position.Y + 1).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X - 1, Position.Y + 1).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X - 1, Position.Y).IsAlive) { livingNeighbors++; } return(livingNeighbors); }
private void InitGameBoard() { grid = new Grid(GridSize, GridSize); cellpPanels = new Panel[GridSize, GridSize]; _gameBoard.Width = GridSize * TileSize + (6 * (GridSize + 1)); _gameBoard.Height = GridSize * TileSize + (6 * (GridSize + 1)); for (var n = 0; n < grid.Rows.Count; n++) { for (var m = 0; m < grid.Rows[n].Cells.Count; m++) { var newPanel = new Panel { Size = new Size(TileSize, TileSize), Location = new Point(TileSize * n, TileSize * m) }; _gameBoard.Controls.Add(newPanel); cellpPanels[m, n] = newPanel; newPanel.BackColor = grid.GetCellAt(m, n).IsAlive ? _liveColor : _deadColor; } } PatternGenerator.InsertRandomLiveCells(grid); Repaint(); }
private int GetBottomRightNeighbors(Grid grid) { var livingNeighbors = 0; if (grid.GetCellAt(Position.X - 1, Position.Y - 1).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X, Position.Y - 1).IsAlive) { livingNeighbors++; } if (grid.GetCellAt(Position.X - 1, Position.Y).IsAlive) { livingNeighbors++; } return(livingNeighbors); }
public void Repaint() { for (var n = 0; n < grid.Rows.Count; n++) { for (var m = 0; m < grid.Rows[n].Cells.Count; m++) { cellpPanels[m, n].BackColor = grid.GetCellAt(m, n).IsAlive ? _liveColor : _deadColor; } } _gameBoard.Refresh(); }