-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlifefunc.c
229 lines (214 loc) · 6.92 KB
/
lifefunc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <life.h>
void gameOfLife(char *ruleString, int n, char oldField[][n], char newField[][n], char topology)
{
// check if ruleString is correct
int aliveLength = 0, deadLength = 0, afterSlash = 0;
while (*ruleString != '\0')
{
if (*ruleString != '/')
{
if (afterSlash)
{
deadLength++;
} else
{
aliveLength++;
}
} else
{
afterSlash++;
}
ruleString++;
}
if (afterSlash != 1 || aliveLength > 9 || deadLength > 9)
{ // ERROR-Exception
printf("ERROR: Rule-input should be of the form \"xxx/yyy.\n");
return;
} else
{
ruleString -= aliveLength+deadLength+1;
// find rule, e.g. "23/2" means:
// living cell with 2 or 3 neighbours stays alive, otherwise dies
// dead cell with 3 neighbours becomes alive, otherwise stays dead
int ruleAlive[aliveLength], ruleDead[deadLength];
int alive = 0, dead = 0; afterSlash = 0;
for (int m=0; m<aliveLength+deadLength+1; m++)
{
if (ruleString[m] != '/')
{
if (afterSlash)
{
ruleDead[dead] = charToInt(ruleString[m]);
dead++;
} else
{
ruleAlive[alive] = charToInt(ruleString[m]);
alive++;
}
} else
{
afterSlash++;
}
}
// e.g.: ruleAlive = [2 3], ruleDead = [3]; aliveLength = 2, deadLength = 1 (length of the rules)
// conpute newField
int livingCells;
for (int row=0; row<n; row++)
{
for (int column=0; column<n; column++)
{
livingCells = livingCellsInNeighbourhood(row,column,n,oldField,topology);
//printf("living cells in [%d,%d]: %d\n",row,column,livingCells);
if (deadOrAlive(livingCells, ruleDead, deadLength, ruleAlive, aliveLength, oldField[row][column]))
{
newField[row][column] = '1';
} else
{
newField[row][column] = '0';
}
}
}
} // } closes the else-part
return;
}
int deadOrAlive(int livingCells,int *ruleDead, int deadLength, int *ruleAlive, int aliveLength, char status)
{ // return 0 for dead, 1 for alive
// e.g.: ruleAlive = [2 3], ruleDead = [3]; aliveLength = 2, deadLength = 1 (length of the rules)
// living cell with 2 or 3 neighbours stays alive, otherwise dies
// dead cell with 3 neighbours becomes alive, otherwise stays dead
//printf("living cells: %d\nruleAlive = [%d,%d]\nruleDead = [%d]\ncell is alive: %c\n",livingCells,ruleAlive[0],ruleAlive[1],ruleDead[0],status);
int alive;
if (status == '1')
{ //alive
alive = 0;
for (int k=0; k<aliveLength; k++)
{
if (livingCells == ruleAlive[k])
{
alive = 1;
}
}
} else if (status == '0')
{ //dead
alive = 0;
for (int k=0; k<deadLength; k++)
{
if (livingCells == ruleDead[k])
{
alive = 1;
}
}
}
//printf("outcome: %d\n\n",alive);
return alive;
}
int livingCellsInNeighbourhood(int row, int column, int n, char field[][n], char topology)
{
int numberOfLivingCells = 0;
for (int k=-1; k<2; k++)
{
for (int l=-1; l<2; l++)//
{
if (k == 0 && l == 0)
continue;
if (k+row > -1 && k+row < n && l+column > -1 && l+column < n)
{ // no outburst; every topology
if (field[k+row][l+column] == '1')
{
numberOfLivingCells++;
}
} else if ((k+row == -1 || k+row == n) && l+column > -1 && l+column < n && topology != 'n')
{ // top/down outburst
if ((field[(k+row+n)%n][(l+column)] == '1' && topology == 't') || (field[(k+row+n)%n][(n-1-(l+column))] == '1' && topology == 'k') || (field[(k+row+n)%n][(n-1-(l+column))] == '1' && topology == 'p'))
{
numberOfLivingCells++;
}
} else if (k+row > -1 && k+row < n && (l+column == -1 || l+column == n) && topology != 'n')
{ // left/right outburst
if ((field[(k+row)][(l+column+n)%n] == '1' && topology == 't') || (field[(k+row)][(l+column+n)%n] == '1' && topology == 'k') || (field[(n-1-(k+row))][(l+column+n)%n] == '1' && topology == 'p'))
{
numberOfLivingCells++;
}
} else if (topology != 'n')
{ // corner outburst
if (field[(k+row+n)%n][(l+column+n)%n] == '1')
{
numberOfLivingCells++;
}
}
}
}
return numberOfLivingCells;
}
int charToInt(char c)
{
int i=0;
switch(c)
{
case '0': i=0; break;
case '1': i=1; break;
case '2': i=2; break;
case '3': i=3; break;
case '4': i=4; break;
case '5': i=5; break;
case '6': i=6; break;
case '7': i=7; break;
case '8': i=8; break;
case '9': i=9; break;
default: break;
}
return i;
}
int printField(int length, char field[][length], int booleanPrint)
{ // boleanPrint == 0: print, else: don't print
for (int k=0; k<length; k++)
{
for (int l=0; l<length; l++)
{
if (field[k][l] == '0' || field[k][l] == '1')
{
if (booleanPrint == 0) {printf("%c",field[k][l]);}
} else
{
return 1;
}
}
if (booleanPrint == 0) {printf("\n");}
}
//if (booleanPrint == 0) {printf("\n");}
return 0;
}
int transferStringToArray(char *string, int length, char field[][length])
{
int column = 0;
int row = 0;
while (*string != '\0')
{
if ((*string == '\\' && *(string+1) == 'n') || *string == ' ' || *string == '\n')
//jump zu next row
{
row++;
column = 0;
} else if (*(string-1) != '\\' || *string != 'n')//(strcmp(string[k],"\\") != 0 || strcmp(string[k+1],"n") != 0)
{
field[row][column] = *string;
//printf("%c in [row,column]: [%d,%d]\n",field[row][column],row,column);
column++;
}
string++;
}
return 0;
}
int checkLengthOfField(char *string)
{
int length=0;
while ((*string != '\0') && ((*string != '\\') && *(string+1) != 'n') && (*string != ' ') && (*string != '\n'))
{
length++;
string++;
}
return length;
}