-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
300 lines (273 loc) · 6.7 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <GL/glut.h>
#include <stdio.h>
#include "array.c"
#include "array2.c"
#include <math.h>
// Callback functions
void render(void);
void mouse(int button, int state, int x, int y);
void keyboard(unsigned char key, int x, int y);
void gameTick(void);
void changeStatus(int index);
int neighbourValid(int position, int active);
int getNeighbourStatus(int position, int active);
int getIndex(float x, float y);
// Edit or Play indicator
int mode = 0;
int main(int argc, char **argv){
// Initilization
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(600,600);
glutInitWindowPosition(100,100);
glutCreateWindow("Main");
// Callback functions
glutDisplayFunc(render);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
// Render everything
void render(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float size = 0.02;
int cols = 100;
int rows = 100;
// Draw the field
for(int i = 0; i<cols; i++){
for(int j = 0; j<rows; j++){
glBegin(GL_POLYGON);
if(game[i*100+j] == 0){
glColor3f(!mode,!mode,!mode);
}else{
glColor3f(mode, mode, mode);
}
glVertex2f(-1+size*j,1-size*i);
glVertex2f(-1+size+size*j,1-size*i);
glVertex2f(-1+size+size*j,1-size-size*i);
glVertex2f(-1+size*j,1-size-size*i);
glEnd();
glFlush();
}
}
// Execute a gametick if it's in play-mode
if(mode){
gameTick();
}
glutPostRedisplay();
glutSwapBuffers();
}
// Mouse callback
void mouse(int button, int state, int x, int y){
// If in edit mode and the left mouse button is pressed,
// change the status of the cell you clicked on
if((!mode) && (button == GLUT_LEFT_BUTTON) && (state == GLUT_DOWN)){
changeStatus(getIndex(x*1.0, y*1.0));
}
}
// Keyboard callback
void keyboard(unsigned char key, int x, int y){
// Invert mode if spacebar is pressed
if(key == 32){
mode = !mode;
}
}
// The logic of the game. Checks and changes cells.
void gameTick(void){
// Execute for every cell
for(int i = 0; i<100; i++){
for(int j = 0; j<100; j++){
int activeCell = i*100+j;
int neighbours = 0;
// Get the number of neighbours of the current cell
for(int i = 1; i<=8; i++){
int ret = neighbourValid(i, activeCell);
if(ret==1){
neighbours += 1;
}
}
// If alive
if(game[activeCell] == 1){
// Rule 1 - Underpopulation
if(neighbours < 2){
game2[activeCell] = 0;
// Rule 2
}else if((neighbours == 2) || (neighbours == 3)){
game2[activeCell] = 1;
// Rule 3 - Overpopulation
}else if(neighbours > 3){
game2[activeCell] = 0;
}
//If dead
}else{
// Rule 4 - Reproduction
if(neighbours == 3){
game2[activeCell] = 1;
}else{
game[activeCell] = 0;
}
}
}
}
// Copy the buffer into the main array
for(int i = 0; i<sizeof(game)/sizeof(game[0]); i++){
game[i] = game2[i];
}
}
// TODO Rewrite function
//
// Returns if <position> is a valid neighbour of cell <active>
// Either 1 or 0 or 404 (if the neighbour is not available due to corner/borders)
//
// Positions:
//
// 1 2 3
// 4 A 5
// 6 7 8
//
// A = Active(array index)
//
//
//
//
int neighbourValid(int position, int active){
int y = active/100;
int x = ((double)(active%100)/100)*100;
// If border left
if(x == 0){
// If corner top left
if(y == 0){
if((position == 5) || (position == 7) || (position == 8)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// If corner bottom left
}else if(y == 99){
if((position == 2) || (position == 3) || (position == 5)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// Border left no special case
}else{
if((position == 2) || (position == 3) || (position == 5) || (position == 7) || (position == 8)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
}
// If border right
}else if(x == 99){
// If corner top right
if(y == 0){
if((position == 4) || (position == 6) || (position == 7)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// If corner bottom right
}else if(y == 99){
if((position == 1) || (position == 2) || (position == 4)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// Border right no special case
}else{
if((position == 1) || (position == 2) || (position == 4) || (position == 6) || (position == 7)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
}
// Border top
}else if(y == 0){
// If corner top left
if(x == 0){
if((position == 5) || (position == 7) || (position == 8)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// If corner top right
}else if(x == 99){
if((position == 4) || (position == 6) || (position == 7)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// Border top no special case
}else{
if((position == 4) || (position == 5) || (position == 6) || (position == 7) || (position == 8)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
}
// Border bottom
}else if(y == 99){
// If corner bottom left
if(x == 0){
if((position == 2) || (position == 3) || (position == 5)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// If corner bottom right
}else if(x == 99){
if((position == 1) || (position == 2) || (position == 4)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
// Border bottom no special case
}else{
if((position == 1) || (position == 2) || (position == 3) || (position == 4) || (position == 5)){
return getNeighbourStatus(position, active);
}else{
return 404;
}
}
}else{
return getNeighbourStatus(position, active);
}
}
// Returns status of cell <active> in positition <position>
// Either 1 or 0
//
// Positions:
//
// 1 2 3
// 4 A 5
// 6 7 8
//
// A = Active(array index)
int getNeighbourStatus(int position, int active){
if(position == 1){
return game[active-100-1];
}else if(position == 2){
return game[active-100];
}else if(position == 3){
return game[active-100+1];
}else if(position == 4){
return game[active-1];
}else if(position == 5){
return game[active+1];
}else if(position == 6){
return game[active+100-1];
}else if(position == 7){
return game[active+100];
}else if(position == 8){
return game[active+100+1];
}
}
// getIndex by coordinates
int getIndex(float x, float y){
return floor(y/6.0)*100+(floor(x/6.0));
}
// Inverts the value of cell <index>
void changeStatus(int index){
game[index] = !game[index];
}