-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputer5.cc
More file actions
317 lines (279 loc) · 9.79 KB
/
computer5.cc
File metadata and controls
317 lines (279 loc) · 9.79 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
//
// computer5.cpp
// PawnPusher9000
//
// Created by Lavi on 2015-12-02.
// Copyright © 2015 Lavi. All rights reserved.
//
#include "computer5.h"
#include <ctype.h>
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <algorithm>
#include <queue>
//I am using Tomasz Michniewski's evaluation heuristic https://chessprogramming.wikispaces.com/Simplified+evaluation+function
long total_eval=0;
const float PawnSquare[64]={
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
};
const float KnightSquare[64]={
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50
};
const float BishopSquare[64]={
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20
};
const float RookSquare[64]={
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
};
const float QueenSquare[64]={
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20
};
const float KingSquareMiddleGame[64]={
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
};
const float KingSquareEndGame[64]={
-50,-40,-30,-20,-20,-30,-40,-50,
-30,-20,-10, 0, 0,-10,-20,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-30, 0, 0, 0, 0,-30,-30,
-50,-30,-30,-30,-30,-30,-30,-50
};
float ComputerAI_5::evaluatePosition(int ply , float alpha, float beta){
int _bestMove;
total_eval++;
vector <int> choice= cp.getAllLegalMoves();
if (choice.size()==0){
if (cp.isCheck()){
if (cp.colourToMove()==WHITE) return -10000;
else return 10000;
}
else return 0;
}
sort(choice.begin(),choice.end(), choicePriority(cp));
float v;
if (cp.colourToMove()==WHITE){
v=-11000;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
MoveFootprint &fp=cp.makeMove(choice[choiceInd]/64, choice[choiceInd]%64, 'Q');
float innerV;
if (ply<=1 && fp.pieceOnEndPos==' ' && (!cp.isCheck() || ply<=-3)){
innerV=evaluatePosition();
}
else innerV=evaluatePosition(ply-1, alpha, beta);
if (innerV>9000) innerV--;
if (innerV<-9000) innerV++;
if (innerV>v) {
_bestMove=choice[choiceInd];
}
v=std::max(v,innerV);
alpha=std::max(alpha, v);
cp.undoMove(fp);
delete &fp;
if (beta<=alpha) break;
}
}
else {
v=11000;
for (int choiceInd=0;choiceInd<choice.size();choiceInd++){
MoveFootprint &fp=cp.makeMove(choice[choiceInd]/64, choice[choiceInd]%64, 'q');
float innerV;
if (ply<=1 && (fp.pieceOnEndPos==' ') && (!cp.isCheck() || ply<=-3)){
innerV=evaluatePosition();
}
else innerV=evaluatePosition(ply-1, alpha, beta);
if (innerV>9000) innerV--;
if (innerV<-9000) innerV++;
if (innerV<v) {
_bestMove=choice[choiceInd];
}
v=std::min(v,innerV);
beta=std::min(beta, v);
cp.undoMove(fp);
delete &fp;
if (beta<=alpha) break;
}
}
bestMove=_bestMove;
return v;
}
float ComputerAI_5::pieceValue(char c){
switch (c){
case 'P':
return 1;
case 'N':
return 3.2;
case 'B':
return 3.3;
case 'R':
return 5;
case 'Q':
return 9;
case 'K':
return 10000;
}
return 0;
}
float ComputerAI_5::positionalBonus(char piece, int sqr, bool endGame){
if (piece==toupper(piece)) sqr=63-sqr; // square values are reversed for black (and symmetric horizontally)
switch (toupper(piece)) {
case 'P':
return PawnSquare[sqr]/100;
case 'N':
return KnightSquare[sqr]/100;
case 'B':
return BishopSquare[sqr]/100;
case 'R':
return RookSquare[sqr]/100;
case 'Q':
return QueenSquare[sqr]/100;
case 'K':
return (endGame?KingSquareEndGame[sqr]:KingSquareMiddleGame[sqr])/100;
}
return 0;
}
//heuristical evaluation, without looking ahead
float ComputerAI_5::evaluatePosition(){
total_eval++;
float eval;
float whiteMaterialEval=0;
float blackMaterialEval=0;
for (int i=1;i<6;i++){
whiteMaterialEval+=pieceValue(cp.pieceTypes[i])*cp.numPiecesOfType(cp.pieceTypes[i]);
}
for (int i=7;i<12;i++){
blackMaterialEval+=pieceValue(toupper(cp.pieceTypes[i]))*cp.numPiecesOfType(cp.pieceTypes[i]);
}
//when should you start moving the king towards the centre?
bool whiteEndGame=cp.numPiecesOfType('q')==0 && cp.numPiecesOfType('r')<=1;
bool blackEndGame=cp.numPiecesOfType('Q')==0 && cp.numPiecesOfType('R')<=1;
float whitePositionalEval=0;
float blackPositionalEval=0;
for (int i=0;i<12;i++){
for (int j=0;j<cp.numPiecesOfType(cp.pieceTypes[i]);j++){
if (i<6) whitePositionalEval+=positionalBonus(cp.pieceTypes[i], cp.getSquareOfPiece(cp.pieceTypes[i], j), whiteEndGame);
else blackPositionalEval+=positionalBonus(cp.pieceTypes[i], cp.getSquareOfPiece(cp.pieceTypes[i], j), blackEndGame);
}
}
//white has only a king
if (whiteMaterialEval==0){
if (cp.numPiecesOfType('q')>0 || cp.numPiecesOfType('r')>0)
blackPositionalEval+=((float) 5)/((float) kingCageSize(WHITE));
}
if (blackMaterialEval==0){
if (cp.numPiecesOfType('Q')>0 || cp.numPiecesOfType('R')>0)
whitePositionalEval+=((float) 5)/((float) kingCageSize(BLACK));
}
eval=whiteMaterialEval+whitePositionalEval-blackMaterialEval-blackPositionalEval;
//eval*=50/(whiteMaterialEval+blackMaterialEval); // incentivize trading down when ahead.
return eval;
}
void ComputerAI_5::makeMove(){
total_eval=0;
cp.setupPosition(game.getCurrentPosition().getFEN());
int depth=3;
while (total_eval<MAX_EVAL){
total_eval=0;
float ev=evaluatePosition(depth);
if (ev>9000 || ev<-9000) break;
depth++;
}
game.makeMove(cp.ChessSquareConverti(bestMove/64), cp.ChessSquareConverti(bestMove%64), 'Q');
}
/*
alpha beta pruning is most efficient when the best moves are evaluated relatatively quickly
thus we order by the fast heuristic of biggest material captures
*/
bool choicePriority::operator() (int move1,int move2){
int endValue1=ComputerAI::pieceValue(toupper(cp.getSquare(move1%64)));
int endValue2=ComputerAI::pieceValue(toupper(cp.getSquare(move2%64)));
if (endValue1>endValue2) return true;
if (endValue2>endValue1) return false;
int begValue1=ComputerAI::pieceValue(toupper(cp.getSquare(move1/64)));
int begValue2=ComputerAI::pieceValue(toupper(cp.getSquare(move2/64)));
return begValue1<begValue2;
}
//so far, the computer was dreadful at checkmating the lone king.
//so, we will incentivize it to make small king cages when only the king is left.
//checks how many squares the king of colour can go to if given infinity moves
int ComputerAI_5::kingCageSize(Colour colour){
bool kingCanReach[64];
for (int i=0;i<64;i++) kingCanReach[i]=false;
int numReach=1;
std::queue<int> toExplore;
toExplore.push(cp.getSquareOfPiece(colour==WHITE?'K':'k', 0));
kingCanReach[toExplore.front()]=true;
//int lastKSqr=toExplore.front();
//int lastKSqrPiece=' ';
int initialKSqr=toExplore.front();
int initialCanEnPassantAt=cp.canEnPassantAt;
cp.canEnPassantAt=-1;
Colour initialColour=cp.colourToMove();
cp.setColourToMove(colour);
cp.setSquare(initialKSqr, ' ');
while (!toExplore.empty()){
int ksqr=toExplore.front();
char lastPiece=cp.getSquare(ksqr);
cp.setSquare(ksqr,colour==WHITE?'K':'k');
vector<int> legalMoves=cp.getLegalMovesFrom(ksqr);
for (int i=0;i<legalMoves.size();i++){
if (!kingCanReach[legalMoves[i]]){
kingCanReach[legalMoves[i]]=true;
toExplore.push(legalMoves[i]);
numReach++;
}
}
cp.setSquare(ksqr, lastPiece);
toExplore.pop();
}
cp.setSquare(initialKSqr, colour==WHITE?'K':'k');
cp.setColourToMove(initialColour);
cp.canEnPassantAt=initialCanEnPassantAt;
total_eval+=numReach*5;
return numReach;
}