-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.cpp
104 lines (78 loc) · 2.17 KB
/
logic.cpp
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
#include "logic.h"
#include <time.h>
#include <assert.h>
Logic::Logic() {
field[0] = {1, 1, 1, 0};
field[1] = {0, 1, 1, 1};
field[2] = {1, 1, 1, 0};
/*field[0][0] = 1;
field[99][99] = 1;
field[0][99] = 1;
field[99][0] = 1;*/
}
void Logic::setField(const Field& field) {
this->field = field;
}
void Logic::randomizeField() {
srand(clock());
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
field[c][i] = rand()%2;
}
}
}
void Logic::updateField() {
char adjastent[100][100];
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
adjastent[c][i] = getAdjastentCellsNum(c , i);
}
}
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
if(field[c][i] == Dead and
adjastent[c][i] == 3) {
field[c][i] = Live;
}
}
}
for(short c = 0; c < 100; c++) {
for(short i = 0; i < 100; i++) {
if(field[c][i] == Live and
(adjastent[c][i] < 2 or
adjastent[c][i] > 3)) {
field[c][i] = Dead;
}
}
}
}
const Logic::Field& Logic::getField() const {
return field;
}
char Logic::getAdjastentCellsNum(char x, char y) const {
assert(x < 100 and x > -1 and
y < 100 and y > -1);
char live_cells = 0;
if(field[x][y] != 0) live_cells--;
if(x != 0 and y != 0 and
x != 99 and y != 99) {
for(short c = -1; c < 2; c++)
for(short i = -1; i < 2; i++)
if(field[x+c][y+i] != 0)
live_cells++;
} else {
for(short c = -1; c < 2; c++) {
for(short i = -1; i < 2; i++) {
char checked_x = c+x;
char checked_y = i+y;
if(checked_x == -1) checked_x = 99;
if(checked_y== -1) checked_y = 99;
if(checked_x == 100) checked_x = 0;
if(checked_y == 100) checked_y = 0;
if(field[checked_x][checked_y] != 0)
live_cells++;
}
}
}
return live_cells;
}