Skip to content

Commit

Permalink
Add project files.
Browse files Browse the repository at this point in the history
  • Loading branch information
bjartelund committed Mar 16, 2023
1 parent afc22e3 commit f9da366
Show file tree
Hide file tree
Showing 3 changed files with 234 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ConwayGameOfLife.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions ConwayGameOfLife.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33414.496
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConwayGameOfLife", "ConwayGameOfLife.csproj", "{C76CEFAD-BB0B-456D-A698-AAA664074D1B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C76CEFAD-BB0B-456D-A698-AAA664074D1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C76CEFAD-BB0B-456D-A698-AAA664074D1B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C76CEFAD-BB0B-456D-A698-AAA664074D1B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C76CEFAD-BB0B-456D-A698-AAA664074D1B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93FEB115-E5DE-4787-AFBD-3CC154A20670}
EndGlobalSection
EndGlobal
199 changes: 199 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
namespace GameOfLife
{
static class Program
{
// Define the size of the grid
const int Rows = 20;
const int Cols = 40;
static int changes = 0;

static int generation = 0;
// Create a random number generator
static Random random = new Random();

// Create a boolean array to store the state of each cell
static bool[,] grid = new bool[Rows, Cols];

static void Main(string[] args)
{
// Initialize the grid with random values
InitializeGrid();

// Start an infinite loop
while (true)
{
// Display the grid on the console
DisplayGrid();

// Update the grid according to the rules
UpdateGrid();

// Wait for some time before repeating
Thread.Sleep(500);
generation++;
}
}

static void InitializeGrid()
{
// Loop through every cell in the grid
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
// Assign a random value (true or false) to each cell
grid[i, j] = random.Next(2) == 0;
}
}
}

static void DisplayGrid()
{
// Clear the console screen
Console.Clear();
Console.WriteLine($"Generation {generation} - Changed cells {changes}");
// Loop through every cell in the grid
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
// If the cell is alive, display a star (*)
if (grid[i, j])
{
Console.Write("*");
}
else // If the cell is dead, display a space ( )
{
Console.Write(" ");
}
}

// Move to the next line after each row
Console.WriteLine();
}
}

static void UpdateGrid()
{
// Create a temporary array to store the new state of each cell
bool[,] newGrid = new bool[Rows, Cols];

// Loop through every cell in the grid
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
// Count how many live neighbours the current cell has
int liveNeighbours = CountLiveNeighbours(i, j);

// Apply the rules of life to determine if the current cell lives or dies

if (grid[i, j])
{
if (liveNeighbours < 2 || liveNeighbours > 3)
{
newGrid[i, j] = false;
}
else
{
newGrid[i, j] = true;
}
}

else
{
if (liveNeighbours == 3)
{
newGrid[i, j] = true;
}
else
{
newGrid[i, j] = false;
}
}
}
}

changes=CountChanges(grid, newGrid);
// Copy the temporary array to the original grid
Array.Copy(newGrid, grid, Rows * Cols);
}

// A helper method to count how many live neighbours a given cell has
static int CountLiveNeighbours(int x, int y)
{
// Initialize a counter variable
int count = 0;

// Loop through all adjacent cells in a 3x3 square around the given cell
for (int i = -1; i <= 1; i++)
{
for (int j = -1; j <= 1; j++)
{
// Skip the current cell itself
if (i == 0 && j == 0)
{
continue;
}

// Calculate the coordinates of the neighbour cell
int neighbourX = x + i;
int neighbourY = y + j;

// Wrap around the edges of the grid if necessary
if (neighbourX < 0)
{
neighbourX += Rows;
}

if (neighbourY < 0)
{
neighbourY += Cols;
}

if (neighbourX >= Rows)
{
neighbourX -= Rows;
}

if (neighbourY >= Cols)
{
neighbourY -= Cols;
}

// If the neighbour cell is alive, increment the counter
if (grid[neighbourX, neighbourY])
{
count++;
}
}
}

// Return the final count of live neighbours
return count;
}
// A helper method to count how many cells change state between two generations
static int CountChanges(bool[,] oldGrid, bool[,] newGrid)
{
// Initialize a counter variable
int count = 0;

// Loop through every cell in the grid
for (int i = 0; i < Rows; i++)
{
for (int j = 0; j < Cols; j++)
{
// If the state of the cell is different in the old and new grid, increment the counter
if (oldGrid[i, j] != newGrid[i, j])
{
count++;
}
}
}

// Return the final count of changes
return count;
}
}
}

0 comments on commit f9da366

Please sign in to comment.