-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathparser.hpp
351 lines (280 loc) · 9.61 KB
/
parser.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// Created by jguegant on 2017-06-10.
//
#ifndef META_CRUSH_SAGA_PARSER_HPP
#define META_CRUSH_SAGA_PARSER_HPP
#include <tuple>
#include "board.hpp"
#include "constexpr_string.hpp"
#include "constexpr_string_view.hpp"
#include "game_state.hpp"
#include "utils.hpp"
struct BoardParameters
{
int row_count;
int column_count;
int score;
int moves;
int index_first_candy;
};
constexpr int extract_int(auto it)
{
int result = 0;
char digit = *it;
const int digits_max = 5; // assuming score and moves are less than million
int digits_count = 0;
while ('0' <= digit && digit <= '9' && digits_count <= digits_max) {
result = 10 * result + (digit - '0');
++it;
digit = (*it);
++digits_count;
}
return result;
}
CONSTEXPR int stoi(const constexpr_string_view& s)
{
int result = 0;
for (const char c : s) {
result = result * 10 + (c - '0');
}
return result;
}
CONSTEXPR auto itos(int x)
{
int digits = digits_amount(x);
constexpr_string<10> result;
for (int i = 0; i < digits; ++i) {
result[digits - 1 - i] = (x % 10) + '0';
x = x / 10;
}
return result.substr(0, digits);
}
constexpr char map_candy_type[] = { ' ', 'R', 'G', 'B', 'Y' };
constexpr char encode_candy_type(CandyType t)
{
return map_candy_type[static_cast<int>(t)];
}
constexpr CandyType decode_candy_type(char c)
{
switch (c) {
case ' ':
return CandyType::None;
case 'R':
return CandyType::Red;
case 'G':
return CandyType::Green;
case 'B':
return CandyType::Blue;
case 'Y':
return CandyType::Yellow;
default:
throw std::runtime_error("Invalid candy type");
}
}
struct state_decoration
{
char left;
char right;
char top;
char bottom;
};
constexpr state_decoration encode_candy_state(CandyState s)
{
state_decoration dec = { ' ', ' ', ' ', ' ' };
if (s.matched) {
dec = { '*', '*', '*', '*' };
}
return dec;
}
CONSTEXPR CandyState decode_candy_state(char up_char)
{
CandyState state = { false };
if (up_char == '*') {
state.matched = true;
} else if (up_char != ' ') {
throw std::runtime_error("Invalid matched state!");
}
return state;
}
constexpr int candy_size = 3;
constexpr int row_padding = 5; // space, pipe, pipe, space, newline
constexpr auto parse_board_parameters(auto str)
{
BoardParameters ret{ 0, 0, 0, 0, 0 };
constexpr int noncandy_rows = 2; // border, border
constexpr int lines_after_topborder_end_to_candy = 1; // empty
constexpr int offset_candy_after_topborder_end = 4; // space, space, pipe, space
auto gs_begin = str.cbegin();
auto gs_end = str.cend();
auto topborder_lowerbound = find(gs_begin + 1, gs_end, '\n');
auto topborder_end = find(topborder_lowerbound + 1, gs_end, '\n');
auto field_width = topborder_end - topborder_lowerbound;
ret.column_count = (field_width - row_padding) / candy_size;
auto score_start = find(gs_begin + 1, gs_end, '>');
ret.row_count = (((score_start - topborder_lowerbound) / field_width) - noncandy_rows) / candy_size;
ret.index_first_candy = (topborder_end - gs_begin) + (lines_after_topborder_end_to_candy * field_width) + offset_candy_after_topborder_end;
const auto score_value_offset = 9; // length of "> score: "
ret.score = extract_int(score_start + score_value_offset);
auto moves_start = find(score_start + 1, gs_end, '>');
const auto moves_value_offset = 9; // length of "> moves: "
ret.moves = extract_int(moves_start + moves_value_offset);
return ret;
}
template <class GameString>
CONSTEXPR auto parse_board()
{
constexpr auto board_string = GameString{}();
constexpr auto board_parameters = parse_board_parameters(board_string);
constexpr int column_count = board_parameters.column_count;
constexpr int row_count = board_parameters.row_count;
constexpr int index_first_candy = board_parameters.index_first_candy;
bool any_hovered = false;
int hovered_x = 0;
int hovered_y = 0;
bool any_selected = false;
int selected_x = 0;
int selected_y = 0;
std::array<std::array<candy, column_count>, row_count> board{};
constexpr int width = ((column_count * candy_size) + row_padding);
constexpr int candy_state_offset = (-width); // same horisontal coordinates at previous row
for (int i = 0; i < row_count; ++i) {
for (int j = 0; j < column_count; ++j) {
const int candy_type_index = i * candy_size * width + j * candy_size + index_first_candy;
const int candy_matched = candy_type_index + candy_state_offset;
board[i][j] = candy{
decode_candy_type(board_string[candy_type_index]),
decode_candy_state(board_string[candy_matched])
};
char selected_or_hovered = board_string[candy_type_index - 1];
if (selected_or_hovered == '[') {
any_selected = true;
selected_x = i;
selected_y = j;
} else if (selected_or_hovered == '(') {
any_hovered = true;
hovered_x = i;
hovered_y = j;
} else if (selected_or_hovered != ' ' && selected_or_hovered != '*') {
throw std::runtime_error("Invalid hover state!");
}
}
}
if (!any_hovered) {
if (any_selected) {
hovered_x = selected_x;
hovered_y = selected_y;
} else {
hovered_x = row_count / 2;
hovered_y = column_count / 2;
}
}
return GameState<row_count, column_count>{ board, hovered_x, hovered_y, any_selected, selected_x, selected_y, board_parameters.score, board_parameters.moves };
}
template <class GameString>
CONSTEXPR auto parse_score(GameString&& get_game_state_string)
{
constexpr auto str = get_game_state_string();
auto score_begin = find(str.cbegin(), str.cend(), ':') + 2;
auto score_end = find(score_begin, str.cend(), '\n');
return stoi({ score_begin, static_cast<int>(score_end - score_begin) });
}
template <class GameString>
CONSTEXPR auto parse_moves(GameString&& get_game_state_string)
{
constexpr auto str = get_game_state_string();
auto score_begin = find(str.cbegin(), str.cend(), ':') + 1;
auto moves_begin = find(score_begin, str.cend(), ':') + 2;
auto moves_end = find(moves_begin, str.cend(), '\n');
return stoi({ moves_begin, static_cast<int>(moves_end - moves_begin) });
}
template <class GameString>
CONSTEXPR auto parse_game_state()
{
CONSTEXPR auto board = parse_board<GameString>();
return board;
}
template <std::size_t RowCount, std::size_t ColumnCount>
CONSTEXPR auto print_board_to_array(const game_engine<RowCount, ColumnCount>& engine)
{
auto board = engine.get_board();
constexpr int width = ((ColumnCount * candy_size) + row_padding);
constexpr auto e = [](CandyState s) constexpr
{
return encode_candy_state(s);
};
constexpr int board_size_in_char = width * ((RowCount * candy_size) + 2);
constexpr_string<board_size_in_char> result;
int cursor = 0;
#define W(C) result[cursor++] = C
#define NEWLINE() \
W(' '); \
W('|')
#define ENDLINE() \
W('|'); \
W(' '); \
W('\n')
#define EPILOG() \
W(' '); \
W(' '); \
for (int r = 0; r < width - row_padding; ++r) { \
W('-'); \
} \
W(' '); \
W(' '); \
W('\n')
EPILOG();
for (const auto& row : board) {
NEWLINE();
for (const auto& column : row) {
W(' ');
W(e(column.state).top);
W(' ');
}
ENDLINE();
NEWLINE();
for (const auto& column : row) {
W(e(column.state).left);
W(encode_candy_type(column.type));
W(e(column.state).right);
}
ENDLINE();
NEWLINE();
for (const auto& column : row) {
W(' ');
W(e(column.state).bottom);
W(' ');
}
ENDLINE();
}
EPILOG();
auto hovered_x = engine.get_hovered_x();
auto hovered_y = engine.get_hovered_y();
auto hovered_center = (hovered_y * candy_size + 3) + (hovered_x * candy_size + 2) * width;
result[hovered_center - 1] = '(';
result[hovered_center + 1] = ')';
if (engine.is_any_selected()) {
auto selected_x = engine.get_selected_x();
auto selected_y = engine.get_selected_y();
auto selected_center = (selected_y * candy_size + 3) + (selected_x * candy_size + 2) * width;
result[selected_center - 1] = '[';
result[selected_center + 1] = ']';
}
return result;
}
CONSTEXPR auto print_score(auto& engine)
{
return constexpr_string("> score: ").append(itos(engine.get_score())).append(constexpr_string("\n"));
}
CONSTEXPR auto print_moves(auto& engine)
{
return constexpr_string("> moves: ").append(itos(engine.get_moves())).append(constexpr_string("\n"));
}
CONSTEXPR auto print_game_state(auto& engine)
{
auto result = constexpr_string(" Meta crush saga \n");
auto board = print_board_to_array(engine);
auto score = print_score(engine);
auto moves = print_moves(engine);
return result.append(board).append(score).append(moves);
}
#endif //TEMPLATE_CRUSH_SAGA_PARSER_HPP