-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
executable file
·214 lines (168 loc) · 5.23 KB
/
game.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
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 <array>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <utility>
#include <set>
#include <thread>
#include <fstream>
#include "gl_utils.hpp"
#include "game.hpp"
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
void initialize_from_random_soup(
Board &board, unsigned int width, unsigned int height) {
board = {};
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
int alive = rand() % 2 + 0.5;
if(alive == 1) board.insert({i, j});
}
}
}
void initialize_from_RLE(Board& board, const std::string& rle,
int startX, int startY) {
board = {};
int x = startX, y = startY;
std::istringstream rleStream(rle);
char ch;
while (rleStream.get(ch)) {
std::string countStr;
// Accumulate digits to form the count number
while (std::isdigit(ch)) {
countStr.push_back(ch);
if (!rleStream.get(ch)) {
break;
}
}
int count = countStr.empty() ? 1 : std::stoi(countStr);
switch (ch) {
case 'b': // Dead cell
case '.':
x += count;
break;
case 'o': // Alive cell
for (int i = 0; i < count; ++i, ++x) {
board.insert({x, y});
}
break;
case '$': // End of line
y += count;
x = startX;
break;
}
}
}
Board get_neighbours(Cell c) {
Board neighbours;
for(int dx = -1; dx <= 1; dx++){
for(int dy = -1; dy <= 1; dy++){
if(dx == 0 && dy == 0) continue;
neighbours.insert({c.first + dx, c.second + dy});
}
}
return neighbours;
}
void update_section(const Board &b, Board &slice,
size_t start_row, size_t end_row ) {
Board candidates;
auto it = b.begin();
std::advance(it, start_row);
// slice candidates by index
for(size_t i = start_row; i < end_row && it != b.end(); i++, it++){
candidates.insert(*it);
}
Board c_neighbours;
for(const Cell &c : candidates) {
Board neighbours = get_neighbours(c);
c_neighbours.insert(neighbours.begin(), neighbours.end());
}
candidates.insert(c_neighbours.begin(), c_neighbours.end());
for(const Cell &c : candidates){
int n_count = 0;
for(const Cell &n : get_neighbours(c)){
if(b.count(n)) n_count ++;
}
bool alive = b.count(c);
if(alive && (n_count == 2 || n_count == 3)) slice.insert(c);
else if(!alive && n_count == 3) slice.insert(c);
}
}
void threaded_get_next_board(
Board &board,
std::vector<std::thread> &threads,
int num_threads,
std::vector<Board> &slices){
int rows_per_thread = board.size() / num_threads;
Board next_board;
for(int i = 0; i < num_threads; i++){
int start_row = i * rows_per_thread;
int end_row = (i + 1) * rows_per_thread;
if(i == num_threads - 1){
end_row = board.size() - 1;
}
slices[i] = {};
threads[i] = std::thread(update_section,
std::ref(board),
std::ref(slices[i]),
start_row, end_row);
}
for(auto &t : threads){
t.join();
}
for(const Board &slice : slices){
next_board.insert(slice.begin(), slice.end());
}
board = std::move(next_board);
}
void print_to_term(Board &board, const int iterations, const bool print) {
int i = 0;
int num_threads = std::thread::hardware_concurrency();
std::vector<std::thread> threads(num_threads);
std::vector<Board> slices(num_threads);
while(i < iterations){
if(print) {
std::stringstream buffer;
buffer << "\033[2J\033[H";
for (const Cell& cell : board) {
unsigned int r = cell.first, c = cell.second;
buffer << "\033[" << r + 1 << ";" << c + 1 << "H";
buffer << "□";
}
// Print the entire buffer to std::cout in one go
clearScreen();
std::cout << buffer.str();
std::cout.flush();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
threaded_get_next_board(board, threads, num_threads, slices);
i++;
}
}
int main(int argc, char** argv) {
std::string RLE_file = "2c5-spaceship-gun-p416.txt";
//std::string RLE_file = "smaller-ship.txt";
//std::string RLE_file = "oscillator.txt";
//std::string RLE_file = "queen_bee.txt";
std::ifstream file("../rle_files/" + RLE_file);
if (!file.is_open()) {
std::cerr << "Error opening file: " << RLE_file << std::endl;
return 1;
}
std::stringstream buffer;
buffer << file.rdbuf();
file.close();
std::string contents = buffer.str();
const char* RLE = contents.c_str();
Board board = {};
unsigned int board_height = board.size() + 2;
unsigned int board_width = board.size() + 2;
initialize_from_RLE(board, RLE, board_width/2, board_height/2);
init_window(argc, argv, board);
return 0;
}