-
Notifications
You must be signed in to change notification settings - Fork 2
/
game.cpp
559 lines (504 loc) · 14.1 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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <vector>
#include <cstring>
#include <cassert>
#include "game.hpp"
namespace GwanKei {
bool is_valid_game_piece_id(int id) {
return (0 <= id && id <= 1+(25+1)*4);
}
bool is_valid_layout_index(int index) {
return (0 <= index && index < 25);
}
bool is_valid_layout(const Piece *data) {
int count[42] = {0};
for(int i=0; i<25; i++) {
Cell cell = convert_layout_index_to_cell(i);
if( (cell.get_y() == 1 && data[i].get_id() == 0)
|| (cell.get_y() < 5 && data[i].get_id() == 41) ) {
return false;
}
// 計數
count[data[i].get_id()]++;
}
for(int i=0; i<12; i++) {
if(count[PIECES[i]] != NUMBER_OF_PIECES[PIECES[i]])
return false;
}
if(data[convert_cell_to_layout_index(Cell(1,6,2,0))] != Piece(31)
&& data[convert_cell_to_layout_index(Cell(1,6,2,1))] != Piece(31))
return false;
return true;
}
Orient convert_player_to_orient(Player player) {
return static_cast<Orient>(player+1);
}
Player convert_orient_to_player(Orient orient) {
assert(orient != Central);
return static_cast<Player>(orient-1);
}
Cell convert_layout_index_to_cell(int index, Player player /* = Orange */) {
assert(is_valid_layout_index(index));
return Cell(
convert_player_to_orient(player),
Y_OF_INDEX[index],
X_OF_INDEX[index],
LR_OF_INDEX[index]
);
}
int convert_cell_to_layout_index(const Cell& cell) {
// Central = -2
if(cell.get_group() == Central)
return -2;
// Ordinary
int t = 18*cell.get_lr()+6*(cell.get_x()-1) + cell.get_y()-1;
// Camp = -1
return INDEX_OF_CELL[t];
}
Layout::Layout(bool masked /* = false */) {
assert(is_valid_layout(this->data));
this->masked = masked;
}
Layout::Layout(const Piece *data) {
assert(is_valid_layout(data));
for(int i=0; i<25; i++)
this->data[i] = data[i];
}
bool Layout::is_masked() const {
return this->masked;
}
Piece Layout::get(int index) const {
assert(!is_masked());
return data[index];
}
Piece Layout::get(int y, int x, LeftRight lr) const {
assert(!is_masked());
return data[convert_cell_to_layout_index(Cell(South,y,x,lr))];
}
bool Layout::is_able_to_swap(int index1, int index2) const {
assert(!is_masked());
Piece copy[25];
for(int i=0; i<25; i++)
copy[i] = data[i];
copy[index1] = data[index2];
copy[index2] = data[index1];
return is_valid_layout(copy);
}
void Layout::swap(int index1, int index2) {
assert(!is_masked());
assert(is_able_to_swap(index1, index2));
Piece t = data[index1];
data[index1] = data[index2];
data[index2] = t;
}
std::string Layout::to_string() const {
std::string result = "";
if(is_masked()) {
result = "[Masked]";
} else {
for(int y=1; y<=6; y++) {
for(int t=1; t<=5; t++) {
int x;
LeftRight lr;
if(t <= 3) {
x = t;
lr = Left;
} else if(t == 4) {
x = 2;
lr = Right;
} else {
x = 1;
lr = Right;
}
int index = convert_cell_to_layout_index(Cell(South,y,x,lr));
if(index == -1)
result += "[ ]";
else if(data[index].get_id() == 0)
result += "[00]";
else
result += "[" + std::to_string(data[index].get_id()) + "]";
result += " ";
} // for t in 1..5
result += "\n";
} // for y in 1..6
} // masked ?
return result;
}
Layout& Layout::operator = (const Layout& right) {
std::copy(right.data, right.data+25, data);
this->masked = right.masked;
return *this;
}
Element::Element() {
// empty
// this->id = 0;
}
Element::Element(Player player, int layout_index) {
assert(is_valid_layout_index(layout_index));
this->id = 1+player*26+(layout_index+1);
}
int Element::get_id() const {
return id;
}
bool Element::is_empty() const {
return (id == 0);
}
Player Element::get_player() const {
assert(!is_empty());
return static_cast<Player>((id-1) / 26);
}
bool Element::is_unknown() const {
return unknown;
}
void Element::set_unknown() {
unknown = true;
}
void Element::set_known() {
unknown = false;
}
int Element::get_layout_index() const {
assert(!is_empty());
return ((id-1)%26 - 1);
}
bool Element::is_first_row() const {
if(!is_empty()) {
Cell cell = convert_layout_index_to_cell(get_layout_index());
return (cell.get_y() == 1);
} else {
return false;
}
}
bool Element::is_last_two_rows() const {
if(!is_empty()) {
Cell cell = convert_layout_index_to_cell(get_layout_index());
return (cell.get_y() >= 5);
} else {
return false;
}
}
Element& Element::operator = (const Element& right) {
this->id = right.id;
return *this;
}
Feedback::Feedback() {
// null
}
Feedback::Feedback(
MoveResult move_result, const std::list<Cell>& route, bool show_flag
) {
this->move_result = move_result;
this->route = route;
this->show_flag = show_flag;
}
bool Feedback::is_nothing() const {
return (move_result == Nothing);
}
bool Feedback::is_route_turned() const {
std::vector<Cell> route_arr;
std::vector<Bound> bounds;
for(auto I=route.begin(); I!=route.end(); I++) {
route_arr.push_back(*I);
}
for(int i=0; (i+1)<route_arr.size(); i++) {
Cell current = route_arr[i];
Cell next = route_arr[i+1];
std::list<Bound> adjacents = current.get_adjacents();
for(auto J=adjacents.begin(); J!=adjacents.end(); J++) {
if(J->get_target() == next) {
bounds.push_back(*J);
} // the corressponding bound
} // for adjacents
} // for i (index of arr)
for(int i=0; (i+1)<bounds.size(); i++) {
Bound current = bounds[i];
Bound next = bounds[i+1];
if(!next.is_linkable(current, false)) {
// 不能轉向的話接不上
// 結論:轉向了
return true;
}
}
return false;
}
bool Feedback::is_flag_shown() const {
return show_flag;
}
MoveResult Feedback::get_move_result() const {
return move_result;
}
std::list<Cell> Feedback::get_route() const {
return route;
}
Cell Feedback::get_moved_cell() const {
return route.front();
}
Cell Feedback::get_target_cell() const {
return route.back();
}
Feedback& Feedback::operator = (const Feedback& right) {
this->move_result = right.move_result;
this->route = right.route;
this->show_flag = right.show_flag;
return *this;
}
void Game::init_board() {
for(int i=0; i<4; i++) {
Player player = static_cast<Player>(i);
for(int j=0; j<25; j++) {
if(enabled[i]) {
Cell cell = convert_layout_index_to_cell(j, player);
board[cell.get_id()] = Element(player, j);
}
} // for j in 0..24
} // for i in 0..3
}
Game::Game(const Layout& layout_S, const Layout& layout_N, bool EW /*=1*/) {
if(!EW) {
layout[convert_orient_to_player(South)] = layout_S;
layout[convert_orient_to_player(North)] = layout_N;
enabled[convert_orient_to_player(South)] = true;
enabled[convert_orient_to_player(North)] = true;
} else {
layout[convert_orient_to_player(East)] = layout_S;
layout[convert_orient_to_player(West)] = layout_N;
enabled[convert_orient_to_player(East)] = true;
enabled[convert_orient_to_player(West)] = true;
}
init_board();
}
Game::Game(const Layout* layouts) {
for(int i=0; i<4; i++) {
this->layout[i] = layouts[i];
this->enabled[i] = true;
}
init_board();
}
bool Game::is_1v1() const {
for(int i=0; i<4; i++) {
if(!enabled[i]) {
return true;
}
}
return false;
}
Element Game::element_of(Cell cell) const {
return board[cell.get_id()];
}
Piece Game::piece_of(Element element) const {
assert(!element.is_empty() && !element.is_unknown());
Player player = element.get_player();
/**
* Something Ugly:
* 布局不明的玩家的已知棋子 -> 無司令以致亮出的軍旗
* 對於非一般玩法,該邏輯需要調整
*/
assert(!(layout[player].is_masked() && !show_flag[player]));
if(layout[player].is_masked() && show_flag[player])
return Piece(31);
else
return layout[player].get(element.get_layout_index());
}
std::list<Cell> Game::reachables_of(Cell cell, bool able_to_turn) const {
std::list<Cell> result;
for(int i=0; i<4631; i++) {
if(is_valid_cell_id(i)) {
Cell target(i);
if(is_movable(cell, target, true, able_to_turn? Piece(32): Piece(0))) {
result.push_back(target);
} // movable
} // valid cell id
} // for cell id
return result;
}
bool Game::is_movable(Cell from, Cell to, bool assume, Piece piece) const {
Element from_element = board[from.get_id()];
Element to_element = board[to.get_id()];
assert(!from_element.is_empty());
if(!assume) {
assert(!from_element.is_unknown());
piece = piece_of(from_element);
}
if(from_element.is_empty() || from_element.is_unknown()) {
return false;
} else if(from.get_type() == Headquarter) {
return false;
} else if(!to_element.is_empty() && to.get_type() == Camp) {
return false;
} else if(piece == Piece(41)) {
return false;
} else if(
!to_element.is_empty()
&& (to_element.get_player() == from_element.get_player())
) {
return false;
} else if(
!to_element.is_empty()
&& (
int(to_element.get_player())
== (from_element.get_player()+2)%4
)
&& !is_1v1()
) {
return false;
} else {
bool occupy_state[4631] = {0};
for(int i=0; i<4631; i++) {
if(is_valid_cell_id(i)) {
occupy_state[i] = !board[i].is_empty();
} // if i is a valid cell id
} // for i in 0..4630
std::list<Cell> route = get_route(
from, to, occupy_state, piece == Piece(32)
);
if(route.empty())
return false;
else
return true;
}
}
bool Game::has_living_piece(Player player) const {
assert(enabled[player]);
for(int i=0; i<4631; i++) {
if(is_valid_cell_id(i)) {
if(!board[i].is_empty()) {
assert(!board[i].is_unknown());
if(board[i].get_player() == player) {
Cell cell(i);
std::list<Bound> bounds = cell.get_adjacents();
for(auto I=bounds.begin(); I!=bounds.end(); I++) {
if(is_movable(cell, I->get_target()) ) {
return true;
} // movable
} // for adjacents
} // this player
} // not empty
} // valid cell id
} // for cell id
return false;
}
Feedback Game::move(Cell from, Cell to, MoveResult force_result) {
Element from_element = board[from.get_id()];
Element to_element = board[to.get_id()];
assert(!from_element.is_empty());
assert(from.get_type() != Headquarter);
bool occupy_state[4631] = {0};
for(int i=0; i<4631; i++) {
if(is_valid_cell_id(i)) {
occupy_state[i] = !board[i].is_empty();
}
}
MoveResult result;
std::list<Cell> route;
bool show_flag = false;
if(!from_element.is_unknown() && !to_element.is_unknown()) {
assert(force_result == Nothing);
// count the number of 40
int n_of_40_before = 0;
if(!from_element.is_empty() && piece_of(from_element) == Piece(40)) {
n_of_40_before++;
}
if(!to_element.is_empty() && piece_of(to_element) == Piece(40)) {
n_of_40_before++;
}
// process board
if(!to_element.is_empty()) {
result = Piece::attack(piece_of(from_element), piece_of(to_element));
} else {
result = Nothing;
}
route = get_route(
from, to, occupy_state, piece_of(from_element) == Piece(32)
);
assert(!route.empty());
if(result == Nothing || result == Bigger) {
board[to.get_id()] = from_element;
} else if(result == Equal) {
board[to.get_id()] = Element();
}
board[from.get_id()] = Element();
// count the number of 40
int n_of_40_after = 0;
Element target = board[to.get_id()];
if(!target.is_empty() && !target.is_unknown()) {
if(piece_of(target) == Piece(40)) {
n_of_40_after++;
}
}
if(n_of_40_after < n_of_40_before) {
show_flag = true;
}
} else {
assert(force_result != Nothing);
result = force_result;
route = get_route(from, to, occupy_state, true);
assert(!route.empty());
if(result == Nothing || result == Bigger) {
board[to.get_id()] = from_element;
} else if(result == Equal) {
board[to.get_id()] = Element();
}
board[from.get_id()] = Element();
}
steps++;
last_feedback = Feedback(result, route, show_flag);
return last_feedback;
}
void Game::annihilate(Player player) {
for(int i=0; i<4631; i++) {
if(is_valid_cell_id(i)) {
if(!board[i].is_empty() && board[i].get_player() == player) {
// set empty
board[i] = Element();
}
} // is valid cell id
} // for cell id
}
Game Game::get_game_with_mask(Player perspective, MaskMode mask_mode) const {
Game result = *this;
auto set_unknown = [&result, perspective, this](int delta) {
Player player = static_cast<Player>((perspective+delta)%4);
if(!enabled[player]) {
return;
}
result.layout[player] = Layout::Masked();
bool has40 = false;
int pos31 = -1;
for(int j=0; j<4631; j++) {
if(is_valid_cell_id(j)) {
if(!board[j].is_empty()
&& board[j].get_player() == player) {
if(piece_of(board[j]).get_id() == 40) {
has40 = true;
}
if(piece_of(board[j]).get_id() == 31) {
pos31 = j;
}
result.board[j].set_unknown();
} // not empty and is this player
} // valid cell id
} // for cell id
if(!has40) {
result.show_flag[player] = true;
if(pos31 != -1)
result.board[pos31].set_known();
}
};
if(mask_mode == NoExpose) {
for(int i=1; i<=3; i++) {
set_unknown(i);
}
} else if(mask_mode == DoubleExpose) {
set_unknown(1);
set_unknown(3);
}
return result;
}
Game& Game::operator = (const Game& right) {
memcpy(board, right.board, 4631*sizeof(Element));
memcpy(layout, right.layout, 4*sizeof(Layout));
memcpy(enabled, right.enabled, 4*sizeof(bool));
memcpy(show_flag, right.show_flag, 4*sizeof(bool));
steps = right.steps;
last_feedback = right.last_feedback;
return *this;
}
}