-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
213 lines (195 loc) · 4.27 KB
/
Board.cpp
File metadata and controls
213 lines (195 loc) · 4.27 KB
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
#include "Board.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <list>
namespace Life {
using std::max;
using std::min;
using std::ostream;
using std::stringstream;
using std::string;
using std::pair;
using std::list;
/**
* @brief builds a square board
* @param size size of the board
**/
Board::Board (const int size, const unsigned int survival,
const unsigned int birth) : Board (size, size, survival, birth) {
}
/**
* @brief builds a w*h board
* @param h height
* @param w width
**/
Board::Board (const int h, const int w, const unsigned int survival,
const unsigned int birth) : board (h, w), survival (survival), birth (birth) {
}
Board::~Board() {
}
/**
* @brief const cell access
* @param r row
* @param c column
* @return cell at r,c (const)
**/
const bool &Board::operator() (const int r, const int c) const {
return board (r, c);
}
/**
* @brief cell access
* @param r row
* @param c column
* @return cell at r,c
**/
bool &Board::operator() (const int r, const int c) {
return board (r, c);
}
/**
* @brief const cell access
* @param p pair of (row, column)
* @return cell at row,column (const reference)
**/
const bool &Board::operator() (const pair<int, int> &p) const {
return (*this) (p.first, p.second);
}
/**
* @brief cell access
* @param p pair of (row,column)
* @return cell at row,column
**/
bool &Board::operator() (const pair<int, int> &p) {
return (*this) (p.first, p.second);
}
/**
* @brief performs a single step
* @return a reference to the board after the step
**/
Board &Board::step() {
Matrix<bool> result (board);
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
int neighbors = countNeighbors (i, j);
unsigned int mask = 1;
while (neighbors > 0) {
mask *= 2;
neighbors--;
}
if (board (i, j)) {
if ( (mask & survival) == 0) {
result (i, j) = false;
}
} else {
if ( (mask & birth) != 0) {
result (i, j) = true;
}
}
}
}
board = result;
return *this;
}
/**
* @brief counts number of neighbors of the given cell
* @param r row
* @param c column
* @return the number of neighbors
**/
int Board::countNeighbors (const int r, const int c) {
int count = 0;
for (int i = max (r - 1, 0); i <= min (r + 1, board.getHeight() - 1); i++) {
for (int j = max (c - 1, 0); j <= min (c + 1, board.getWidth() - 1); j++) {
if ( (i == r) && (j == c)) {
continue;
}
if (board (i, j)) {
count++;
}
}
}
return count;
}
/**
* @brief toggles the given coordinates
* @param r row
* @param c column
* @return *this
**/
Board &Board::toggle (const int r, const int c) {
(*this) (r, c) = ! (*this) (r, c);
return *this;
}
/**
* @brief toggles the given cell
* @param p pair of coordinates (row, column)
* @return *this
**/
Board &Board::toggle (const pair<int, int> &p) {
return toggle (p.first, p.second);
}
/**
* @brief updates a list of coordinates
* @param l the list of coordinates
* @param toggle sets to true if true, toggles if false (default is true)
* @return *this
**/
Board &Board::updateList (const list<pair<int, int>> &l, const bool update) {
for (auto & i: l) {
if (update) {
(*this) (i.first, i.second) = true;
} else {
toggle (i);
}
}
return *this;
}
/**
* @brief resets the board
* @return *this
**/
Board &Board::reset() {
for (int i = 0; i < board.getHeight(); i++) {
for (int j = 0; j < board.getWidth(); j++) {
(*this) (i, j) = false;
}
}
return *this;
}
/**
* @brief returns the height of the board
* @return the height of the board
**/
int Board::getHeight() const {
return board.getHeight();
}
/**
* @brief returns the width of the board
* @return the width of the board
**/
int Board::getWidth() const {
return board.getWidth();
}
/* external functions **/
ostream &operator<< (ostream &os, const Board &b) {
stringstream s;
char c;
s << b.board;
s.read (&c, 1);
while (!s.eof()) {
switch (c) {
case '0':
c = DEAD_CELL;
break;
case '1':
c = LIVING_CELL;
break;
}
os << c;
s.read (&c, 1);
}
return os;
}
}