This commit is contained in:
Pablu23
2023-01-10 22:54:09 +01:00
parent 4938d306cd
commit 875b77ca43
5 changed files with 324 additions and 0 deletions

7
.gitignore vendored
View File

@@ -30,3 +30,10 @@
*.exe *.exe
*.out *.out
*.app *.app
.vs/
.git/
x64/
*.filters
.user
GameOfLifeInC/x64/

31
GameOfLifeInC.sln Normal file
View File

@@ -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

View File

@@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{3c48d7e2-62db-4680-aa11-c36484a3d447}</ProjectGuid>
<RootNamespace>GameOfLifeInC</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.c" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>

147
GameOfLifeInC/main.c Normal file
View File

@@ -0,0 +1,147 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>
#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, &nothing);
}
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;
}