-
-
Save MrChrisWeinert/fb23e31133f8b07ea608cb794f5cde19 to your computer and use it in GitHub Desktop.
Conway's Game of Life in C# (Console App) with basic arrays and without any fancy complicated stuff. No error checking included.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace GameOfLife { | |
public class LifeSimulation { | |
private int Heigth; | |
private int Width; | |
private bool[,] cells; | |
/// <summary> | |
/// Initializes a new Game of Life. | |
/// </summary> | |
/// <param name="Heigth">Heigth of the cell field.</param> | |
/// <param name="Width">Width of the cell field.</param> | |
public LifeSimulation(int Heigth, int Width) { | |
this.Heigth = Heigth; | |
this.Width = Width; | |
cells = new bool[Heigth, Width]; | |
GenerateField(); | |
} | |
/// <summary> | |
/// Advances the game by one generation and prints the current state to console. | |
/// </summary> | |
public void DrawAndGrow() { | |
DrawGame(); | |
Grow(); | |
} | |
/// <summary> | |
/// Advances the game by one generation according to GoL's ruleset. | |
/// </summary> | |
private void Grow() { | |
for (int i = 0; i < Heigth; i++) { | |
for (int j = 0; j < Width; j++) { | |
int numOfAliveNeighbors = GetNeighbors(i, j); | |
if (cells[i, j]) { | |
if (numOfAliveNeighbors < 2) { | |
cells[i, j] = false; | |
} | |
if (numOfAliveNeighbors > 3) { | |
cells[i, j] = false; | |
} | |
} | |
else { | |
if (numOfAliveNeighbors == 3) { | |
cells[i, j] = true; | |
} | |
} | |
} | |
} | |
} | |
/// <summary> | |
/// Checks how many alive neighbors are in the vicinity of a cell. | |
/// </summary> | |
/// <param name="x">X-coordinate of the cell.</param> | |
/// <param name="y">Y-coordinate of the cell.</param> | |
/// <returns>The number of alive neighbors.</returns> | |
private int GetNeighbors(int x, int y) { | |
int NumOfAliveNeighbors = 0; | |
for (int i = x - 1; i < x + 2; i++) { | |
for (int j = y - 1; j < y + 2; j++) { | |
if (!((i < 0 || j < 0) || (i >= Heigth || j >= Width))) { | |
if (cells[i, j] == true) NumOfAliveNeighbors++; | |
} | |
} | |
} | |
return NumOfAliveNeighbors; | |
} | |
/// <summary> | |
/// Draws the game to the console. | |
/// </summary> | |
private void DrawGame() { | |
for (int i = 0; i < Heigth; i++) { | |
for (int j = 0; j < Width; j++) { | |
Console.Write(cells[i, j] ? "x" : " "); | |
if (j == Width - 1) Console.WriteLine("\r"); | |
} | |
} | |
Console.SetCursorPosition(0, Console.WindowTop); | |
} | |
/// <summary> | |
/// Initializes the field with random boolean values. | |
/// </summary> | |
private void GenerateField() { | |
Random generator = new Random(); | |
int number; | |
for (int i = 0; i < Heigth; i++) { | |
for (int j = 0; j < Width; j++) { | |
number = generator.Next(2); | |
cells[i, j] = ((number == 0) ? false : true); | |
} | |
} | |
} | |
} | |
internal class Program { | |
// Constants for the game rules. | |
private const int Heigth = 10; | |
private const int Width = 30; | |
private const uint MaxRuns = 100; | |
private static void Main(string[] args) { | |
int runs = 0; | |
LifeSimulation sim = new LifeSimulation(Heigth, Width); | |
while (runs++ < MaxRuns) { | |
sim.DrawAndGrow(); | |
// Give the user a chance to view the game in a more reasonable speed. | |
System.Threading.Thread.Sleep(100); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment