From 875b77ca43e8d19eb2e4dbc6d9968f1f822eb69a Mon Sep 17 00:00:00 2001 From: Pablu23 <43807157+Pablu23@users.noreply.github.com> Date: Tue, 10 Jan 2023 22:54:09 +0100 Subject: [PATCH] Initial --- .gitignore | 7 ++ GameOfLifeInC.sln | 31 +++++ GameOfLifeInC/GameOfLifeInC.vcxproj | 135 +++++++++++++++++++++ GameOfLifeInC/GameOfLifeInC.vcxproj.user | 4 + GameOfLifeInC/main.c | 147 +++++++++++++++++++++++ 5 files changed, 324 insertions(+) create mode 100644 GameOfLifeInC.sln create mode 100644 GameOfLifeInC/GameOfLifeInC.vcxproj create mode 100644 GameOfLifeInC/GameOfLifeInC.vcxproj.user create mode 100644 GameOfLifeInC/main.c diff --git a/.gitignore b/.gitignore index 259148f..4f2425d 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,10 @@ *.exe *.out *.app + +.vs/ +.git/ +x64/ +*.filters +.user +GameOfLifeInC/x64/ \ No newline at end of file diff --git a/GameOfLifeInC.sln b/GameOfLifeInC.sln new file mode 100644 index 0000000..bc1b639 --- /dev/null +++ b/GameOfLifeInC.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33205.214 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GameOfLifeInC", "GameOfLifeInC\GameOfLifeInC.vcxproj", "{3C48D7E2-62DB-4680-AA11-C36484A3D447}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Debug|x64.ActiveCfg = Debug|x64 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Debug|x64.Build.0 = Debug|x64 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Debug|x86.ActiveCfg = Debug|Win32 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Debug|x86.Build.0 = Debug|Win32 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Release|x64.ActiveCfg = Release|x64 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Release|x64.Build.0 = Release|x64 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Release|x86.ActiveCfg = Release|Win32 + {3C48D7E2-62DB-4680-AA11-C36484A3D447}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DC566C10-E04B-41AC-AFFC-6E10C7DE547F} + EndGlobalSection +EndGlobal diff --git a/GameOfLifeInC/GameOfLifeInC.vcxproj b/GameOfLifeInC/GameOfLifeInC.vcxproj new file mode 100644 index 0000000..7c43804 --- /dev/null +++ b/GameOfLifeInC/GameOfLifeInC.vcxproj @@ -0,0 +1,135 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {3c48d7e2-62db-4680-aa11-c36484a3d447} + GameOfLifeInC + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/GameOfLifeInC/GameOfLifeInC.vcxproj.user b/GameOfLifeInC/GameOfLifeInC.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/GameOfLifeInC/GameOfLifeInC.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/GameOfLifeInC/main.c b/GameOfLifeInC/main.c new file mode 100644 index 0000000..3b0032f --- /dev/null +++ b/GameOfLifeInC/main.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include +#include + +#define ROUNDS 500 +#define LENGTH 100 +#define FULLLENGTH (LENGTH * LENGTH) + +struct cell { + int alive; + int nextTurn; + int* neighbours[8]; +}; + +void setNeighbours(struct cell* cells, int i, int* nothing) { + for (size_t y = 0; y < 8; y++) + { + cells[i].neighbours[y] = nothing; + } + + // Top + if (i - (LENGTH) > 0) + cells[i].neighbours[1] = &cells[i - (LENGTH)].alive; + + // Bottom + if (i + (LENGTH) < FULLLENGTH) + cells[i].neighbours[6] = &cells[i + (LENGTH)].alive; + + // Left + if (i % LENGTH != 0) { + // Top Left + if (i - (LENGTH + 1) >= 0) + cells[i].neighbours[0] = &cells[i - (LENGTH + 1)].alive; + + // Left + if (i - 1 >= 0) + cells[i].neighbours[3] = &cells[i - 1].alive; + + // Bottom Left + if (i + (LENGTH - 1) >= 0 && i + (LENGTH - 1) < FULLLENGTH) + cells[i].neighbours[3] = &cells[i + (LENGTH - 1)].alive; + } + + // Right + if ((i + 1) % LENGTH != 0) { + // Top Right + if (i - (LENGTH - 1) >= 0) + cells[i].neighbours[2] = &cells[i - (LENGTH - 1)].alive; + + // Right + if (i + 1 < FULLLENGTH) + cells[i].neighbours[4] = &cells[i + 1].alive; + + // Bottom Right + if (i + (LENGTH + 1) < FULLLENGTH) + cells[i].neighbours[7] = &cells[i + (LENGTH + 1)].alive; + } +} + +int aliveNeighbours(struct cell* cell) { + int num = 0; + for (size_t i = 0; i < 8; i++) + { + int x = *cell->neighbours[i]; + if (x != -1) + num += x; + } + return num; +} + +void calculateNext(struct cell* cell) { + int neighCount = aliveNeighbours(cell); + + if (neighCount < 2) { + cell->nextTurn = 0; + return; + } + + if ((cell->alive == 1 && neighCount == 2) || neighCount == 3) { + cell->nextTurn = 1; + return; + } + + if (neighCount > 3) { + cell->nextTurn = 0; + } +} + +void update(struct cell* cells) { + for (size_t i = 0; i < FULLLENGTH; i++) + { + cells[i].alive = cells[i].nextTurn; + } +} + +int main() { + + struct cell* cells = (struct cell*)calloc(FULLLENGTH, sizeof(struct cell)); + + if (cells == NULL) + return -1; + + int nothing = -1; + + srand(time(NULL)); + + for (size_t i = 0; i < FULLLENGTH; i++) + { + cells[i].alive = rand() % 2; + + //cells[i].alive = i; + + setNeighbours(cells, i, ¬hing); + } + + char screen[FULLLENGTH + LENGTH + 1]; + + for (size_t k = 0; k < ROUNDS; k++) { + int screenIndex = 0; + + for (size_t i = 0; i < FULLLENGTH; i++) { + if (i % LENGTH == 0){ + screen[screenIndex] = '\n'; + screenIndex++; + } + screen[screenIndex] = cells[i].alive + '0'; + screenIndex++; + + calculateNext(&cells[i]); + } + screen[FULLLENGTH + LENGTH] = '\0'; + + system("cls"); + //printf("-----"); + printf("%s\n", screen); + + //Sleep(50); + + update(cells); + } + + free(cells); + + return 0; +}