-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_utilities.c
More file actions
283 lines (262 loc) · 6.54 KB
/
file_utilities.c
File metadata and controls
283 lines (262 loc) · 6.54 KB
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
/*
* Created By: Andrew Overman
* Date: 2/7/2018
* C Code for the file utilities methods
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
/*
*Reads in the contents of a file to the program
*@Param char* filename: The name of the file being read
*@Param char** buffer: The array that the board is being saved into
*@return int: 0 if successful
* got the code to get the file size off of stack overflow. It uses the fseek, the ftell, and fread
* https://stackoverflow.com/questions/174531/easiest-way-to-get-files-contents-in-c
*/
int read_file( char* filename, char **buffer ){
FILE *fileptr;
fileptr = fopen(filename,"r");
if(fileptr == NULL){
printf("Error, could not find file\n");
return 1;
}
fseek(fileptr, 0, SEEK_END);
int size = ftell(fileptr);
fseek(fileptr, 0, SEEK_SET);
*buffer = malloc(sizeof(char) * size);
fread(*buffer, 1, size, fileptr);
fclose(fileptr);
return 0;
}
/*
*Saves the contents of the simulator to a file.
*@Param char* filename: This is the name of the file that the method saves to
*@Param char* buffer: The array that the board is being saved into
*@Param int size: The size of the file being saved
*@return int: 0 if successful
*/
int write_file( char* filename, char *buffer, int size){
FILE *fileptr;
fileptr = fopen(filename, "w");
if(fileptr == NULL){
printf("Error, could not find file\n");
return 0;
}
fprintf(fileptr, buffer, size);
return 0;
}
/*
*A method to request memory for the board
*@Param int x: the height of the board
*@Param int y: the width of the board
*@return int: 0 if successful
* I got help from Alan Sisouphone on Slack for how to pass the array around
* We also used https://www.geeksforgeeks.org/dynamically-allocate-2d-array-c/ for reference. We used example number 3 on this site.
*/
void createBoard(int x, int y, int*** buffer){
int i, j = 0;
(*buffer) = (int**)malloc(x * sizeof(int *));
for (i = 0; i < x; i++)
(*buffer)[i] = (int*)malloc(y * sizeof(int));
for (i = 0; i < x; i++)
for(j = 0; j < y; j++)
(*buffer)[i][j] = 0;
}
/*
*A method to check the surroundings of the cell and update the cell
*@Param int x: the x coordinate of the cell
*@Param int y: the y coordinate of the cell
*@Param int** buffer: the board
*@return int: returns what the cell should be: 0 if empty, 1 if alive, 2 if dead
* I got help from Alan Sisouphone for how to pass the array around
*/
int checkSurround(int x, int y, int*** buffer){
int state = (*buffer)[x][y];
int count = 0;
//This if block checks all non edge or corner spaces
if(x > 0 && y > 0 && x < (sizeof((*buffer))/sizeof(int*)) && y < (sizeof((*buffer)[0])/sizeof(int))) {
x--;
y--;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
x = x - 2;
y++;
if ((*buffer)[x][y] == 1)
count++;
x = x + 2;
if ((*buffer)[x][y] == 1)
count++;
x = x - 2;
y++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
} //This if block checks the top left corner
else if(x == 0 && y == 0) {
x++;
if ((*buffer)[x][y] == 1)
count++;
x--;
y++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
} //This if block checks the bottom right corner
else if(x == (sizeof((*buffer))/sizeof(int*)) && y == (sizeof((*buffer)[0])/sizeof(int))) {
y--;
if ((*buffer)[x][y] == 1)
count++;
x--;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
} //This if block checks the bottom left corner
else if(x == 0 && y == (sizeof((*buffer)[0])/sizeof(int))) {
y--;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
} //This if block checks the top right corner
else if(x == (sizeof((*buffer))/sizeof(int*)) && y == 0) {
x--;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
}// This if block checks the Left edge
else if(x == 0 && y > 0 && y < (sizeof((*buffer)[0])/sizeof(int))) {
y--;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
x--;
if ((*buffer)[x][y] == 1)
count++;
}//This if block checks the right Edge
else if(x == (sizeof((*buffer))/sizeof(int*)) && y > 0 && y < (sizeof((*buffer)[0])/sizeof(int))) {
y--;
if ((*buffer)[x][y] == 1)
count++;
x--;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
}//This if block checks the top edge
else if(y == 0 && x > 0 && x < (sizeof((*buffer))/sizeof(int*))) {
x--;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
y--;
if ((*buffer)[x][y] == 1)
count++;
}//This if block checks the bottom edge
else if(y == (sizeof((*buffer))/sizeof(int*)) && x > 0 && x < (sizeof((*buffer)[0])/sizeof(int*))) {
x--;
if ((*buffer)[x][y] == 1)
count++;
y--;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
x++;
if ((*buffer)[x][y] == 1)
count++;
y++;
if ((*buffer)[x][y] == 1)
count++;
}//This checks count to see how many neighbors there are, and also checks state to see what the correct action to take is.
if (count < 2 && state == 1)
return 2;
else if ((count == 2 || count == 3) && state == 1)
return 1;
else if (count == 3 && state == 2)
return 1;
else if (count > 3 && state == 1)
return 2;
else
return state;
return 0;
}
/*
*A method to make a copy of the board
*@Param int** buffer: the board
*@Param int x: the width of the board
*@Param int y: the height of the board
* I got help from Alan Sisouphone to know how to pass the array around
*/
void copyBoard(int x, int y, int*** buffer, int*** copy){
int i, j = 0;
for(i = 0; i < x; i++){
for(j = 0; j < y; j++){
(*copy)[i][j] = (*buffer)[i][j];
}
}
}
/*
*A method to display the new board after a generation
*@Param int* buffer: the board
*@Param int x: the width of the board
*@Param int y: the height of the board
*/
void updateBoard(int x, int y, int*** buffer){
int i, j = 0;
for(i = 0; i < x; i++){
for(j = 0; j < y; j++){
printf("|%i",(*buffer)[i][j]);
}
printf("|\n");
}
printf("\n");
}