-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine.cpp
98 lines (89 loc) · 2.85 KB
/
engine.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
#include "engine.h"
void do_random_game() {
GameState state;
std::vector<DeploymentMove> deploymentMoves;
std::vector<Move> moves;
DeploymentMove deploymentMove;
Move move;
state.newGame();
while(!state.hasWinner()) {
if(state.state == deployment) {
deploymentMoves = state.getDeploymentMoves();
state.doDeploymentMove(deploymentMoves.at(rand() % deploymentMoves.size()));
} else {
moves = state.getMoves();
state.doMove(moves.at(rand() % moves.size()));
}
}
print_game_state(&state);
}
void print_game_state(GameState* state) {
bool bold = false;
for(int y = 9; y >= 0; y--) {
cout << "\033[107m";
for(int x = 0; x < 10; x++) {
bold = ((state->lastMovesStack.size() > 0) && ((x == state->lastMovesStack.back().x && y == state->lastMovesStack.back().y) || (x == state->lastMovesStack.back().dx && y == state->lastMovesStack.back().dy)));
if(bold) {
if(state->getOtherPlayerTurn() == blue) {
cout << "\u001b[47m";
} else {
cout << "\u001b[47m";
}
}
if(isLake(x, y)) {
cout << "\033[104m \033[107m";
} else if(state->isEmpty(x, y)) {
cout << " ";
} else if(state->getOwner(x, y) == blue) {
cout << "\033[34m" << getPieceChar(state->getPieceAt(x, y)) << " \033[107m";
} else {
cout << "\033[31m" << getPieceChar(state->getPieceAt(x, y)) << " \033[107m";
}
if(bold) {
cout << "\033[107m";
}
}
cout << "\033[0m\n";
}
cout << "\033[0m\n";
}
void Game::setAgent(Player player, Agent* agent) {
if(player == red) {
redAgent = agent;
} else {
blueAgent = agent;
}
}
GameState Game::getGameState() {
return state;
}
Player Game::play(int turn_limit, bool verbose) {
Move move;
state = GameState();
state.newGame();
redAgent->setup();
blueAgent->setup();
while(!state.hasWinner() && state.turn < turn_limit) {
if(state.state == deployment) {
if(state.player_turn == red) {
redAgent->do_deployment(&state);
} else {
blueAgent->do_deployment(&state);
}
} else {
if(state.player_turn == red) {
redAgent->get_move(&state, &move);
} else {
blueAgent->get_move(&state, &move);
}
state.doMove(move);
}
if(verbose) {
print_game_state(&state);
}
}
if(verbose) {
cout << "Player " << (state.getWinner() == red ? "Red" : "Blue") << " has won on turn " << state.turn << "!\n";
}
return state.getWinner();
}