-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.c
355 lines (335 loc) · 9.32 KB
/
life.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
/* Game of Life
*/
#include <stdio.h>
#include <stdlib.h>
#include "twoD.h"
/**
* Steps the game forward
* @param rows
* @param columns
* @param grid
* @return
*/
char** step(int rows, int columns, char **grid){
unsigned int top;
unsigned int bottom;
unsigned int left;
unsigned int right;
int neighbors;
char **out = make2Dchar(rows, columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
// flags for edges of the grid
top = 0;
bottom = 0;
left = 0;
right = 0;
if(i==0){
top=1;
}
if(i==(rows-1)){
bottom=1;
}
if(j==0){
left=1;
}
if(j==(columns-1)){
right=1;
}
// checks all neighbors
neighbors=0;
if(!right){
if(grid[i][j+1]=='x'){
neighbors++;
}
}
if(!left){
if(grid[i][j-1]=='x'){
neighbors++;
}
}
if(!top){
if(grid[i-1][j]=='x'){
neighbors++;
}
}
if(!bottom){
if(grid[i+1][j]=='x'){
neighbors++;
}
}
if(!right&&!top){
if(grid[i-1][j+1]=='x'){
neighbors++;
}
}
if(!left&&!top){
if(grid[i-1][j-1]=='x'){
neighbors++;
}
}
if(!right&&!bottom){
if(grid[i+1][j+1]=='x'){
neighbors++;
}
}
if(!left&&!bottom){
if(grid[i+1][j-1]=='x'){
neighbors++;
}
}
// checks if current cell is alive or dead
if(grid[i][j]=='x') { // Death & survival logic
if (neighbors <= 1 || neighbors >= 4) {
out[i][j] = ' ';
} else {
out[i][j] = 'x';
}
} else{ // Birth logic
if (neighbors==3){
out[i][j] = 'x';
}
else{
out[i][j] = ' ';
}
}
}
}
free(grid);
return out;
}
/**
* Compares two 2D int arrays of the same specified size
* @param rows
* @param columns
* @param grid1
* @param grid2
* @return 1 if the same and 0 if different
*/
int compare(int rows, int columns, char **grid1, char **grid2){
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
if (grid1[i][j]!=grid2[i][j]){
return 0;
}
}
}
return 1;
}
/**
* prints the grid from a char array
* @param generation
* @param rows
* @param columns
* @param grid
* @return
*/
int printGrid(int generation, int rows, int columns, char **grid){
printf("Generation %d\n", generation);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
printf("%c",grid[i][j]);
}
printf("\n");
}
return 0;
}
/**
* Fills the specified grid with " " for initialization purposes
* @param rows
* @param columns
* @param grid
* @return an initialized grid of chars
*/
char** initializeGrid(int rows, int columns, char **grid){
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
grid[i][j]=' ';
}
}
return grid;
}
/** Main function.
* @param argc Number of words on the command line.
* @param argv Array of pointers to character strings containing the words on the command line.
* @return 0 if success, 1 if invalid command line or unable to open file.
*
*/
int main(int argc, char **argv) {
printf("Game of Life\n");
FILE *input; // Stream descriptor for file containing initial grid
char *inputFileName; // Name of file containing initial grid
int rows; // Number of rows in the grid
int columns; // Number of columns in the grid
int gens; // Number of generations to produce
int doPrint; // 1 if user wants to print each generation, 0 if not
int doPause; // 1 if user wants to pause after each generation, 0 if not
char **currGrid; // A 2D array to hold the current state
char **pastGrid1; // A 2D array to hold the previous state
char **pastGrid2; // A 2D array to hold the state 2 cycles before
int inRows;
int inColumns;
int cell;
int lastGen;
// See if there are the right number of arguments on the command line
if ((argc < 5) || (argc > 7)) {
// If not, tell the user what to enter.
printf("Usage:\n");
printf(" ./life rows columns generations inputFile [print] [pause]\n");
return EXIT_FAILURE;
}
// Input processing
rows = atoi(argv[1]); // Convert from character string to integer.
columns = atoi(argv[2]);
gens = atoi(argv[3]);
// checks that row, column, and generation inputs are positive
if(rows<1||columns<1||gens<1){
printf("Row, column, and generation inputs must be greater than 0 to run the simulation");
return EXIT_FAILURE;
}
inputFileName = argv[4];
doPrint=0;
doPause=0;
// Controls the doPrint variable
if(argc >= 6){
if(argv[5][0] == 'y'||argv[5][0]=='Y'){
doPrint=1;
}
else if(argv[5][0] == 'n'||argv[5][0]=='N'){
doPrint=0;
}
else{
printf("Invalid input: use y or n to specify if the program prints");
return EXIT_FAILURE;
}
}
// Controls the doPause variable
if(argc == 7){
if(argv[6][0] == 'y'||argv[6][0]=='Y'){
doPause=1;
}
else if(argv[6][0] == 'n'||argv[6][0]=='N'){
doPause=0;
}
else{
printf("Invalid input: use y or n to specify if the program pauses");
return EXIT_FAILURE;
}
}
input = fopen(inputFileName, "r");
if (!input) {
printf("Unable to open input file: %s\n", inputFileName);
return EXIT_FAILURE;
}
currGrid = make2Dchar(rows, columns);
pastGrid1 = make2Dchar(rows, columns);
pastGrid2 = make2Dchar(rows, columns);
// Intitializes arrays
initializeGrid(rows, columns, currGrid);
initializeGrid(rows, columns, pastGrid1);
initializeGrid(rows, columns, pastGrid2);
inColumns=0;
inRows=0;
int columnCount;
// Reads through the input file to determine the size of the input in rows and columns
while (!feof(input)){
columnCount=0;
while(!feof(input)){
cell=fgetc(input);
if(cell==10){
inRows++;
break;
}
else{
columnCount++;
}
}
if(columnCount>inColumns){
inColumns=columnCount;
}
}
// Checks input rows
if(inColumns>columns){
printf("Too few columns for input");
return EXIT_FAILURE;
}
if(inRows>rows){
printf("Too few rows for input");
return EXIT_FAILURE;
}
input = fopen(inputFileName, "r");
// Initial assignment of array values
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
if(j>inColumns){
inColumns=j;
}
if(feof(input)){
break;
}
cell=fgetc(input);
if(cell==10){
break;
}
else if(cell==120){
pastGrid1[i][j]='x';
}
else{
pastGrid1[i][j]=' ';
}
}
if(i>inRows){
inRows=i;
}
if(feof(input)){
break;
}
}
// calculates offsets for centering the inputs
int offsetRow;
offsetRow=rows/2-inRows/2;
int offsetColumn;
offsetColumn=columns/2-inColumns/2;
// Offsets the grid
for(int i=offsetRow, k=0; k<=inRows; i++, k++){
for (int j=offsetColumn, l=0; l<=inColumns; j++, l++) {
currGrid[i][j]=pastGrid1[k][l];
}
}
// Clears the gird holding the first set of inputs
initializeGrid(rows, columns, pastGrid1);
// Simulation loop
for (int k = 0; k < gens+1; ++k) {
if(doPrint){
printGrid(k,rows,columns,currGrid);
}
for (int l = 0; l < rows; ++l) {
for (int i = 0; i < columns; ++i) {
pastGrid2[l][i]=pastGrid1[l][i];
}
}
for (int l = 0; l < rows; ++l) {
for (int i = 0; i < columns; ++i) {
pastGrid1[l][i]=currGrid[l][i];
}
}
currGrid=step(rows,columns,currGrid);
lastGen=k;
if(compare(rows, columns, currGrid, pastGrid2)){
printf("Program has ended due to repetition at generation %d\n", lastGen);
break;
}
if(doPause){
printf("Press Enter to continue\n");
getchar();
}
}
if(!doPrint){
printGrid(lastGen,rows,columns,currGrid);
}
// Frees up memory on ending the program
free(currGrid);
free(pastGrid1);
free(pastGrid2);
return EXIT_SUCCESS;
}