-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathGameOfLife.h
195 lines (176 loc) · 4.91 KB
/
GameOfLife.h
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
#pragma once
#include <iostream>
#include <ostream>
#include <utility>
#include <set>
#include "ApprovalTests.hpp"
#include "GameOfLife.h"
struct Point
{
Point(int x, int y) : x_(x), y_(y)
{
}
bool operator==(const Point& rhs) const
{
return x_ == rhs.x_ && y_ == rhs.y_;
}
bool operator!=(const Point& rhs) const
{
return !(rhs == *this);
}
bool operator<(const Point& rhs) const
{
if (x_ < rhs.x_)
return true;
if (rhs.x_ < x_)
return false;
return y_ < rhs.y_;
}
bool operator>(const Point& rhs) const
{
return rhs < *this;
}
bool operator<=(const Point& rhs) const
{
return !(rhs < *this);
}
bool operator>=(const Point& rhs) const
{
return !(*this < rhs);
}
int x_;
int y_;
};
class GameOfLife
{
std::vector<Point> aliveCells;
std::string aliveCharacter = "X";
std::string deadCharacter = ".";
public:
explicit GameOfLife(int width,
int height,
const std::function<int(int, int)>& function)
{
std::vector<Point> points;
for (int x = 0; x <= width; ++x)
{
for (int y = 0; y <= height; ++y)
{
const Point point = Point(x, y);
points.push_back(point);
}
}
*this = GameOfLife(function, points);
}
GameOfLife(const std::function<int(int, int)>& function, std::vector<Point>& points)
{
for (const auto& point : points)
{
if (function(point.x_, point.y_))
{
aliveCells.push_back(point);
}
}
}
explicit GameOfLife(std::vector<Point>& alivePoints) : aliveCells(alivePoints)
{
}
explicit GameOfLife(const std::string& cells)
{
int x = 0;
int y = 0;
for (const auto& item : cells)
{
if (item == ' ')
{
continue;
}
if (item == '\n')
{
if (!(x == 0 && y == 0))
{
x = 0;
y += 1;
}
continue;
}
if (item == 'X')
{
aliveCells.push_back(Point(x, y));
}
x += 1;
}
}
bool isAlive(int x, int y) const
{
return (std::find(aliveCells.begin(), aliveCells.end(), Point(x, y)) !=
aliveCells.end());
}
std::string printCell(int x, int y) const
{
return isAlive(x, y) ? aliveCharacter : deadCharacter;
}
std::string print(int width, int height) const
{
return ApprovalTests::Grid::print(
width, height, [&](int x, int y, std::ostream& s) {
s << printCell(x, y) << " ";
});
}
std::vector<Point> getRelevantPoints() const
{
std::set<Point> points;
for (const auto& item : aliveCells)
{
points.insert(Point(item.x_ - 1, item.y_ - 1));
points.insert(Point(item.x_ - 1, item.y_ - 0));
points.insert(Point(item.x_ - 1, item.y_ + 1));
points.insert(Point(item.x_ - 0, item.y_ - 1));
points.insert(Point(item.x_ - 0, item.y_ - 0));
points.insert(Point(item.x_ - 0, item.y_ + 1));
points.insert(Point(item.x_ + 1, item.y_ - 1));
points.insert(Point(item.x_ + 1, item.y_ - 0));
points.insert(Point(item.x_ + 1, item.y_ + 1));
}
return std::vector<Point>(points.begin(), points.end());
}
GameOfLife advance() const
{
std::function<int(int, int)> function2 = [this](int x, int y) {
// clang-format off
int count =
this->isAlive(x - 1, y - 1) +
this->isAlive(x - 1, y - 0) +
this->isAlive(x - 1, y + 1) +
this->isAlive(x - 0, y - 1) +
// this->isAlive(x - 0, y - 0) +
this->isAlive(x - 0, y + 1) +
this->isAlive(x + 1, y - 1) +
this->isAlive(x + 1, y - 0) +
this->isAlive(x + 1, y + 1);
// clang-format on
return count == 3 || (count == 2 && this->isAlive(x, y));
};
auto points = getRelevantPoints();
auto newGame = GameOfLife(function2, points);
newGame.setAliveCell(aliveCharacter);
newGame.setDeadCell(deadCharacter);
return newGame;
}
// begin-snippet: storyboard_return_values
std::string setAliveCell(std::string alive)
{
aliveCharacter = std::move(alive);
return aliveCharacter;
}
// end-snippet
std::string setDeadCell(std::string dead)
{
deadCharacter = std::move(dead);
return deadCharacter;
}
std::string toString()
{
return print(5, 5);
}
};