Skip to content

Instantly share code, notes, and snippets.

@lennartb-
Last active May 17, 2023 03:39
Show Gist options
  • Save lennartb-/7482783 to your computer and use it in GitHub Desktop.
Save lennartb-/7482783 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.
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);
}
}
}
}
@dizingof
Copy link

dizingof commented Mar 5, 2020

How can realize this through objects?

@Melodi17
Copy link

Melodi17 commented Aug 4, 2021

The render method is more effective if written to a string then released at once

private void DrawGame()
        {
            string buffer = "";
            for (int i = 0; i < Heigth; i++) {
                for (int j = 0; j < Width; j++) {
                    buffer += cells[i, j] ? "x" : " ";
                }

                buffer += "\n";
            }
            Console.SetCursorPosition(0, Console.WindowTop);
            Console.Write(buffer.TrimEnd('\n'));
        }

@Fantoom
Copy link

Fantoom commented Sep 20, 2021

there is a typo in "Heigth"

@AmirhossainMohammadi
Copy link

kossher :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment