-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArena.cpp
More file actions
401 lines (340 loc) · 11.5 KB
/
Copy pathArena.cpp
File metadata and controls
401 lines (340 loc) · 11.5 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
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
#include "Arena.h"
#include <algorithm>
unordered_map <char, PieceColor> colorNotationReverse {
{'w', PieceColor::WHITE},
{'b', PieceColor::BLACK},
};
unordered_map <PieceColor, string> colorNotation {
{PieceColor::WHITE, "w"},
{PieceColor::BLACK, "b"},
};
unordered_map <char, vector<Direction>> dirNotationReverse{
{'\\', {Direction::SE,Direction::NW}},
{'-', {Direction::E,Direction::W}},
{'/', {Direction::NE, Direction::SW }}
};
unordered_map <Direction, string> dirNotation {
{Direction::NW ,"\\"},
{Direction::NE ,"/"},
{Direction::SW ,"/"},
{Direction::SE ,"\\"},
{Direction::W ,"-"},
{Direction::E ,"-"},
};
unordered_map<char, PieceName> nameNotationReverse{
{'G', PieceName::GRASSHOPPER},
{'Q', PieceName::QUEEN},
{'L', PieceName::LADYBUG},
{'P', PieceName::PILLBUG},
{'M', PieceName::MOSQUITO},
{'B', PieceName::BEETLE},
{'A', PieceName::ANT},
{'S', PieceName::SPIDER},
};
unordered_map<PieceName, string> nameNotation{
{PieceName::GRASSHOPPER, "G"},
{PieceName::QUEEN, "Q"},
{PieceName::LADYBUG, "L"},
{PieceName::PILLBUG, "P"},
{PieceName::MOSQUITO, "M"},
{PieceName::BEETLE, "B"},
{PieceName::ANT, "A"},
{PieceName::SPIDER, "S"},
};
set<PieceName> singlePieces = {MOSQUITO, QUEEN, LADYBUG, PILLBUG};
set<Direction> westernDirection = {NW, SW, W};
/*
*Use this to set a player to a cpu
*Initialized to human players by default
*/
void Arena::setPlayer(int playerNum, MonteCarloTree& searchAlgo) {
if (playerNum == 0) {
playerOneCPU = true;
CPU[0].initializeTo(searchAlgo);
} else if (playerNum == 1) {
playerTwoCPU = true;
CPU[1].initializeTo(searchAlgo);
} else {
cout << "Illegal player set in Arena::setPlayer()" << endl;
throw 3;
}
}
//assumes that Arena::currentGameState has not yet made the specified move
string Arena::convertToNotation(MoveInfo move){
if (move == MoveInfo()) return "pass";
//if not spawning
PieceColor oldColor;
PieceName oldName = move.pieceName;
if (move.oldPieceLocation.count() == 1)
oldColor = currentGameState.findTopPieceColor(move.oldPieceLocation);
else
oldColor = currentGameState.turnColor;
string notation = colorNotation[oldColor] + nameNotation[oldName];
//if not spawning
if (move.oldPieceLocation.count() == 1){
notation += findTopPieceOrder(move.oldPieceLocation);
} else {
if (singlePieces.find(oldName) == singlePieces.end())
notation += to_string(countPieces(oldColor, oldName) + 1);
}
Bitboard test;
// if landing on top of another piece, use that in the notation
if (currentGameState.allPieces.containsAny(move.newPieceLocation)){
test.initializeTo(move.newPieceLocation);
notation += " ";
notation += colorNotation[currentGameState.findTopPieceColor(test)];
notation += nameNotation[currentGameState.findTopPieceName(test)];
notation += findTopPieceOrder(test);
return notation;
}
Bitboard piecesExceptCurrent(currentGameState.allPieces);
piecesExceptCurrent.notIntersectionWith(move.oldPieceLocation);
//find a relative direction to connect to (if not the first piece)
for (auto direction: hexagonalDirections) {
test.initializeTo(move.newPieceLocation);
test.shiftDirection(direction);
if (piecesExceptCurrent.containsAny(test)) {
notation += " ";
Direction opposite = oppositeDirection[direction];
//use the direction that is opposite in notation
if (westernDirection.find(opposite) != westernDirection.end())
notation += dirNotation[opposite];
notation += colorNotation[currentGameState.findTopPieceColor(test)];
notation += nameNotation[currentGameState.findTopPieceName(test)];
notation += findTopPieceOrder(test);
if (westernDirection.find(opposite) == westernDirection.end())
notation += dirNotation[opposite];
return notation;
}
}
return notation;
}
//assumes that Arena::currentGameState has not yet made the specified move
MoveInfo Arena::convertFromNotation(string notation) {
string pieceIdentifier = "";
string newLocation = "";
MoveInfo move;
if (notation == "pass") return move;
string * ptr = &pieceIdentifier;
//split into strings delimited by space
for (unsigned i = 0 ; i < notation.size() ; i++) {
if (notation[i] == ' '){
ptr = &newLocation;
continue;
}
*ptr += notation[i];
}
//interpret the first half of the string
move.pieceName = nameNotationReverse[pieceIdentifier[1]];
PieceColor color = colorNotationReverse[pieceIdentifier[0]];
string pieceOrderString = "";
if (pieceIdentifier.size() > 2) {
pieceOrderString = pieceIdentifier[2];
if (pieceOrderString == "1" && singlePieces.find(move.pieceName) != singlePieces.end())
pieceOrderString = "";
}
//determine which piece the string corresponds to
Bitboard foundPieces(*currentGameState.getPieces(move.pieceName));
foundPieces.intersectionWith(*currentGameState.getPieces(color));
// since many pieces of a given name and color
// can be placed in the hive, determine which one it is
Bitboard foundPiece;
for (auto& piece: foundPieces.splitIntoBitboards()) {
auto description = pieceOrders[piece.hash()].back();
if (std::get<0>(description) == move.pieceName &&
std::get<1>(description) == color &&
std::get<2>(description) == pieceOrderString) {
foundPiece = piece;
}
}
move.oldPieceLocation = foundPiece;
//if the second half of the string exists
if (newLocation.size() == 0 ) {
move.newPieceLocation = startSpawnBoard;
} else {
set<char> symbols {'\\','/','-'};
bool isWesternDirection = symbols.find(newLocation[0]) != symbols.end();
//interpret the second half of the string
char newNameString = newLocation[1 + isWesternDirection];
PieceName newName = nameNotationReverse[newNameString];
char newColorString = newLocation[0 + isWesternDirection];
PieceColor newColor = colorNotationReverse[newColorString];
//determine possible pieces
foundPieces.initializeTo(*currentGameState.getPieces(newColor));
foundPieces.intersectionWith(*currentGameState.getPieces(newName));
//determine whether it is a directional move or a climb
string pieceOrderString = "";
bool containsDirection = false;
char dir;
for (char c: symbols) {
if (newLocation.find(c) != string::npos) {
containsDirection = true;
dir = c;
break;
}
}
if ((int)newLocation.size() > (2 + containsDirection)) {
pieceOrderString = newLocation[2 + isWesternDirection];
}
// since many pieces of a given name and color
// can be placed in the hive, determine which one it is
foundPiece.clear();
for (auto& piece: foundPieces.splitIntoBitboards()) {
auto description = pieceOrders[piece.hash()].back();
if (std::get<0>(description) == newName &&
std::get<1>(description) == newColor &&
std::get<2>(description) == pieceOrderString) {
foundPiece = piece;
}
}
//follow the instruction given in notation
if (containsDirection) {
Direction direction = dirNotationReverse[dir][isWesternDirection];
foundPiece.shiftDirection(direction);
}
move.newPieceLocation = foundPiece;
}
if (move.newPieceLocation == Bitboard() && move.oldPieceLocation == Bitboard()) {
cout << "Parse error for input notation" << endl;
cout << notation << endl;
throw 55;
}
return move;
}
//Assumes that specified move is legal
void Arena::makeMove(string move){
MoveInfo moveInfo = convertFromNotation(move);
makeMove(moveInfo);
};
//Assumes that move is legal
void Arena::makeMove(MoveInfo move){
moveHistoryNotation.push_back(convertToNotation(move));
//if no move made
if (move == MoveInfo()) {
moveHistory.push_back(move);
currentGameState.replayMove(move);
cout << "HERREE" << endl;
return;
}
Bitboard movingPiece = move.oldPieceLocation;
Bitboard newLocation = move.newPieceLocation;
string pieceOrderString = "";
PieceColor color;
PieceName name = move.pieceName;
//if spawning a piece
if (movingPiece.count() == 0) {
color = currentGameState.turnColor;
//find certain pieces that do not occur more than once
if (singlePieces.find(move.pieceName) == singlePieces.end()){
//find all identical pieces and add 1 to the count
int num = countPieces(color, name);
pieceOrderString = to_string(num + 1);
}
} else {
color = currentGameState.findTopPieceColor(movingPiece);
pieceOrderString = findTopPieceOrder(movingPiece);
}
//remove from old location, insert into new
if (movingPiece.count())
pieceOrders.at(movingPiece.hash()).pop_back();
if (newLocation.count() == 0){
cout << move.toString("") << endl;
throw 3;
}
pieceOrders[newLocation.hash()].push_back(make_tuple(name, color, pieceOrderString));
//update
moveHistory.push_back(move);
currentGameState.replayMove(move);
};
void Arena::undo(string move){
MoveInfo moveInfo = convertFromNotation(move);
undo(moveInfo);
}
void Arena::undo(MoveInfo move){
moveHistory.pop_back();
moveHistoryNotation.pop_back();
auto pieceOrder = pieceOrders.at(move.newPieceLocation.hash()).back();
if (!(move.oldPieceLocation == Bitboard()))
pieceOrders[move.oldPieceLocation.hash()].push_back(pieceOrder);
pieceOrders.at(move.newPieceLocation.hash()).pop_back();
currentGameState.undoMove(move);
}
//Assumes that the piece is already in the hive
string Arena::findTopPieceOrder(Bitboard piece){
return std::get<2>(pieceOrders.at(piece.hash()).back());
};
int Arena::countPieces(PieceColor color, PieceName name){
Bitboard pieces = *currentGameState.getPieces(color);
pieces.intersectionWith(*currentGameState.getPieces(name));
int amount = 0;
for (Bitboard& piece : pieces.splitIntoBitboards()) {
for (auto description: pieceOrders[piece.hash()]){
if (std::get<0>(description)== name && std::get<1>(description) == color) {
amount++;
}
}
}
return amount;
}
/*
* Allows both agents to take a turn. If it is a human agent,
* require correct input from the console. If it is a computer agent
* require correct input from the search algorithm
*
* returns true if the game is over
*/
bool Arena::battle(bool silent) {
bool vals[] = {playerOneCPU, playerTwoCPU};
for (int i = 0 ; i < 2 ; i++) {
if ((PieceColor)i != currentGameState.turnColor) continue;
bool isCPU = vals[i];
MoveInfo selectedMove;
vector<MoveInfo> moves = currentGameState.generateAllMoves();
if (!moves.size()) {
currentGameState.replayMove(selectedMove);
if (!silent) {
string a = (isCPU) ? "Computer" : "Human";
cout << "[" << a << " Player " << i << "] pass" << endl;
}
continue;
};
set<string> moveStrings;
for (auto move: moves) {
if (!silent)
cout << convertToNotation(move) <<endl;
moveStrings.insert(convertToNotation(move));
}
if (isCPU) {
selectedMove = CPU[i].multiSearch(currentGameState, numCores);
if (std::find(moves.begin(), moves.end(), selectedMove) == moves.end()) {
cout << "SearchAlgorithm " << i << " returned an illegal move " << endl;
throw "hello darkness my old friend";
}
} else {
string inputMove;
do {
cout << "[Human Player " << i << "] please enter a valid move: "<< endl;
getline(cin, inputMove);
cout << endl;
} while (moveStrings.find(inputMove) == moveStrings.end() );
selectedMove = convertFromNotation(inputMove);
}
if (!silent) {
string a = (isCPU) ? "Computer" : "Human";
cout << "[" << a << " Player " << i << "] " << convertToNotation(selectedMove) << endl;
}
makeMove(selectedMove);
if (currentGameState.checkDraw() || currentGameState.checkVictory() != PieceColor::NONE) {
if (!silent) {
cout << "Game Over" << endl;
if (currentGameState.checkDraw())
cout << "Draw" << endl;
else {
cout << "Player " << currentGameState.checkVictory() << " has won";
}
}
return true;
}
}
return false;
}