-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgame.cpp
212 lines (159 loc) · 5.19 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
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdlib>
#include "game.h"
#include "hexGraph.h"
#include "hexBoard.h"
using namespace std;
ostream& operator<<(ostream &out, Space sp) {
switch (sp) {
case P_EMPTY:
out << "-";
break;
case P_BLACK:
out << "B";
break;
case P_WHITE:
out << "W";
break;
}
return out;
}
void Game::gameLoop() {
Space winner;
bool playerWentFirst = Player::goesFirst(); // Get player input
if (!playerWentFirst) { // Make a predetermined first move
unsigned int rank = min(2u, board->sideLength - 1); // Account for teeny tiny boards
board->setSpace(rank, rank, P_BLACK); // I read somewhere that 2,2 is a balanced opening move (remember pie rule).
}
hexGraph gameGraph(board);
int comMoveIndex = -1;
while(true) {
turn++;
drawBoard();
if (comMoveIndex != -1) { // Remind the player of where the CPU went
pair<int, int> comMove = board->getCoords(comMoveIndex);
cout << "Black just moved (" << comMove.first << ", " << comMove.second << ")." << endl;
}
movePlayer(turn == 1 && !playerWentFirst); // Pass whether the pi rule is in effect
drawBoard();
if ((winner = gameGraph.checkWinner(board))) break;
cout << "Calculating optimal CPU move..." << endl;
comMoveIndex = gameGraph.getAIMove(*board, ai_monte_carlo_iterations, ai_plies, P_BLACK); // Calculate move
board->setSpace(comMoveIndex, P_BLACK);
if ((winner = gameGraph.checkWinner(board))) break;
}
drawBoard();
if (winner == P_WHITE) cout << "Player Wins!" << endl;
if (winner == P_BLACK) cout << "CPU Player Wins!" << endl;
}
void Game::movePlayer(bool piRule){
pair<int, int> move;
if (piRule) cout << "(To use the pie rule, move where the CPU player just did.)" << endl;
int badmove = false;
do {
if (badmove) {
drawBoard();
cout << "Invalid Move." << endl;
}
move = Player::getPlayerMove();
badmove = true;
} while (!board->isValidMove(move.first, move.second, piRule));
// Record move in game board
board->setSpace(move.first, move.second, P_WHITE);
}
void Game::drawBoard() {
// The drawing routine turned into spaghetti.
// I promise the rest of the program is a lot more readable.
// First print out a nice turn banner
string hz = "-";
int boardHeight = board->sideLength;
int boardWidth = boardHeight;
int indent = 3;
int bannersize = (2 * indent) + 5 + boardWidth * 3 + log10(boardWidth);
string vt = "";
for (int i = 0; i < indent - 1; i++) vt = vt + " "; // This is really sloppy, I know.
cout << endl;
int turndigits = log10(turn) + 1;
int bannerpadding = (bannersize - (11 + turndigits)) / 2;
for (int i = 0; i < bannerpadding; i++) cout << hz;
cout << " HEX Turn " << turn << " ";
for (int i = 0; i < bannerpadding; i++) cout << hz;
if (turndigits % 2 == 0) cout << hz;
cout << endl << vt << endl;
// Now the heavy work of printing the board itself
const int totalpadding = log10(boardHeight);
// Print first row of column numbers
cout << vt;
for (int i = 0; i < totalpadding + 2; i++) {
cout << " ";
}
for (int j = 0; j < boardWidth; j++) {
if (j < 10) {
cout << " ";
}
else {
cout << j/10 << " ";
}
}
cout << endl;
// Print second row of column numbers
cout << vt;
for (int i = 0; i < totalpadding + 3; i++) {
cout << " ";
}
for (int j = 0; j < boardWidth; j++) {
cout << j%10 << " ";
}
cout << endl;
// Print the rows, one by one
int intlength;
for (int i = boardHeight - 1; i >= 0; i--) {
cout << vt;
// Pad the board on the left to make it a parallelogram
for (int j = 0; j < boardHeight - i - 1; j++) {
cout << " ";
}
// Pad the number on the left, so that the board doesn't shift around.
intlength = (i == 0) ? 0 : log10(i);
for (int j = 0; j < totalpadding - intlength; j++) {
cout << " ";
}
// Print the row nunber
cout << " " << i << " ";
// Print all of the spaces in the row
for(int j = 0; j < boardWidth; j++) {
cout << board->getSpace(j, i) << " ";
}
// Print the row number one more time on the right
cout << " " << i << endl;
}
// Print first row of column numbers
cout << vt;
for (int i = 0; i < boardWidth + totalpadding + 4; i++) {
cout << " ";
}
for (int j = 0; j < boardWidth; j++) {
if (j >= 10) cout << j/10 << " ";
else cout << j << " ";
}
cout << endl;
// Print second row of column numbers
cout << vt;
for (int i = 0; i < boardWidth + totalpadding + 5; i++) {
cout << " ";
}
for (int j = 0; j < boardWidth; j++) {
if (j < 10) {
cout << " ";
}
else {
cout << j%10 << " ";
}
}
cout << endl << endl;
for (int i = 0; i < bannersize; i++) cout << hz;
cout << endl << endl;
}