private Cell UpdateCell(PositionInt position, Cell[][] cells, GameOfLifeRules rules) { Cell result = new Cell(); result.isAlive = false; int numberOfAliveNeighbours = 0; // cell position relative to the current cell, // find living neighbours for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (!((x == 0) && (y == 0))) // if not at the center { // position will then be normalized in Cell.GetByPosition() to create an 'endless' plate PositionInt neighbourPosition = new PositionInt(position.GetX() + x, position.GetY() + y); Cell neighbourCell = Cell.GetByPosition(ref neighbourPosition, cells); if (neighbourCell.isAlive) { numberOfAliveNeighbours++; } } } } if (CompareWithRule(numberOfAliveNeighbours, rules.neighbours_ToCreate)) { result.isAlive = true; } if (CompareWithRule(numberOfAliveNeighbours, rules.neighbours_ToSurvive)) { result.isAlive = true; } else if (CompareWithRule(numberOfAliveNeighbours, rules.neighbours_ToDie)) { result.isAlive = false; } return(result); }
public GameOfLifePlate(GameOfLifeRules rules) { cells = new Cell[rules.plateSize.x][]; for (int x = 0; x < cells.Length; x++) { cells[x] = new Cell[rules.plateSize.y]; for (int y = 0; y < cells[x].Length; y++) { cells[x][y] = new Cell(); } } this.rules = rules; foreach (PositionInt liveCellPosition in rules.initialLiveCells) { PositionInt position = liveCellPosition; Cell cell = Cell.GetByPosition(ref position, cells); cell.isAlive = true; } }
protected void RunButton_Click(object sender, EventArgs e) { GameOfLifeRules gameRules = Newtonsoft.Json.JsonConvert.DeserializeObject <GameOfLifeRules>(textBox.Text); PerformCalculations(gameRules); }
public async void PerformCalculations(GameOfLifeRules gameRules = null) { runButton.Enabled = false; GameOfLifeRules rules = new GameOfLifeRules(); if (gameRules == null) { rules.plateSize = new PositionInt(60, 60); rules.initialLiveCells = new PositionInt[] { new PositionInt(1, 0), new PositionInt(2, 0), new PositionInt(0, 1), new PositionInt(0, 2), new PositionInt(0, 3), new PositionInt(1, 2), new PositionInt(1, 4), new PositionInt(2, 4), new PositionInt(3, 1), new PositionInt(3, 3), new PositionInt(4, 2), new PositionInt(5, 2), new PositionInt(6, 2), new PositionInt(7, 2), new PositionInt(8, 2), new PositionInt(7, 1), new PositionInt(7, 3) }; for (int i = 0; i < rules.initialLiveCells.Length; i++) { rules.initialLiveCells[i] = new PositionInt( rules.initialLiveCells[i].GetX() + (rules.plateSize.GetX() / 2), rules.initialLiveCells[i].GetY() + (rules.plateSize.GetY() / 2)); } //rules.initialLiveCells = new PositionInt[] { new PositionInt(0, 0), new PositionInt(1, 0), new PositionInt(2, 0) }; rules.neighbours_ToSurvive = new int[] { 2, 3 }; rules.neighbours_ToDie = new int[] { 0, 1, 4, 5, 6, 7, 8 }; rules.neighbours_ToCreate = new int[] { 3 }; rules.numberOfIterations = 50; rules.delay = 70; defaultRules = rules; } else { rules = gameRules; } textBox.Height = picture.Size.Height; textBox.Width = picture.Size.Width; textBox.Text = JsonConvert.SerializeObject(rules, Formatting.Indented); GameOfLifePlate plate = new GameOfLifePlate(rules); for (int i = 0; i < rules.numberOfIterations; i++) { List <GraphPointArray> cells = new List <GraphPointArray>(); int x = 0; foreach (GameOfLifePlate.Cell[] cellRow in plate.GetCells()) { int y = 0; foreach (GameOfLifePlate.Cell thisCell in cellRow) { Color cellColor = Color.Black; if (thisCell.isAlive) { cellColor = Color.White; } cells.Add(new GraphPointArray(new List <Position> { new Position(x, y) }, cellColor)); y++; } x++; } if (graph == null) { graph = new Graph( 500, 500, 4, cells.ToArray(), Color.FromArgb(70, 70, 70), $"", new Size(1, 1), false ); graph.StartForm(); } picture.Image = (Image)graph.PaintGraph(); graph.SetGraphPointArray(cells.ToArray()); plate.NextIteration(); picture.Invalidate(); picture.Update(); picture.Refresh(); await Task.Delay(rules.delay); } runButton.Enabled = true; }
public void SetRules(GameOfLifeRules rules) { this.rules = rules; }