-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
361 lines (317 loc) · 10.7 KB
/
board.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
#include "stdafx.h"
#include "Board.h"
#include <algorithm>
#include <iostream>
using namespace std;
Board::Board() :positionLastPlacedP1(-1), positionLastPlacedP2(-1) {
for (int i = 0; i < 24; i++) { //make 24 positions on the board
gamePositions[i] = Node(i);
}
}
Board::Board(map<int, Node> board, int lastPosP1, int lastPosP2) {
gamePositions = board;
positionLastPlacedP1 = lastPosP1;
positionLastPlacedP2 = lastPosP2;
}
int Board::getBoard(int position) {
return gamePositions[position].getPlayerOccupy();
}
void Board::setBoard(int location, int player) {
gamePositions[location].setPlayerOccupy(player);
}
bool Board::adjacentPositions(int token, int position) {
//given the following token, the valid positions are:
for (int i = 0; i < gamePositions[token].getAdjacentPositions().size(); i++) {
if (gamePositions[token].getAdjacentPositions().at(i) == position) {
return true;
}
}
return false;
}
void Board::displayBoard() {
for (int i = 0; i < 24; i++) {
//draws the top/bottom row of the board
if (i < 2) {
cout << gamePositions[i].getPlayerOccupy() << "---------";
}
//draw the middle top row of the board
else if (i == 2) {
cout << gamePositions[i].getPlayerOccupy() << endl;
cout << "| | |" << endl;
}
else if (i == 3) {
cout << "| " << gamePositions[i].getPlayerOccupy() << "-----";
}
else if (i == 4 || i == 19) {
cout << gamePositions[i].getPlayerOccupy() << "-----";
}
else if (i == 5) {
cout << gamePositions[i].getPlayerOccupy() << " |" << endl;
cout << "| | | | |" << endl;
}
else if (i == 6 || i == 15) {
cout << "| | " << gamePositions[i].getPlayerOccupy();
}
else if (i == 7 || i == 16) {
cout << "--" << gamePositions[i].getPlayerOccupy() << "--";
}
else if (i == 8 || i == 17) {
cout << gamePositions[i].getPlayerOccupy() << " | |" << endl;
}
else if (i == 9) {
cout << "| | | | | |" << endl;
cout << gamePositions[i].getPlayerOccupy() << "---";
}
else if (i == 10 || i == 12) {
cout << gamePositions[i].getPlayerOccupy() << "--";
}
else if (i == 11) {
cout << gamePositions[i].getPlayerOccupy() << " ";
}
else if (i == 13) {
cout << gamePositions[i].getPlayerOccupy() << "---";
}
else if (i == 14) {
cout << gamePositions[i].getPlayerOccupy() << endl;
cout << "| | | | | |" << endl;
}
else if (i == 18) {
cout << "| | | | |" << endl;
cout << "| " << gamePositions[i].getPlayerOccupy() << "-----";
}
else if (i == 20) {
cout << gamePositions[i].getPlayerOccupy() << " |" << endl;
cout << "| | |" << endl;
}
else if (i == 21 || i == 22) {
cout << gamePositions[i].getPlayerOccupy() << "---------";
}
else {
cout << gamePositions[i].getPlayerOccupy() << endl;
}
}
}
int Board::evaluateBoard(int Player, int phase) {
int counter = 0; //counts to see if all adjacent pieces to a token are blocked, also used for another evaluation to see if current player has more pieces
int counter2 = 0;
int score = 0;
int positionToCheck;
int otherPlayer;
if (Player == 1) {
otherPlayer = 2;
}
else {
otherPlayer = 1;
}
//check if amount of mills by player
vector<vector<int>> allMills; //mills that can be formed for that position
vector<int> millToCheck; //individual mill ( contains 2 other positions )
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == Player && gamePositions[millToCheck.at(1)].getPlayerOccupy() == Player) {
score += 20;
}
}
}
}
//check amount of mills by other player
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == otherPlayer) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == otherPlayer && gamePositions[millToCheck.at(1)].getPlayerOccupy() == otherPlayer) {
score -= 20;
}
}
}
}
//if we block a mill of the opponent
allMills.clear();
millToCheck.clear();
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == otherPlayer && gamePositions[millToCheck.at(1)].getPlayerOccupy() == otherPlayer) {
score += 8;
}
}
}
}
//if other player blocks our move
allMills.clear();
millToCheck.clear();
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == otherPlayer) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == Player && gamePositions[millToCheck.at(1)].getPlayerOccupy() == Player) {
score -= 6;
}
}
}
}
//if we almost create a mill (one spot is unoccupied)
allMills.clear();
millToCheck.clear();
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == Player || gamePositions[millToCheck.at(1)].getPlayerOccupy() == Player) {
score += 6;
}
}
}
}
//if opponent almost creates a mill
allMills.clear();
millToCheck.clear();
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == otherPlayer) {
allMills = gamePositions[i].getMills();
for (int j = 0; j < allMills.size(); j++) {
millToCheck = allMills.at(j);
if (gamePositions[millToCheck.at(0)].getPlayerOccupy() == otherPlayer || gamePositions[millToCheck.at(1)].getPlayerOccupy() == otherPlayer) {
score -= 5;
}
}
}
}
//check if move was blocked
/*if(Player == 2){
if(positionLastPlacedP2 != -1 && positionLastPlacedP1 != -1){
//for every adjacent position, if it is the opposing player, check all adjacent pieces of that position
//for that position, if all of its adjacent pieces blocks it from moving (current player), then we add to score
for(int i = 0; i < gamePositions[positionLastPlacedP2].getAdjacentPositions().size(); i++){
if(gamePositions[gamePositions[positionLastPlacedP2].getAdjacentPositions().at(i)].getPlayerOccupy() == 1){
//check the adjacent positions of that, to see if its blocked
int newPositionToCheck = gamePositions[positionLastPlacedP2].getAdjacentPositions().at(i);
for(int j = 0; j < gamePositions[newPositionToCheck].getAdjacentPositions().size(); j++){
if(gamePositions[newPositionToCheck].getAdjacentPositions().at(j) == 2){
counter++;
if(counter == gamePositions[newPositionToCheck].getAdjacentPositions().size()){
score += 5;
}
}
}
}
}
}
}
else{
if(positionLastPlacedP2 != -1 && positionLastPlacedP1 != -1){
for(int i = 0; i < gamePositions[positionLastPlacedP1].getAdjacentPositions().size(); i++){
if(gamePositions[gamePositions[positionLastPlacedP1].getAdjacentPositions().at(i)].getPlayerOccupy() == 2){
//check the adjacent positions of that, to see if its blocked
int newPositionToCheck = gamePositions[positionLastPlacedP1].getAdjacentPositions().at(i);
for(int j = 0; j < gamePositions[newPositionToCheck].getAdjacentPositions().size(); j++){
if(gamePositions[newPositionToCheck].getAdjacentPositions().at(j) == 1){
counter++;
if(counter == gamePositions[newPositionToCheck].getAdjacentPositions().size()){
score += 5;
}
}
}
}
}
}
}*/
//see if its a winning move
counter = 0;
counter2 = 0;
if (phase == 3) {
counter = 0;
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) { //if player's total token count is less than 3
counter++;
}
else {
counter2++;
}
}
if (counter2 < 3) { //opponent loses
score += 500;
}
else if (counter < 3) {
score -= 500;
}
}
return score;
}
//player 2 = AI(max player), player 1 = Human(min player)
vector<Board> Board::generateBoard(int Player, int phase) {
//temp vector used to store boards as we generate them
vector<Board> temp_vec;
//different possible boards for each, we assuming maximizing player is player 1
if (phase == 1) { //phase 1
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == 0) { //find the open spot and put the maximizingplayer's token on it
Board newBoard(gamePositions, positionLastPlacedP1, positionLastPlacedP2);
//used to create new object to put into vector
newBoard.setBoard(i, Player); //change the location where we found the empty spot and put the player's token
newBoard.setPositionLastPlaced(i, Player);
temp_vec.push_back(newBoard); //add this instance to the vector
}
}
return temp_vec;
}
else if (phase == 2) { //phase 2
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) { //if there is a token on the piece
for (int j = 0; j < gamePositions[i].getAdjacentPositions().size(); j++) { //get the adjacent positions
if (gamePositions[gamePositions[i].getAdjacentPositions().at(j)].getPlayerOccupy() == 0) { //find the board of the adjacent positions
Board newBoard(gamePositions, positionLastPlacedP1, positionLastPlacedP2);
newBoard.tokenLastPos = i; //so we can refer back to where token was originally placed
newBoard.setBoard(i, 0); //set the position of the desired token that we want to move, to 0
newBoard.setBoard(gamePositions[i].getAdjacentPositions().at(j), Player); //set the adjacent position to maximizing player
newBoard.setPositionLastPlaced(gamePositions[i].getAdjacentPositions().at(j), Player);
temp_vec.push_back(newBoard);
}
}
}
}
return temp_vec;
}
else { //phase 3
for (int i = 0; i < 24; i++) {
if (gamePositions[i].getPlayerOccupy() == Player) {
for (int j = 0; j < 24; j++) { //for each spot that is empty, you can generate a board for it
if (gamePositions[j].getPlayerOccupy() == 0) {
Board newBoard(gamePositions, positionLastPlacedP1, positionLastPlacedP2);
newBoard.tokenLastPos = i;
newBoard.setBoard(i, 0);
newBoard.setBoard(j, Player);
newBoard.setPositionLastPlaced(j, Player);
temp_vec.push_back(newBoard);
}
}
}
}
return temp_vec;
}
//if removing phase, generate possible removes
}
int Board::getPosLastPlaced(int player) {
if (player == 1) {
return positionLastPlacedP1;
}
else {
return positionLastPlacedP2;
}
}
void Board::setPositionLastPlaced(int position, int player) {
if (player == 1) {
positionLastPlacedP1 = position;
}
else {
positionLastPlacedP2 = position;
}
}