Rules (from Baidu Encyclopedia, Conway Life Game Entry)
At the beginning of the game, each cell is randomly set to a certain state of one of "life" or "death". Then, according to some rules, the status of each cell in the next generation is calculated and the life and death distribution map of the next generation of cells is drawn.
What kind of iteration rules should be stipulated? A simple law of survival that reflects both coordination and competition between life. For simplicity, the most basic consideration is to assume that each cell follows the exact same law of survival; further, limit the mutual influence between cells to only the 8 neighbors closest to the cell.
That is to say, the state after each cell iterates is determined by the state of the cell and the surrounding 8 cells. After these restrictions are made, there are still many ways to specify the specific details of the "law of survival". For example, in Conway's life game, the following law of survival is stipulated.
(1) When the current cell is in a dead state, when there are 3 surviving cells around it, the cell becomes a living state after iteration (simulated reproduction); if it was originally alive, it remains unchanged.
(2) When the current cell is in a state of survival, when the surrounding neighbor cells are less than two (not including two) survive, the cell becomes dead (sparse in simulated life).
(3) When the current cell is in a viable state, the cell remains as it is when there are two or three viable cells around it.
(4) When the current cell is in a state of survival, when there are more than 3 surviving cells around, the cell becomes dead (simulating that there are too many lifespans).
Key interfaces implemented by console
Functions to set console cursor: public static void SetCursorPosition (int left, int top); where the left parameter is a column and the top parameter is a row.
Set the properties of the console background color: public static ConsoleColor BackgroundColor { get; set; } Black is used to represent the surviving cells, and white is used to represent the surviving cells.
Code implementation
using System; using ; namespace CellularAutomata { class Program { private static int gridRowCol = 32; private static Cell[,] grid = new Cell[gridRowCol, gridRowCol]; private static int sleepMs = 33; private static int initAlivePossibility = 4; // 4 means 1/4 static void Main(string[] args) { try { Init(); // Main loop while (true) { Update(); (sleepMs); } } catch (Exception e) { (e); (); } } private static void Init() { // Set Console Size = 256; = 256; = 256; = 80; Random random = new Random(); for (int i = 0; i < (0); i++) { for (int j = 0; j < (1); j++) { grid[i, j] = new Cell(); int value = (0, initAlivePossibility); if (value == 0) { grid[i, j].Value = true; } else { grid[i, j].Value = false; } } } } private static void Update() { for (int i = 0; i < (0); i++) { for (int j = 0; j < (1); j++) { int aliveCount = NeighborAliveCount(i, j); if (grid[i, j].Value) // Alive { if (aliveCount < 2 || aliveCount > 3) { grid[i, j].Value = false; } } else // Death { if (aliveCount == 3) { grid[i, j].Value = true; } } if (grid[i, j].Value) { SetAlive(i, j); } else { SetDeath(i, j); } } } } private static int NeighborAliveCount(int i, int j) { int count = 0; for (int m = i - 1; m <= i + 1; m++) { for (int n = j - 1; n <= j + 1; n++) { if (m == i && n == j) continue; if (m < 0 || m >= (0)) continue; if (n < 0 || n >= (1)) continue; if (grid[m, n].Value) count++; } } return count; } private static void SetAlive(int i, int j) { string aliveStr = " "; (j * , i); = ; (aliveStr); } private static void SetDeath(int i, int j) { string deathStr = " "; (j * , i); = ; (deathStr); } } public class Cell { public bool Value { get; set; } } }
Complete code:/jingjiangtao/CellularAutomata
The Cell class is a cell class, which has a bool attribute Value, true means survival, and false means death. Writing cells individually into classes instead of a bool value is for subsequent possible extensions.
The grid variable is a two-dimensional array that represents the grid. The size can be set by gridRowCol. The default is 32 and should not be too large.
The sleepMs variable is the interval between loops, the unit is milliseconds, and the default is 33ms.
The initAlivePossibility variable determines the probability of survival of cells in the lattice when initializing. The calculation method is 1/initAlivePossibility. For example, initAlivePossibility=4, which means that the survival probability of each cell during initialization is 1/4.
The cell and console sizes in the grid are initialized in the Main() function. The step of setting the console size may throw an out-of-bounds exception. If this value occurs, it needs to be modified. Next is the main loop, and the interval between each loop is sleepMs.
Update() is a function that implements rules.
The NeighborAliveCount() function obtains the number of survival of adjacent cells of the specified cell.
The SetAlive() function and the SetDeath() function set the display on the console.
If there is any error, please correct me, thank you!
The above is the detailed content of the example of C# implementing Conway Life Game (Cell Automator). For more information about C# implementing Conway Life Game, please pay attention to my other related articles!