-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmove.cpp
More file actions
341 lines (284 loc) · 11.1 KB
/
Copy pathmove.cpp
File metadata and controls
341 lines (284 loc) · 11.1 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
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
// moves.cpp: Main game loop and move legality verifier.
//
// Created by Eitan Frachtenberg on 11/18/15.
//
#include <cassert>
#include <iostream>
#include "move.h"
#include "board.h"
#include "player.h"
#include "move_notifier.h"
using namespace std;
namespace grandeur {
///////////////////////////////////////////////////////////////////
bool GameMove::operator==(const GameMove& rhs) const
{
if (type_ != rhs.type_) {
return false;
}
if (type_ == MoveType::TAKE_GEMS) {
return payload_.gems_ == rhs.payload_.gems_;
} else {
return payload_.card_ == rhs.payload_.card_;
}
}
///////////////////////////////////////////////////////////////////
ostream& operator<<(ostream& os, const GameMove& mv)
{
static constexpr const char* dname[] = { "LOW", "MEDIUM", "HIGH" };
switch (mv.type_) {
case TAKE_GEMS:
os << "Take: " << mv.payload_.gems_;
break;
case BUY_CARD:
os << "Buy: " << mv.payload_.card_.id_;
break;
case RESERVE_CARD:
os << "Reserve: ";
if (mv.payload_.card_.isWild()) {
os << "Wildcard from deck " << dname[mv.payload_.card_.id_.type_];
} else {
os << mv.payload_.card_.id_;
}
break;
}
return os;
}
using Counts = array<gem_count_t, 5>;
///////////////////////////////////////////////////////////////////
// Test whether a combination of gems we're trying to take are too many gems for
// the given table gems
bool inline
isLegalTake(const Counts& take, const Counts& table, const Counts& mine)
{
for (unsigned i = 0; i < NCOLOR - 1; ++i) {
if ((take[i] > table[i])
|| (mine[i] + take[i] < 0)
|| (take[i] == SAME_COLOR_GEMS && table[i] < MIN_SAME_COLOR_TABLE_GEMS)) {
return false;
}
}
return true;
}
///////////////////////////////////////////////////////////////////
// For a given vector of gem-count combinations, search all permutation of this
// combination to find legal gem-taking permutations, and add them to moves.
// A gem-count combination is an ascending-sorted array of five counts, representing
// How many gems of each colors to take (where the actual colors vary by permutation).
static void
addTakeGemCombination(Moves& moves, player_id_t pid, const Board& board,
Counts counts, const Counts& table, const Counts& mine)
{
do {
if (isLegalTake(counts, table, mine)) {
moves.push_back(GameMove(Gems(begin(counts), end(counts))));
}
} while (next_permutation(begin(counts), end(counts)));
}
///////////////////////////////////////////////////////////////////
// Enumerate all possible combinations of taking gems that match the amount
// of gems player already has.
static void
addTakeGemMoves(Moves& moves, player_id_t pid, const Board& board)
{
static const Counts sameColor = { 0, 0, 0, 0, 2 };
static const Counts diffColors = { 0, 0, 1, 1, 1 };
static const Counts netAddZero1 = { -2, 0, 0, 0, 2 };
static const Counts netAddZero2 = { -1, -1, 0, 1, 1 };
static const Counts netAddOne1 = { -1, 0, 0, 0, 2 };
static const Counts netAddOne2 = { -1, -1, 1, 1, 1 };
static const Counts netAddTwo1 = { -1, 0, 1, 1, 1 };
static const Counts netAddTwo2 = { 0, 0, 0, 1, 1 };
const Counts table({ board.tableGems().getCount(gem_color_t(0)),
board.tableGems().getCount(gem_color_t(1)),
board.tableGems().getCount(gem_color_t(2)),
board.tableGems().getCount(gem_color_t(3)),
board.tableGems().getCount(gem_color_t(4))
});
const Counts mine({ board.playerGems(pid).getCount(gem_color_t(0)),
board.playerGems(pid).getCount(gem_color_t(1)),
board.playerGems(pid).getCount(gem_color_t(2)),
board.playerGems(pid).getCount(gem_color_t(3)),
board.playerGems(pid).getCount(gem_color_t(4))
});
const auto ngems = board.playerGems(pid).totalGems();
// Enumerate all legal moves that take gems of a single color:
if (ngems <= MAX_PLAYER_GEMS - SAME_COLOR_GEMS) {
addTakeGemCombination(moves, pid, board, sameColor, table, mine);
}
// Enumerate all legal moves that take gems of different colors (no returns):
if (ngems <= MAX_PLAYER_GEMS - DIFFERENT_COLOR_GEMS) {
addTakeGemCombination(moves, pid, board, diffColors, table, mine);
}
// Enumerate all legal moves that take and return gems (net zero change):
else if (ngems == MAX_PLAYER_GEMS) {
addTakeGemCombination(moves, pid, board, netAddZero1, table, mine);
addTakeGemCombination(moves, pid, board, netAddZero2, table, mine);
}
// Enumerate all legal moves that take and return gems (net one gem taken):
else if (ngems == MAX_PLAYER_GEMS - 1) {
addTakeGemCombination(moves, pid, board, netAddOne1, table, mine);
addTakeGemCombination(moves, pid, board, netAddOne2, table, mine);
}
// Enumerate all legal moves that take and return gems (net two gems taken):
else if (ngems == MAX_PLAYER_GEMS - 2) {
addTakeGemCombination(moves, pid, board, netAddTwo1, table, mine);
addTakeGemCombination(moves, pid, board, netAddTwo2, table, mine);
}
}
///////////////////////////////////////////////////////////////////
// Enumerate all the cards (table/reserves) we can afford to buy:
static void
addBuyCardMoves(Moves& moves, player_id_t pid, const Board& board)
{
const auto& gems = board.playerGems(pid);
const auto& prestige = board.playerPrestige(pid);
auto allCards = board.tableCards();
for (const auto c : board.playerReserves(pid)) {
if (!c.isWild()) {
allCards.push_back(c);
}
}
for (const auto& card : allCards) {
const auto balance = gems.actualCost(card.cost_ - prestige);
if (!((gems - balance).hasNegatives())) {
assert(!card.isWild());
assert(!card.isNull());
moves.push_back(GameMove(card, MoveType::BUY_CARD));
}
}
}
///////////////////////////////////////////////////////////////////
// Enumerate all the cards that can be reserved:
static void
addReserveCardMoves(Moves& moves, player_id_t pid, const Board& board)
{
if (board.playerReserves(pid).size() >= MAX_PLAYER_RESERVES
|| board.playerGems(pid).totalGems() >= MAX_PLAYER_GEMS
|| board.tableGems().getCount(YELLOW) <= 0) {
return;
}
// Reserves from table cards:
for (const auto& card : board.tableCards()) {
moves.push_back(GameMove(card, MoveType::RESERVE_CARD));
}
// Reserves from undealt cards:
if (board.remainingCards(LOW) > 0) {
moves.push_back(GameMove(LOW_CARD, MoveType::RESERVE_CARD));
}
if (board.remainingCards(MEDIUM) > 0) {
moves.push_back(GameMove(MEDIUM_CARD, MoveType::RESERVE_CARD));
}
if (board.remainingCards(HIGH) > 0) {
moves.push_back(GameMove(HIGH_CARD, MoveType::RESERVE_CARD));
}
}
///////////////////////////////////////////////////////////////////
// Accumulate legal moves of all four types:
Moves
legalMoves(const Board& board, player_id_t pid)
{
Moves ret;
addTakeGemMoves(ret, pid, board);
addBuyCardMoves(ret, pid, board);
addReserveCardMoves(ret, pid, board);
return ret;
}
///////////////////////////////////////////////////////////////////
// Evaluate a move on a throwaway board to check its legality:
MoveStatus
isLegalMove(Board board, player_id_t pid, const GameMove& move)
{
return makeMove(board, pid, move);
}
///////////////////////////////////////////////////////////////////
// Execute a given move on a given board
MoveStatus
makeMove(Board& board, player_id_t pid, const GameMove& mymove, const Card& replacement)
{
MoveStatus status = LEGAL_MOVE;
switch (mymove.type_) {
case MoveType::TAKE_GEMS:
status = board.takeGems(pid, mymove.payload_.gems_);
break;
case MoveType::BUY_CARD:
assert(!mymove.payload_.card_.isWild());
assert(!mymove.payload_.card_.isNull());
status = board.buyCard(pid, mymove.payload_.card_.id_, replacement);
break;
case MoveType::RESERVE_CARD:
assert(!mymove.payload_.card_.isNull());
status = board.reserveCard(pid, mymove.payload_.card_, replacement);
break;
}
return status;
}
///////////////////////////////////////////////////////////////////
// Chose a move for a given player, find replacement card if necessary,
// and execute the move.
static MoveStatus
playerMove(Board& board, player_id_t pid, Cards& deck,
const Player* player, const Moves& legal)
{
if (legal.empty()) {
return LEGAL_MOVE;
}
auto pMove = player->getMove(board, legal);
// Find replacement card if buying/reserving from table:
Card replacement = NULL_CARD;
Card payloadCard = pMove.payload_.card_;
switch (pMove.type_) {
case TAKE_GEMS: break; // No need to replace any cards
case RESERVE_CARD:
if (payloadCard.isWild()) {
payloadCard = popFromDeck(payloadCard.id_.type_, deck);
assert(!payloadCard.isNull());
}
// Fall through to next case:
case BUY_CARD:
if (cardIn(pMove.payload_.card_.id_, board.tableCards())) {
replacement = popFromDeck(payloadCard.id_.type_, deck);
MoveNotifier::instance().notifyObservers(MoveEvent::REPLACEMENT_CARD, board, pid, replacement);
}
break;
}
const GameMove newMove = (pMove.type_ == TAKE_GEMS)?
pMove :
GameMove(payloadCard, pMove.type_);
const auto nobles = board.tableNobles();
MoveStatus status = makeMove(board, pid, newMove, replacement);
assert(status == LEGAL_MOVE);
MoveNotifier::instance().notifyObservers(
MoveEvent::MOVE_TAKEN, board, pid, { pMove });
if (board.tableNobles() != nobles) {
for (auto n : nobles) {
if (board.tableNobles().cend() ==
find(board.tableNobles().cbegin(), board.tableNobles().cend(), n)) {
MoveNotifier::instance().notifyObservers(MoveEvent::NOBLE_WON, board, pid, n);
}
}
}
return status;
}
///////////////////////////////////////////////////////////////////
player_id_t
mainGameLoop(Board& board, Cards& deck, Players& players)
{
MoveNotifier::instance().notifyObservers(MoveEvent::GAME_BEGAN, board, 0);
while (!board.gameOver()) {
board.newRound();
for (player_id_t pid = 0; pid < board.playersNum(); ++pid) {
const auto legal = legalMoves(board, pid);
playerMove(board, pid, deck, players[pid], legal);
}
}
// End of game: find winner:
const auto winner = board.leadingPlayer();
if (winner < board.playersNum()) {
MoveNotifier::instance().notifyObservers(MoveEvent::GAME_WON, board, winner);
} else {
MoveNotifier::instance().notifyObservers(MoveEvent::TIE, board, winner);
}
return winner;
}
} // namespace