-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathou3.c
331 lines (276 loc) · 7.96 KB
/
ou3.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
/*
* Programmeringsteknik med C och Matlab
* Fall 18
* Assignment 3
* File: ou3.c
* Description: A simple implementation of Conway's Game of Life.
* Author: Mikolaj Wagner
* CS username: c18mwj
* Date: 25/10
* Input: Choice of initial configuration and then instruction to step
* or exit.
* Output: Prints the game field in each step.
* Limitations: No validation of input.
*/
/*
1. Initialisering av världen
2. Så länge användaren inte valt att avsluta
2.1. Skriv ut världen
2.2. Skriv ut menyn
2.3. Läs in användarens val
2.4. Om nytt steg
2.4.1.Uppdatera världen en generation
Programmet ska använda sig av funktioner (dvs fler än de ovan nämnda) och den
givna egendefinierade datastrukturen cell.
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Constants, representation of states */
#define ALIVE 'X'
#define DEAD '.'
/* Declaration of data structure */
typedef struct{
char current;
char next;
} cell;
/* Declaration of functions */
void initField(const int rows, const int cols, cell field[rows][cols]);
void loadGlider(const int rows, const int cols, cell field[rows][cols]);
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]);
void loadRandom(const int rows, const int cols, cell field[rows][cols]);
void loadCustom(const int rows, const int cols, cell field[rows][cols]);
void printWorld(const int rows, const int cols, cell field[rows][cols]);
void printMenu(void);
void updateWorld(const int rows, const int cols, cell field[rows][cols]);
void userChoice(const int rows, const int cols, cell field[rows][cols]);
void neighbours(const int rows, const int cols, cell field[rows][cols]);
/* Function: main
* Description: Start and run games, interact with the user.
* Input: About what initial structure and whether to step or exit.
* Output: Information to the user, and the game field in each step.
*/
int main(void) {
const int rows = 20;
const int cols = 20;
cell field[rows][cols];
initField(rows, cols, field);
userChoice(rows, cols, field);
return 0;
}
/* Function: initField
* Description: Initialize all the cells to dead, then asks the user about
* which structure to load, and finally load the structure.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void initField(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) {
for (int c = 0 ; c < cols ; c++) {
field[r][c].current = DEAD;
}
}
printf("Select field spec to load ([G]lider, [S]emaphore, [R]andom ");
printf("or [C]ustom): ");
int ch = getchar();
/* Ignore following newline */
if (ch != '\n') {
getchar();
}
switch (ch) {
case 'g':
case 'G':
loadGlider(rows, cols, field);
break;
case 's':
case 'S':
loadSemaphore(rows, cols, field);
break;
case 'r':
case 'R':
loadRandom(rows, cols, field);
break;
case 'c':
case 'C':
default:
loadCustom(rows, cols, field);
break;
}
}
/* Function: loadGlider
* Description: Inserts a glider into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadGlider(const int rows, const int cols, cell field[rows][cols]) {
field[0][1].current = ALIVE;
field[1][2].current = ALIVE;
field[2][0].current = ALIVE;
field[2][1].current = ALIVE;
field[2][2].current = ALIVE;
}
/* Function: loadSemaphore
* Description: Inserts a semaphore into the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadSemaphore(const int rows, const int cols, cell field[rows][cols]) {
field[8][1].current = ALIVE;
field[8][2].current = ALIVE;
field[8][3].current = ALIVE;
}
/* Function: loadRandom
* Description: Inserts a random structure into the field.
* Input: The field array and its size.
* Output: The field array is updated. There is a 50 % chance that a cell
* is alive.
*/
void loadRandom(const int rows, const int cols, cell field[rows][cols]) {
srand(time(NULL));
for (int r = 0; r < rows; r = r + 1){
for (int c = 0; c < cols; c = c + 1){
int value = rand() % 2;
if (value == 0){
field[r][c].current = DEAD;
}
else{
field[r][c].current = ALIVE;
}
}
}
}
/* Function: loadCustom
* Description: Lets the user specify a structure that then is inserted into
* the field.
* Input: The field array and its size.
* Output: The field array is updated.
*/
void loadCustom(const int rows, const int cols, cell field[rows][cols]) {
printf("Give custom format string: ");
do {
int r, c;
scanf("%d,%d", &r, &c);
field[r][c].current = ALIVE;
} while (getchar() != '\n');
}
/* Function: printWorld
* Description: Prints the world on the users screen.
* Input: None.
* Output: The field is printed out.
*/
void printWorld(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) {
for (int c = 0 ; c < cols ; c++) {
printf(" %c ", field[r][c].current);
}
printf("\n");
}
}
/* Function: printMenu
* Description: Prints the menu on the user screen.
* Input: None.
* Output: The menu is printed out.
*/
void printMenu(){
printf("Select one of the following options:\n");
printf(" (enter) Step\n");
printf(" (any) Exit\n");
}
/* Function: updateWorld
* Description: Updates the world.
* Input: None
* Output: The updated world.
*/
void updateWorld(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0 ; r < rows ; r++) {
for (int c = 0 ; c < cols ; c++) {
field[r][c].current = field[r][c].next;
}
}
}
/* Function: userChoice
* Description: Reads the choice of the user and prints out functions
* updateWorld and printMenu.
* Input: Choice of character.
* Output: The updated world of choice or end of program.
*/
void userChoice(const int rows, const int cols, cell field[rows][cols]) {
char choice;
do {
printWorld(rows, cols, field);
printMenu();
choice = getchar();
if (choice == '\n')
{
neighbours(rows, cols, field);
updateWorld(rows, cols, field);
}
} while(choice == '\n');
}
/* Function: neighbours
* Description: Looks for how many neighbours the cell have around it self and
* updates.
* Input: Nothing
* Output: The new generation of the cell.
*/
void neighbours(const int rows, const int cols, cell field[rows][cols]) {
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < rows; c++)
{
int alive=0;
if ((r + 1)< rows && (c - 1)>=0
&& field[r + 1][c - 1].current == ALIVE)
{
alive++;
}
if ((c - 1) >= 0 && field[r][c - 1].current == ALIVE)
{
alive++;
}
if ((r - 1) >= 0 && (c - 1) >= 0 && field[r - 1][c - 1].current == ALIVE)
{
alive++;
}
if ((r + 1 < rows) && field[r + 1][c].current == ALIVE)
{
alive++;
}
if ((r - 1) >= 0 && field[r - 1][c].current == ALIVE)
{
alive++;
}
if ((r + 1)< rows && (c + 1)< cols && field[r + 1][c + 1].current == ALIVE)
{
alive++;
}
if ((c + 1) < cols && field[r][c + 1].current == ALIVE)
{
alive++;
}
if ((r - 1) >= 0 && (c + 1) < cols && field[r - 1][c + 1].current == ALIVE)
{
alive++;
}
if (alive < 2)
{
field[r][c].next = DEAD;
}
else if (field[r][c].current == ALIVE && (alive == 2 || alive == 3))
{
field[r][c].next = ALIVE;
}
else if (alive > 3)
{
field[r][c].next = DEAD;
}
else if (field[r][c].current == DEAD && alive == 3)
{
field[r][c].next = ALIVE;
}
else{
field[r][c].next = DEAD;
}
}
}
}