-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAI.cc
316 lines (250 loc) · 9.84 KB
/
AI.cc
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
//
// AI.cc
// chess
//
// Created by User on 2015-11-23.
// Copyright (c) 2015 Jonah Librach. All rights reserved.
//
#include "AI.h"
#define BOLDRED "\033[1m\033[31m"
bool AI::isDiagCheck(Board* board, Square* toMove, Piece* king) const{
int r = king -> getSquare() -> getRow();
int c = king -> getSquare() -> getColumn();
for(int i = 0; i < 4; i++){
bool pieceSeen = false;
for(int k = 0; k < 8 && !pieceSeen; k++){
Square* potCheck = board -> getSquare(Chess::DIAGONAL[i][0] * k + r, Chess::DIAGONAL[i][0] * k + c);
if(potCheck){
pieceSeen = potCheck -> piece();
if(toMove == potCheck)
return true;
}
}
}
return false;
}
bool AI::isVertCheck(Board* board, Square* toMove, Piece* king) const{
int r = king -> getSquare() -> getRow();
int c = king -> getSquare() -> getColumn();
for(int i = 0; i < 4; i++){
bool pieceSeen = false;
for(int k = 0; k < 8 && !pieceSeen; k++){
Square* potCheck = board -> getSquare(Chess::ADJACENT[i][0] * k + r, Chess::ADJACENT[i][0] * k + c);
if(potCheck){
pieceSeen = potCheck -> piece();
if(toMove == potCheck)
return true;
}
}
}
return false;
}
bool AI::isKnightCheck(Board* b, Square* toMove, Piece* king) const{
int r = king -> getSquare() -> getRow();
int c = king -> getSquare() -> getColumn();
for(int i = 0; i < 8; i++){
Square* potCheck = b -> getSquare(Chess::KNIGHT[i][0] + r, Chess::KNIGHT[i][1] + c);
if(potCheck){
if(toMove == potCheck)
return true;
}
}
return false;
}
Move* AI::canMovePiece(string wanted, Square* to) const{
set<Piece*>::iterator it;
for(it = pieces -> begin(); it != pieces -> end(); ++it){
if(wanted.find((*it) -> getPieceID()) != string::npos){
if((*it) -> hasMove(to)){
Move* m = new Move;
m -> first = (*it) -> getSquare();
m -> second = to;
return m;
}
}
}
return NULL;
}
bool AI::lessThan(Move* lhs, Move* rhs, Board* board) const{
//we want to avoid our best pieces being caputred
//also, we don't want to 'avoid a capture' by moving to
//a square where we can get captured.
//when deciding between general moves, chose towards the centre
//look to avoid captures of higher valued pieces
//look to capture higher valued pieces
//if the piece gives check, that's a plus (not in general but for this assignment)
/*
Ranking:
1. Avoid capture of best piece.
2. Capture best piece.
3. Give check.
4. Move towards the middle.
*/
//comparison 1: check if they are about to be caputred
Piece* left = lhs -> first -> piece();
Piece* leftAttack = lhs -> second -> piece();
bool leftRecapturable = board -> isControlledByEnemiesOf(left, lhs -> second);
Piece* right = rhs -> first -> piece();
Piece* rightAttack = rhs -> second -> piece();
bool rightRecaptureable = board -> isControlledByEnemiesOf(right, rhs -> second);
double pll = (left -> getMaterialValue()) * leftRecapturable; //potential loss left
double gainLeft = leftAttack ? leftAttack -> getMaterialValue() : 0;
double plr = (right -> getMaterialValue()) * rightRecaptureable; //^ ^ right
double gainRight = rightAttack ? rightAttack -> getMaterialValue() : 0;
double netGainLeft = gainLeft - pll;
double netGainRight = gainRight - plr;
if(netGainLeft != netGainRight){
std::cout << "FOR MOVE BELOW MATERIAL LOSS: (" << netGainLeft << " vs " << netGainRight << ")\n";
return netGainLeft < netGainRight;
}
//assign giving check a value of 2
Piece* enemyKing = board -> findPiece(getPlayerID() ? 'K' : 'k');
if(left -> getPieceID() == 'N' || left -> getPieceID() == 'n'){
netGainLeft += 2 * isKnightCheck(board, lhs -> second, enemyKing);
}else if(left -> getPieceID() == 'B' || left -> getPieceID() == 'b'){
netGainLeft += 2 * isDiagCheck(board, lhs -> second, enemyKing);
}else if(left -> getPieceID() == 'R' || left -> getPieceID() == 'r'){
netGainLeft += 2 * isVertCheck(board, lhs -> second, enemyKing);
}else if(left -> getPieceID() == 'Q' || left -> getPieceID() == 'q'){
netGainLeft += 2 * isVertCheck(board, lhs -> second, enemyKing);
netGainLeft += 2 * isDiagCheck(board, lhs -> second, enemyKing);
}
if(right -> getPieceID() == 'N' || right -> getPieceID() == 'n'){
netGainRight += 2 * isKnightCheck(board, rhs -> second, enemyKing);
}else if(right -> getPieceID() == 'B' || right -> getPieceID() == 'b'){
netGainRight += 2 * isDiagCheck(board, rhs -> second, enemyKing);
}else if(right -> getPieceID() == 'R' || right -> getPieceID() == 'r'){
netGainRight += 2 * isVertCheck(board, rhs -> second, enemyKing);
}else if(right -> getPieceID() == 'Q' || right -> getPieceID() == 'q'){
netGainRight += 2 * isVertCheck(board, rhs -> second, enemyKing);
netGainRight += 2 * isDiagCheck(board, rhs -> second, enemyKing);
}
if(netGainLeft != netGainRight){
std::cout << "FOR MOVE BELOW MATERIAL GAIN: (" << netGainLeft << " vs " << netGainRight << ")\n";
return netGainLeft < netGainRight;
}
//lastly, check how centered the move is
Square* ls = lhs -> second;
Square* rs = rhs -> second;
int locLHS = Chess::LOCATION_VALUE[ls -> getRow()][ls -> getColumn()];
int locRHS = Chess::LOCATION_VALUE[rs -> getRow()][rs -> getColumn()];
std::cout << "LOCATION DETERMINES: (" << locLHS << " vs " << locRHS << ")\n";
return locLHS < locRHS;
}
AI::AI(int level, int playerNumber) : Player(playerNumber), level(level){
}
AI::~AI(){}
Move* AI::levelOne(set<Move*>* moves) const{
srand(NULL);
int random = rand() % (moves -> size());
set<Move*>::iterator it;
int i = 0;
Move* toReturn = NULL;
for(it = moves -> begin(); !toReturn && it != moves -> end(); ++it){
if(i == random){
return *it;
}
i++;
}
return toReturn;
}
Move* AI::levelTwo(Board* board, set<Move*>* moves) const{
//find checks
Piece* enemyKing = board -> findPiece(getPlayerID() ? 'K' : 'k');
int r = enemyKing -> getSquare() -> getRow();
int c = enemyKing -> getSquare() -> getColumn();
for(int i = 0; i < 8; i++){
Square* to =board -> getSquare(Chess::KNIGHT[i][0] + r, Chess::KNIGHT[i][1] + c);
if(to){
Move* m = canMovePiece("nN", to);
if(m)
return m;
}
}
for(int i = 0; i < 4; i++){
bool pieceSeen = false;
for(int k = 0; k < 8 && !pieceSeen; k++){
Square* to = board -> getSquare(Chess::ADJACENT[i][0] * k + r, Chess::ADJACENT[i][0] * k + c);
if(to){
if(to ->piece())
pieceSeen = true;
Move* m = canMovePiece("rRqQ", to);
if(m)
return m;
}
}
}
for(int i = 0; i < 4; i++){
bool pieceSeen = false;
for(int k = 0; k < 8 && !pieceSeen; k++){
Square* to = board -> getSquare(Chess::DIAGONAL[i][0] * k + r, Chess::DIAGONAL[i][0] * k + c);
if(to){
if(to ->piece())
pieceSeen = true;
Move* m = canMovePiece("bBqQ", to);
if(m)
return m;
}
}
}
//then find captures
set<Move*>::iterator it;
for(it = moves -> begin(); it != moves -> end(); ++it){
if((*it) -> second -> piece())
return *it;
}
//if no captures, move randomly
return levelOne(moves);
}
Move* AI::levelThree(Board* board, set<Move*>* moves) const{
//first avoids captures, then calls level 2 for the other functionalities
//its not looking ahead into the future
//does not do all three (as in the level above)
//LEVEL 2:
//then finds checks
//then captures
set<Move*>::iterator it;
for(it = moves -> begin(); it != moves -> end(); ++it){
Square* curSquare = (*it) -> first;
Piece* toMove = curSquare -> piece();
if(board -> isControlledByEnemiesOf(toMove, curSquare))
return *it;
}
return levelTwo(board, moves);
}
Move* AI::levelFour(Board* board, set<Move*>* moves) const{
Move* bestMove = NULL;
set<Move*>::iterator it;
for(it = moves -> begin(); it != moves -> end(); ++it){
if(!bestMove)
bestMove = *it;
else{
if(lessThan(bestMove, *it, board)){
std::cout << "\t" << BOLDBLUE << *(*it) -> first << " -> " << *(*it) -> second << " > ";
std::cout << *bestMove -> first << " -> " << *bestMove -> second << RESET << endl;
bestMove = *it;
}else{
std::cout << "\t" << BOLDRED << *(*it) -> first << " -> " << *(*it) -> second << " <= ";
std::cout << *bestMove -> first << " -> " << *bestMove -> second << RESET << endl;
}
}
}
return bestMove;
}
Move* AI::makeMove(Board* b, set<Move*>* moves){
std::string goAhead("");
while(cin >> goAhead && goAhead != "move");
if(moves -> size() == 0)
return NULL;
if(level == 1){
return levelOne(moves);
}else if(level == 2){
return levelTwo(b, moves);
}else if(level == 3){
return levelThree(b, moves);
}else if(level == 4){
return levelFour(b, moves);
}
//etc..
return NULL;
}