-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_life_2.c
369 lines (298 loc) · 7.81 KB
/
game_of_life_2.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <conio.h>
/*
* Author: Mawaba Pascal Dao, [email protected]
* Course: CSE 1002, Section 05, Spring 2018
* Project: game-of-life-1
*/
void new_era(int world_size, int world[world_size][world_size], int cycles); //Updates current world
int event_horizon(int i, int j, int world_size, int world[world_size][world_size]); //Checks limits of the matrix, and current position
int pulse(int place, int state, int neighbor); //Checks cell pulse (dead or alive)
void graphics(int world_size, int world[world_size][world_size]);
int main(int argc, char *argv[])
{
char firstLine[100];
int i,size=0;
int iterations = atoi(argv[2]);
FILE *infile;
infile = fopen(argv[1],"r");
fscanf(infile, "%[^\n]s", firstLine); //Reads first line in file to be used to determine matrix size
fclose(infile);
for (i = 0; i < strlen(firstLine); i++)
{
if (firstLine[i] == ';')
{
size++;
}
}
size+=1; //Number of columns is number of semi colons plus one
int world[size][size]; //square matrix, therefore columns equal rows
int j=0,k=0,l=0;
i=0;
char c;
fclose(infile); //Close an reopen file to start reading from beginning
infile = fopen(argv[1],"r");
do
{
c = fgetc(infile); //Reads charachter by charachter...
if (c!=';') // Avoiding semi colons
{
world[k][l]=c-'0'; //Stores charachter as integers in integer matrix
if (l == (size))
{
l = 0;
k+=1;
}
else
{
l++;
}
}
}while(c!=EOF); //Reads until end of file
fclose(infile);
new_era(size,world,iterations); //Function call to update matrix
/*printf("The final position: \n");
for (i = 0; i < size; i++)
{
for (j = 0; j< size; j++)
{
if (world[i][j]==1)
{
printf("%d %d\n", i, j);
}
}
}*/
//graphics(size, world);
FILE *outfile; //Initializing output file
outfile = fopen("output_test.csv","w");
for (i = 0; i < size; i++)
{
for (j = 0; j< size; j++)
{
if (j == (size-1))
fprintf(outfile, "%d",world[i][j]);
else
fprintf(outfile, "%d;",world[i][j]);
}
fprintf(outfile,"\n");
}
fclose(outfile);
return 0;
}
void new_era(int world_size, int world[world_size][world_size], int cycles)
{
int i, j, k, place,neighbor=0;
int new_world[world_size][world_size];
for (k = 1; k <= cycles; k++)
{
system("clear");
for (i=0; i < world_size; i++)
{
for (j=0; j < world_size;j++)
{
new_world[i][j] = 0;
place = event_horizon(i,j,world_size,world); //function call to check current position on the matrix
if (place == 1) //Determines cells to check given a condition
{ //9 total relevant position were determined, bottom, top, left and right sides, four corners, and interior
if (world[i][j+1]==1) //The relevant position determine which neighbouring cells to check for pulse
neighbor++;
if (world[i+1][j]==1)
neighbor++;
if (world[i+1][j+1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor); //updates new matrix
neighbor = 0;
}
else if(place == 2)
{
if (world[i][j-1]==1)
neighbor++;
if (world[i+1][j]==1)
neighbor++;
if (world[i+1][j-1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if(place == 3)
{
if (world[i][j+1]==1)
neighbor++;
if (world[i][j-1]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
if (world[i+1][j+1]==1)
neighbor++;
if (world[i+1][j-1]==1)
neighbor++;
world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 4)
{
if (world[i][j+1]==1)
neighbor++;
if (world[i-1][j+1]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 5)
{
if (world[i][j-1]==1)
neighbor++;
if (world[i-1][j-1]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 6)
{
if (world[i][j+1]==1)
neighbor++;
if (world[i][j-1]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
if (world[i-1][j+1]==1)
neighbor++;
if (world[i-1][j-1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 7)
{
if (world[i+1][j]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
if (world[i][j+1]==1)
neighbor++;
if (world[i-1][j+1]==1)
neighbor++;
if (world[i+1][j+1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 8)
{
if (world[i+1][j]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
if (world[i][j-1]==1)
neighbor++;
if (world[i-1][j-1]==1)
neighbor++;
if (world[i+1][j-1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
else if (place == 0)
{
if (world[i+1][j]==1)
neighbor++;
if (world[i-1][j]==1)
neighbor++;
if (world[i][j-1]==1)
neighbor++;
if (world[i][j+1]==1)
neighbor++;
if (world[i+1][j-1]==1)
neighbor++;
if (world[i+1][j+1]==1)
neighbor++;
if (world[i-1][j-1]==1)
neighbor++;
if (world[i-1][j+1]==1)
neighbor++;
new_world[i][j]=pulse(place, world[i][j], neighbor);
neighbor = 0;
}
}
}
for (i=0; i < world_size; i++)
{
for (j=0; j < world_size;j++)
{
world[i][j]=new_world[i][j];
}
}
printf("iteration %d\n", k);
graphics(world_size, world);
//system("clear");
}
}
int event_horizon(int i, int j, int world_size, int world[world_size][world_size])
{
if (i == 0) // Identifying place on matrix and assighing one of 9 relevent position
{
if (j == 0)
return 1;
else if (j == (world_size-1))
return 2;
else
return 3;
}
else if(i == (world_size-1))
{
if (j == 0)
return 4;
else if (j == (world_size-1))
return 5;
else
return 6;
}
else if (j == 0)
return 7;
else if (j == (world_size-1))
return 8;
else
return 0;
}
int pulse(int place, int state, int neighbor) //Given current state, fuction applies game rules and determines new state of sell (dead or alive)
{
int pulse;
if (neighbor < 2 && state==1)
{
pulse=0;
}
else if(neighbor > 3 && state==1)
{
pulse=0;
}
else if((neighbor == 2 || neighbor ==3) && state==1)
{
pulse=1;
}
else if(neighbor == 3 && state==0)
{
pulse=1;
}
return pulse;
}
void graphics(int world_size, int world[world_size][world_size])
{
int i = 0, j = 0;
for (i=0; i < world_size; i++)
{
for (j=0; j < world_size; j++)
{
if (world[i][j] == 1)
printf("|o");
else
printf("| ");
}
printf("|\n");
}
}