-
Notifications
You must be signed in to change notification settings - Fork 0
/
conway.c
147 lines (121 loc) · 3.42 KB
/
conway.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// //////////////////////////////////////////////////////////////////////// //
// C implementation if Conway's Game of Life using SDL2 //
// By: Kiko //
// //
// https://en.wikipedia.org/wiki/Conway's_Game_of_Life //
// //
// The world is initialized with random values of 0-1 at runtime. //
// The seed / initial state can be modified in the InitWorld() function //
// //////////////////////////////////////////////////////////////////////// //
#include <SDL2/SDL.h>
#include <time.h>
#define ALIVE 1
#define DEAD 0
static const int HEIGHT = 112;
static const int WIDTH = 184;
static const int SCALE = 8;
static const int TICK_DELAY_MS = 70;
void InitWorld(int* world);
void Tick(int** world);
void Draw(SDL_Renderer* renderer, const int* world);
int main(void){
int* world = malloc(WIDTH * HEIGHT * sizeof(int));
InitWorld(world);
SDL_Window* window = NULL;
SDL_Renderer* renderer = NULL;
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(
WIDTH * SCALE, HEIGHT * SCALE,
0, &window, &renderer);
SDL_RenderSetScale(renderer, SCALE, SCALE);
SDL_Event e;
int running = 0;
while(running == 0)
{
Tick(&world);
Draw(renderer, world);
SDL_Delay(TICK_DELAY_MS);
while( SDL_PollEvent( &e ) > 0 )
{
if( e.type == SDL_QUIT ){
printf("Registered QUIT cmd, exiting now\n");
running = 1;
}
}
}
free(world);
return 0;
}
void InitWorld(int* world)
{
srandom(time(NULL));
for(int y = 0; y < HEIGHT; y++)
{
for(int x = 0; x < WIDTH; x++)
{
world[y*WIDTH + x] = random() % 2;
}
}
}
int GetCoords(const int x, const int y)
{
return WIDTH * (y % HEIGHT) + (x % WIDTH);
}
void Draw(SDL_Renderer* renderer, const int* world)
{
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,255,255,255);
for(int y = 0; y < HEIGHT; y++)
{
for(int x = 0; x < WIDTH; x++)
{
if(world[WIDTH * y + x] == 1)
{
SDL_RenderDrawPoint(renderer, x, y);
}
}
}
SDL_RenderPresent(renderer);
}
int EvaluateState(const int self, const int neighbors)
{
if(self == ALIVE)
{
if(neighbors == 2 || neighbors == 3)
{
return ALIVE;
}
}
else
{
if(neighbors == 3)
{
return ALIVE;
}
}
return DEAD;
}
void Tick(int** world)
{
int* nextWorld = calloc(WIDTH * HEIGHT, sizeof(int));
for(int y = 0; y < HEIGHT; y++)
{
for(int x = 0; x < WIDTH; x++)
{
const int currentPos = GetCoords(x,y);
int neighbors = 0;
for(int i = -1; i <= 1; i++)
{
for(int j = -1; j <= 1; j++)
{
neighbors += (*world)[GetCoords(x + i, y + j)];
}
}
neighbors -= (*world)[currentPos];
nextWorld[currentPos] = EvaluateState((*world)[currentPos], neighbors);
}
}
free(*world);
*world = nextWorld;
}