-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
213 lines (185 loc) · 3.93 KB
/
main.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#define RAYGUI_IMPLEMENTATION
#include <stdio.h>
#include <raylib.h>
#include <unistd.h>
#define SIZE 80
enum cell_state
{
DEAD,
ALIVE
};
enum cell_event
{
STAY,
BIRTH,
DEATH
};
enum cell_state current_cell = DEAD;
enum cell_state next_cell = DEAD;
int neighbor_count = 0;
enum cell_event determine_event(enum cell_state current_cell, int neighbor_count)
{
if (current_cell == DEAD && neighbor_count == 3)
{
return BIRTH;
}
else if (current_cell == ALIVE && (neighbor_count == 2 || neighbor_count == 3))
{
return STAY;
}
else
{
return DEATH;
}
}
enum cell_state handle_transition(enum cell_state current_cell, enum cell_event event)
{
switch (event)
{
case STAY:
return current_cell;
case BIRTH:
return ALIVE;
case DEATH:
return DEAD;
}
return DEAD;
}
enum cell_state grid[SIZE][SIZE];
enum cell_state next_grid[SIZE][SIZE];
int count_neighbors(enum cell_state grid[SIZE][SIZE], int i, int j)
{
int count = 0;
if (i > 0 && j > 0 && grid[i - 1][j - 1] == ALIVE)
{
count++;
}
if (i > 0 && grid[i - 1][j] == ALIVE)
{
count++;
}
if (i > 0 && j < SIZE - 1 && grid[i - 1][j + 1] == ALIVE)
{
count++;
}
if (j > 0 && grid[i][j - 1] == ALIVE)
{
count++;
}
if (j < SIZE - 1 && grid[i][j + 1] == ALIVE)
{
count++;
}
if (i < SIZE - 1 && j > 0 && grid[i + 1][j - 1] == ALIVE)
{
count++;
}
if (i < SIZE - 1 && grid[i + 1][j] == ALIVE)
{
count++;
}
if (i < SIZE - 1 && j < SIZE - 1 && grid[i + 1][j + 1] == ALIVE)
{
count++;
}
return count;
}
void game_of_life()
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
int neighbor_count = count_neighbors(grid, i, j);
enum cell_event event = determine_event(grid[i][j], neighbor_count);
next_grid[i][j] = handle_transition(grid[i][j], event);
}
}
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
grid[i][j] = next_grid[i][j];
}
}
}
void display()
{
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (grid[i][j] == ALIVE)
{
DrawRectangle(i * 10, j * 10, 10, 10, BLACK);
}
}
}
EndDrawing();
}
void grid_clear()
{
int i, j;
for (i = 0; i < SIZE; i++)
{
for (j = 0; j < SIZE; j++)
{
grid[i][j] = DEAD;
}
}
}
int main(void)
{
InitWindow(SIZE * 10, (SIZE * 10) + 40, "Game of Life");
SetTargetFPS(60);
grid_clear();
bool paused = true;
while (!WindowShouldClose())
{
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
{
int mouse_x = GetMouseX() / 10;
int mouse_y = GetMouseY() / 10;
if (mouse_x >= 0 && mouse_x < SIZE && mouse_y >= 0 && mouse_y < SIZE)
{
grid[mouse_x][mouse_y] = grid[mouse_x][mouse_y] == ALIVE ? DEAD : ALIVE;
}
}
if (IsKeyPressed(KEY_R))
{
for (int i = 0; i < SIZE; i++)
{
for (int j = 0; j < SIZE; j++)
{
grid[i][j] = GetRandomValue(0, 1);
}
}
}
if (IsKeyPressed(KEY_SPACE))
{
paused = !paused;
}
if (IsKeyPressed(KEY_S))
{
game_of_life();
paused = true;
}
if (IsKeyPressed(KEY_C))
{
grid_clear();
}
if (!paused)
{
game_of_life();
}
else
{
DrawText("Paused", 10, 810, 20, BLACK);
}
display();
usleep(10000);
}
return 0;
}