|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace ConsoleLife |
| 8 | +{ |
| 9 | + public class GameEngine |
| 10 | + { |
| 11 | + public uint CurrentGeneration { get; private set; } |
| 12 | + private bool[,] field; |
| 13 | + private readonly int rows; |
| 14 | + private readonly int cols; |
| 15 | + |
| 16 | + public GameEngine(int rows, int cols, int density) |
| 17 | + { |
| 18 | + this.rows = rows; |
| 19 | + this.cols = cols; |
| 20 | + field = new bool[cols, rows]; |
| 21 | + Random random = new Random(); |
| 22 | + for (int x = 0; x < cols; x++) |
| 23 | + { |
| 24 | + for (int y = 0; y < rows; y++) |
| 25 | + { |
| 26 | + field[x, y] = random.Next(density) == 0; |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + public void NextGeneration() |
| 31 | + { |
| 32 | + var newField = new bool[cols, rows]; |
| 33 | + for (int x = 0; x < cols; x++) |
| 34 | + { |
| 35 | + for (int y = 0; y < rows; y++) |
| 36 | + { |
| 37 | + var neighboursCount = CountNeighbours(x, y); |
| 38 | + var hasLife = field[x, y]; |
| 39 | + if (!hasLife && neighboursCount == 3) |
| 40 | + { |
| 41 | + newField[x, y] = true; |
| 42 | + } |
| 43 | + else if (hasLife && neighboursCount < 2 || neighboursCount > 3) |
| 44 | + { |
| 45 | + newField[x, y] = false; |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + newField[x, y] = field[x, y]; |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + field = newField; |
| 54 | + CurrentGeneration++; |
| 55 | + } |
| 56 | + public bool[,] GetCurrentGeneration() |
| 57 | + { |
| 58 | + var result = new bool[cols, rows]; |
| 59 | + for (int x = 0; x < cols; x++) |
| 60 | + { |
| 61 | + for (int y = 0; y < rows; y++) |
| 62 | + { |
| 63 | + result[x, y] = field[x, y]; |
| 64 | + } |
| 65 | + } |
| 66 | + return result; |
| 67 | + } |
| 68 | + private int CountNeighbours(int x, int y) |
| 69 | + { |
| 70 | + int count = 0; |
| 71 | + for (int i = -1; i < 2; i++) |
| 72 | + { |
| 73 | + for (int j = -1; j < 2; j++) |
| 74 | + { |
| 75 | + var col = (x + i + cols) % cols; |
| 76 | + var row = (y + j + rows) % rows; |
| 77 | + var isSelfChecking = col == x && row == y; |
| 78 | + var hasLife = field[col, row]; |
| 79 | + if (hasLife && !isSelfChecking) |
| 80 | + { |
| 81 | + count++; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + } |
| 86 | + return count; |
| 87 | + } |
| 88 | + private bool ValidateCellPosition(int x, int y) |
| 89 | + { |
| 90 | + return x >= 0 && y >= 0 && x < cols && y < rows; |
| 91 | + } |
| 92 | + private void UpdateCell(int x, int y, bool state) |
| 93 | + { |
| 94 | + if (ValidateCellPosition(x, y)) |
| 95 | + { |
| 96 | + field[x, y] = state; |
| 97 | + } |
| 98 | + } |
| 99 | + public void AddCell(int x, int y) |
| 100 | + { |
| 101 | + UpdateCell(x, y, state: true); |
| 102 | + } |
| 103 | + public void RemoveCell(int x, int y) |
| 104 | + { |
| 105 | + UpdateCell(x, y, state: false); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments