-
Notifications
You must be signed in to change notification settings - Fork 2
/
clife.hpp
217 lines (189 loc) · 5.17 KB
/
clife.hpp
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
#pragma once
#include <iostream>
#include <vector>
#include <stdexcept>
#include <stdlib.h>
extern "C" {
#include "xxhash/xxhash.c"
}
/** Written by Sjors Gielen, eth0 winter 2014
* Feel free to use this for anything you like
*/
/**
* A ValueType must be copyable and implement:
* - ValueType() -> unset
* - ValueType(vector<ValueType>) -> set, init from 8 neighbors, 3 are set
* - ValueType(bool) -> set, init randomly
* - operator bool() const -> return if set
* - char ValueType::getChar() const -> get one character for terminal print
* - void ValueType::print(ostream&) const -> print whatever you want
* - std::string hash() const -> return some hash unique to this value
*/
template <typename ValueType>
struct GameOfLifeRow {
typedef std::vector<ValueType> VectorType;
typedef typename VectorType::iterator IteratorType;
GameOfLifeRow(int w, IteratorType row_ptr)
: width(w)
, row_ptr(row_ptr) {
}
ValueType &operator[](unsigned int x) {
if(x >= width) {
throw std::runtime_error("Out of bounds");
}
IteratorType copy = row_ptr;
while(x-- > 0) {
copy++;
}
return *copy;
}
private:
int width;
IteratorType row_ptr;
};
template <typename ValueType, bool wrapping = true>
struct GameOfLifeField {
GameOfLifeField(int w, int h)
: field(w * h, ValueType())
, height(h)
, width(w) {
}
void resize(int w, int h) {
std::vector<ValueType> newfield(w * h, ValueType());
int minwidth = w < width ? w : width;
int minheight = h < height ? h : height;
for(int x = 0; x < minwidth; ++x) {
for(int y = 0; y < minheight; ++y) {
newfield[y * w + x] = field[y * width + x];
}
}
height = h;
width = w;
field = std::move(newfield);
}
inline int get_height() { return height; }
inline int get_width() { return width; }
GameOfLifeRow<ValueType> operator[](unsigned int y) {
if(y >= height) {
throw std::runtime_error("Out of bounds");
}
return GameOfLifeRow<ValueType>(width, field.begin() + y * width);
}
bool is_in_bounds(int y, int x) {
return y >= 0 && y < height && x >= 0 && x < width;
}
bool is_inside_and_set(int y, int x) {
return is_in_bounds(y, x) && is_set(y, x);
}
void add_if_inside_and_set(std::vector<ValueType> &vt, int y, int x) {
if(is_inside_and_set(y, x)) {
vt.push_back(field[y * width + x]);
}
}
void add_if_inside_or_wrapping_and_set(std::vector<ValueType> &vt, int y, int x) {
if(wrapping) {
y += height;
y %= height;
x += width;
x %= width;
}
return add_if_inside_and_set(vt, y, x);
}
bool is_set(int y, int x) {
if(!is_in_bounds(y, x)) {
throw std::runtime_error("Out of bounds");
}
return field[y * width + x];
}
void neighbors_set(int y, int x, std::vector<ValueType> &neigh) {
add_if_inside_or_wrapping_and_set(neigh, y-1, x-1);
add_if_inside_or_wrapping_and_set(neigh, y-1, x );
add_if_inside_or_wrapping_and_set(neigh, y-1, x+1);
add_if_inside_or_wrapping_and_set(neigh, y , x-1);
add_if_inside_or_wrapping_and_set(neigh, y , x+1);
add_if_inside_or_wrapping_and_set(neigh, y+1, x-1);
add_if_inside_or_wrapping_and_set(neigh, y+1, x );
add_if_inside_or_wrapping_and_set(neigh, y+1, x+1);
}
void nextState() {
std::vector<ValueType> new_field(width * height, ValueType());
std::vector<ValueType> neighbors;
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
neighbors.clear();
neighbors_set(y, x, neighbors);
int num = neighbors.size();
bool set = is_set(y, x);
if(set && (num == 2 || num == 3)) {
new_field[y * width + x] = field[y * width + x];
new_field[y * width + x].age_once();
} else if(num == 3) {
new_field[y * width + x] = ValueType(neighbors);
}
}
}
field = new_field;
}
void print(std::ostream &os) {
os << "+";
for(int j = 0; j < width; ++j) {
os << "-";
}
os << "+" << std::endl;
for(int i = 0; i < height; ++i) {
os << "|";
for(int j = 0; j < width; ++j) {
os << field[i * width + j].getChar();
}
os << "|" << std::endl;
}
os << "+";
for(int j = 0; j < width; ++j) {
os << "-";
}
os << "+" << std::endl << std::endl;
}
void print_simple(std::ostream &os) {
for(int i = 0; i < height; ++i) {
if(i == 0) {
field[0].begin_screen(os);
}
for(int j = 0; j < width; ++j) {
if(j == 0) {
field[i * width].begin_line(os);
}
field[i * width + j].print(os);
if(j == width - 1) {
field[i * width + j].end_line(os);
}
}
if(i == height - 1) {
field[i].end_screen(os);
}
}
os << std::flush;
}
uint64_t field_hash() const {
XXH3_state_t state;
XXH3_64bits_reset(&state);
for(auto const &cell : field) {
bool value = bool(cell);
XXH3_64bits_update(&state, &value, sizeof(value));
}
return XXH3_64bits_digest(&state);
}
void generateRandom(int chance_set) {
if(chance_set < 0 || chance_set > 100) {
throw std::runtime_error("Out of bounds");
}
for(int i = 0; i < height; ++i) {
for(int j = 0; j < width; ++j) {
field[i * width + j] = ValueType(rand() % 100 < chance_set ? true : false);
}
}
}
private:
std::vector<ValueType> field;
int height;
int width;
};