-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReferee.cpp
466 lines (423 loc) · 15.5 KB
/
Referee.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
#include "Referee.h"
#include <algorithm>
void Referee::setBoard(const std::shared_ptr<Board> & board)
{
if (!board_)
{
board_ = board;
board_->width(9);
board_->heigth(9);
board_->registerHandler([this]() {
// Action to do when board is changing
validPawns_.clear();
validwalls_.clear();
validMoves_.clear();
});
}
}
void Referee::setPlayers(const std::shared_ptr<std::vector<Player*>> & players)
{
players_ = players;
for (auto& p : *players_)
{
if (p->startPosition() == BoardPosition("e1"))
{
const auto& arrival_e1 = { "a9", "b9", "c9", "d9", "e9", "f9", "g9", "h9", "i9", };
for (const auto &arrival : arrival_e1)
{
p->addArrival(std::string(arrival));
}
}
else if (p->startPosition() == BoardPosition("e9"))
{
const auto& arrival_e9 = { "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1", "i1" };
for (const auto &arrival : arrival_e9)
{
p->addArrival(std::string(arrival));
}
}
}
}
void Referee::launch()
{
// Initialize start position for players
for (const auto& p : *players_)
{
Move move = PawnPosition(p->startPosition(), p->name(), p->color());
board_->add(move);
}
illegalMove_.clear(); // No illegal move
// Action to do when board is changing
validPawns_.clear();
validwalls_.clear();
validMoves_.clear();
}
bool Referee::Win(const PlayerName & name) const
{
if (!illegalMove_.empty() && (illegalMove_ != name))
return true;
auto& player = find(name);
auto currentPosition = board_->getPawn(name);
for (const auto& arrival : player.arrivalPosition())
{
if (currentPosition == arrival)
return true;
}
return false;
}
void Referee::illegalMove(const PlayerName &name)
{
illegalMove_ = name;
}
bool Referee::ValidWall(const WallPosition &wall) const
{
// Check if player have at least 1 wall ==> done by getValidWalls()
// Check range of walls ==> Already done in 'WallPosition'
// Check if wall is already on board or crosses another wall
auto crossed = wall;
crossed.direction(wall.oppositeDirection());
std::vector<WallPosition> badWalls;
badWalls.push_back(wall);
badWalls.push_back(crossed);
if (wall.direction() == WallPosition::Direction::horizontal)
{
try { badWalls.push_back(wall + Position{ -1,0 }); }
catch (std::out_of_range) {}
try { badWalls.push_back(wall + Position{ 1,0 }); }
catch (std::out_of_range) {}
}
else
{
try { badWalls.push_back(wall + Position{ 0,-1 }); }
catch (std::out_of_range) {}
try { badWalls.push_back(wall + Position{ 0,1 }); }
catch (std::out_of_range) {}
}
for (const auto& w : badWalls)
{
if (board_->existsWall(w))
return false;
}
// Check if wall close the path to arrival
for (const auto& p : *players_)
{
if (!board_->findPath(wall, p->name(), p->arrivalPosition()))
return false;
}
return true;
}
bool Referee::ValidPawn(const PawnPosition &pawn) const
{
auto name = pawn.playerName();
auto currentPawn = board_->getPawn(name);
// Check range of pawns ==> Already done in 'PawnPosition'
// Check if a pawn is already in this position
if (board_->existsPawn(pawn))
return false;
// Check if pawns is a legal move
auto d = currentPawn.distance(pawn);
auto length = d.length();
if (length == 1)
{
// (next to player pawns and no wall between 2 positions)
// Verify the pawn moves to next square
if ((d == Position(1, 0)) || (d == Position(-1, 0))
|| (d == Position(0, 1)) || (d == Position(0, -1)))
{
// Verify no wall block the move
if (noBlockerWalls(currentPawn, pawn))
return true;
}
else
{
// Pawn jumps over another pawn, but bounces on wall behind it.
std::vector<std::tuple<Position, Position, WallPosition>> tests;
//std::vector< distance , opponent, wall >> tests;
//{
// distance,
// opponent,
// wall,
//},
try {
tests.emplace_back(
Position(1, 1),
(Position)currentPawn + Position(0, -1),
WallPosition(currentPawn.x(), currentPawn.y() - 2, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, 1),
(Position)currentPawn + Position(0, -1),
WallPosition(currentPawn.x() - 1, currentPawn.y() - 2, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, 1),
(Position)currentPawn + Position(-1, 0),
WallPosition(currentPawn.x() - 2, currentPawn.y(), WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, 1),
(Position)currentPawn + Position(-1, 0),
WallPosition(currentPawn.x() - 2, currentPawn.y() - 1, WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, -1),
(Position)currentPawn + Position(-1, 0),
WallPosition(currentPawn.x() - 2, currentPawn.y(), WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, -1),
(Position)currentPawn + Position(-1, 0),
WallPosition(currentPawn.x() - 2, currentPawn.y() - 1, WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, -1),
(Position)currentPawn + Position(0, 1),
WallPosition(currentPawn.x(), currentPawn.y() + 1, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(1, -1),
(Position)currentPawn + Position(0, 1),
WallPosition(currentPawn.x() - 1, currentPawn.y() + 1, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, -1),
(Position)currentPawn + Position(0, 1),
WallPosition(currentPawn.x(), currentPawn.y() + 1, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, -1),
(Position)currentPawn + Position(0, 1),
WallPosition(currentPawn.x() - 1, currentPawn.y() + 1, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, -1),
(Position)currentPawn + Position(1, 0),
WallPosition(currentPawn.x() + 1, currentPawn.y(), WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, -1),
(Position)currentPawn + Position(1, 0),
WallPosition(currentPawn.x() + 1, currentPawn.y() - 1, WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, 1),
(Position)currentPawn + Position(1, 0),
WallPosition(currentPawn.x() + 1, currentPawn.y(), WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, 1),
(Position)currentPawn + Position(1, 0),
WallPosition(currentPawn.x() + 1, currentPawn.y() - 1, WallPosition::Direction::vertical)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, 1),
(Position)currentPawn + Position(0, -1),
WallPosition(currentPawn.x(), currentPawn.y() - 2, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
try {
tests.emplace_back(
Position(-1, 1),
(Position)currentPawn + Position(0, -1),
WallPosition(currentPawn.x() - 1, currentPawn.y() - 2, WallPosition::Direction::horizontal)
);
}
catch (const std::out_of_range &) {} // one element is out of board, ignore the test
Position distance;
Position opponent;
WallPosition wall;
for (const auto & t : tests)
{
std::tie(distance, opponent, wall) = t;
if (d == distance)
{
// Opponent at the right place + 1 Wall behind opponent + NO wall between opponent and destination
if (board_->existsPawn(opponent)
&& board_->existsWall(wall)
&& noBlockerWalls(opponent, pawn))
{
return true;
}
}
}
}
}
else if (length == 2)
{
// Pawn jumps over another pawn
// Define where have to be the other pawn
auto pawnJumped = ((Position)pawn + (Position)currentPawn) / 2;
// Verify that pawnJumped exists on board
if (!board_->existsPawn(pawnJumped))
return false;
// Verify that pawn moves 2 squares ahead
if ((d == Position(2, 0)) || (d == Position(-2, 0))
|| (d == Position(0, 2)) || (d == Position(0, -2)))
{
// Verify that no wall can block the jump
if (noBlockerWalls(currentPawn, pawn))
return true;
}
}
return false;
}
bool Referee::ValidMove(const Move &move)const
{
switch (move.type())
{
case Move::Type::pawn:
return ValidPawn(move.pawn());
case Move::Type::wall:
return ValidWall(move.wall());
case Move::Type::illegal_pawn:
case Move::Type::illegal_wall:
case Move::Type::surrend:
case Move::Type::none:
return false;
}
throw std::out_of_range("Unknown move to verify");
}
const std::vector<WallPosition>& Referee::getValidWalls(const PlayerName& name)
{
auto& player = find(name);
if (validwalls_.empty() && player.haveWall())
{
WallPosition wall;
// For each theorical walls, check validity
for (const auto& x : { 1,2,3,4,5,6,7,8 })
{
wall.x(x);
for (const auto& y : { 1,2,3,4,5,6,7,8 })
{
wall.y(y);
for (const auto& d : { WallPosition::Direction::horizontal,WallPosition::Direction::vertical })
{
wall.direction(d);
// Then add it to wall list
if (ValidWall(wall))
validwalls_.push_back(wall);
}
}
}
}
return validwalls_;
}
const std::vector<PawnPosition>& Referee::getValidPawns(const PlayerName & player)
{
if (validPawns_.empty())
{
PawnPosition pawn{ 1, 1, player };
// For each theorical pawns, check validity
for (const auto& x : { 1,2,3,4,5,6,7,8,9 })
{
pawn.x(x);
for (const auto& y : { 1,2,3,4,5,6,7,8,9 })
{
pawn.y(y);
// Then add it to pawn list
if (ValidPawn(pawn))
validPawns_.push_back(pawn);
}
}
}
return validPawns_;
}
std::vector<WallPosition> Referee::findBlockerWalls(const BoardPosition ¤t, const BoardPosition &next) const
{
std::vector<WallPosition> list;
auto d = next.distance(current);
auto length = d.length();
if ((length == 1)
&& ((d == Position(1, 0)) || (d == Position(-1, 0))
|| (d == Position(0, 1)) || (d == Position(0, -1))))
{
// Pawn moves to next case
// Find each possible walls between the 2 pawns
for (const auto primary : { true, false })
{
try
{
auto wall = WallPosition(next, current, primary);
list.emplace_back(wall);
}
catch (const std::out_of_range &) {} // Wall is out of board : ignore it
}
}
else if (length > 1)
{
// middle pawn position
auto middle = PawnPosition(((Position)current + (Position)next) / 2, "");
for (const auto& wall : findBlockerWalls(current, middle))
list.emplace_back(wall);
for (const auto& wall : findBlockerWalls(middle, next))
list.emplace_back(wall);
}
else
{
std::ostringstream oss;
oss << "Move between " << current << " and " << next << " must be vertical/horizontal line.";
throw std::out_of_range(oss.str());
}
return list;
}
bool Referee::noBlockerWalls(const BoardPosition &p1, const BoardPosition &p2) const
{
for (const auto& wall : findBlockerWalls(p1, p2))
{
if (board_->existsWall(wall))
return false;
}
return true;
}
Player& Referee::find(const PlayerName& name) const
{
// Find player by name in vector
auto player = std::find_if(players_->begin(), players_->end(),
[name](const Player *p)->bool { return (p->name() == name); });
if (player == players_->end())
throw std::out_of_range("Player " + name + " unknown");
return *(*player); // *(*iter)
}