-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchessAI.cpp
More file actions
280 lines (257 loc) · 12.5 KB
/
Copy pathchessAI.cpp
File metadata and controls
280 lines (257 loc) · 12.5 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
#include <map>
#include <string>
#include <iostream>
#include <algorithm>
#include <algorithm>
using namespace std;
#include "constants.h"
#include "pieces.h"
#include "bitboard.h"
#include "argparser.h"
#include "chessAI.h"
pthread_mutex_t mutex;
int moveCounter = 0;
ChessAI::ChessAI ( ) {
if(ArgParser::setArgs.find("depth") != ArgParser::setArgs.end()) {
depth = ArgParser::setArgs.find("depth")->second;
} else { depth = 5; }
if(ArgParser::setArgs.find("info") != ArgParser::setArgs.end()) {
info = ArgParser::setArgs.find("info")->second;
} else { info = 0; }
}
ChessAI::ChessAI ( int depth ) {
this->depth = depth;
if(ArgParser::setArgs.find("info") != ArgParser::setArgs.end()) {
info = ArgParser::setArgs.find("info")->second;
} else { info = 0; }
}
void * ChessAI::ThreadWorker ( void * arg ) {
double score = AIChooseNextMove(data.side, data.plieCntr, data.enemyMove, data.currentBoard, data.score, data.id, data.castling);
return (void*)(new pair<double,Move>(score,data.enemyMove));
}
uint64_t ChessAI::AllAttacks ( Piece ** Pieces, Values & CurrentBoard ) {
uint64_t attacks = 0;
for(int iterator = 0; iterator < 6; iterator++) {
int pieceCount = Pieces[iterator]->Split(Pieces[iterator]->position, Pieces[iterator]->pieces);
for(int i = 0; i < pieceCount; i++) {
attacks |= Pieces[iterator]->Attacks(CurrentBoard.AllPieces, CurrentBoard.BPieces, CurrentBoard.WPieces, CurrentBoard.Board, i);
}
}
return attacks;
}
bool ChessAI::WKingCheckControl ( Values currentBoard, Move chosenMove ) {
BitBoard::MovePiece(chosenMove.piece->GetPiece(currentBoard), chosenMove.from, chosenMove.to, currentBoard.WPieces, currentBoard);
Piece * Pieces[6] = { &(currentBoard.BPawns), &(currentBoard.BKnights), &(currentBoard.BKing),
&(currentBoard.BBishops), &(currentBoard.BRooks), &(currentBoard.BQueen) };
uint64_t attacks = AllAttacks(Pieces, currentBoard);
if((attacks & currentBoard.WKing.position) == currentBoard.WKing.position) {
return true;
}
return false;
}
bool ChessAI::BKingCheckControl ( Values currentBoard, Move chosenMove ) {
BitBoard::MovePiece(chosenMove.piece->GetPiece(currentBoard), chosenMove.from, chosenMove.to, currentBoard.BPieces, currentBoard);
Piece * Pieces[6] = { &(currentBoard.WPawns), &(currentBoard.WKnights), &(currentBoard.WKing),
&(currentBoard.WBishops), &(currentBoard.WRooks), &(currentBoard.WQueen) };
uint64_t attacks = AllAttacks(Pieces, currentBoard);
if((attacks & currentBoard.BKing.position) == currentBoard.BKing.position) {
return true;
}
return false;
}
double ChessAI::AIChooseNextMove ( bool side, int plieCntr, Move enemyMove, Values CurrentBoard, double score, uint64_t id, int castling ) {
uint64_t allMoves, numOfChilds = 0;
uint64_t wattacks, battacks;
double thisScore = 0;
int pieceCount, moveCount;
Piece * BPieces[6], * WPieces[6];
Move chosenMove;
if(side && enemyMove.piece != NULL) {
if(castling >= 0) {
score += BitBoard::MovePiece(castling, CurrentBoard.BPieces, CurrentBoard);
} else {
score += BitBoard::MovePiece(enemyMove.piece->GetPiece(CurrentBoard), enemyMove.from, enemyMove.to, CurrentBoard.BPieces, CurrentBoard);
}
} else if(enemyMove.piece != NULL) {
if(castling >= 0) {
score += BitBoard::MovePiece(castling, CurrentBoard.WPieces, CurrentBoard);
} else {
score -= 2*(BitBoard::MovePiece(enemyMove.piece->GetPiece(CurrentBoard), enemyMove.from, enemyMove.to, CurrentBoard.WPieces, CurrentBoard));
}
}
if(plieCntr >= depth) return score;
BPieces[0]=&(CurrentBoard.BPawns), BPieces[1]=&(CurrentBoard.BKnights), BPieces[2]=&(CurrentBoard.BKing),
BPieces[3]=&(CurrentBoard.BBishops), BPieces[4]=&(CurrentBoard.BRooks), BPieces[5]=&(CurrentBoard.BQueen);
WPieces[0]=&(CurrentBoard.WPawns), WPieces[1]=&(CurrentBoard.WKnights), WPieces[2]=&(CurrentBoard.WKing),
WPieces[3]=&(CurrentBoard.WBishops), WPieces[4]=&(CurrentBoard.WRooks), WPieces[5]=&(CurrentBoard.WQueen);
if(side == 0) /*CERNI*/ {
for(int iterator = 0; iterator < 6; iterator++) {
pieceCount = BPieces[iterator]->Split(BPieces[iterator]->position, BPieces[iterator]->pieces);
for(int i = 0; i < pieceCount; i++) {
allMoves = BPieces[iterator]->AllMoves(CurrentBoard.AllPieces, CurrentBoard.BPieces, CurrentBoard.WPieces, CurrentBoard.Board, i);
moveCount = BPieces[iterator]->Split(allMoves, BPieces[iterator]->moves);
for(int y = 0; y < moveCount; y++) {
chosenMove.Initialize(BPieces[iterator], BPieces[iterator]->pieces[i], BPieces[iterator]->moves[y], CurrentBoard.BPieces);
if(BKingCheckControl(CurrentBoard, chosenMove)) {
continue;
}
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
if(id == 0) {
numOfChilds++;
data.Initialize(1, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, -1);
Start();
} else {
thisScore += AIChooseNextMove(1, plieCntr+1, chosenMove, CurrentBoard, score, id);
}
}
}
}
if(CurrentBoard.BQueensideCastling) {
if(CurrentBoard.BRooks.position & A1) {
if(!(CurrentBoard.Board & (B8 | C8 | D8))) {
wattacks = AllAttacks(WPieces, CurrentBoard);
if(!(wattacks & (CurrentBoard.BKing.position | D8 | C8))) {
CurrentBoard.BQueensideCastling = false;
CurrentBoard.BKingsideCastling = false;
chosenMove.Initialize(&CurrentBoard.BKing,0,0,0,0);
if(id == 0) {
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
numOfChilds++;
data.Initialize(1, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, 0);
Start();
} else {
thisScore += AIChooseNextMove(1, plieCntr+1, chosenMove, CurrentBoard, score, id, 0);
}
CurrentBoard.BKingsideCastling = true;
}
}
}
}
if(CurrentBoard.BKingsideCastling) {
if(CurrentBoard.BRooks.position & H8) {
if(!(CurrentBoard.Board & (F8 | G8))) {
wattacks = AllAttacks(WPieces, CurrentBoard);
if(!(wattacks & (CurrentBoard.BKing.position | F8 | G8))) {
//kingside castling
CurrentBoard.BQueensideCastling = false;
CurrentBoard.BKingsideCastling = false;
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
chosenMove.Initialize(&CurrentBoard.BKing,0,0,0,1);
if(id == 0) {
numOfChilds++;
data.Initialize(1, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, 1);
Start();
} else {
thisScore += AIChooseNextMove(1, plieCntr+1, chosenMove, CurrentBoard, score, id, 1);
}
CurrentBoard.BQueensideCastling = true;
}
}
}
}
} else if(side == 1)/*BILI*/ {
for(int iterator = 0; iterator < 6; iterator++) {
pieceCount = WPieces[iterator]->Split(WPieces[iterator]->position, WPieces[iterator]->pieces);
for(int i = 0; i < pieceCount; i++) {
allMoves = WPieces[iterator]->AllMoves(CurrentBoard.AllPieces, CurrentBoard.BPieces, CurrentBoard.WPieces, CurrentBoard.Board, i);
moveCount = WPieces[iterator]->Split(allMoves, WPieces[iterator]->moves);
for(int y = 0; y < moveCount; y++) {
chosenMove.Initialize(WPieces[iterator], WPieces[iterator]->pieces[i], WPieces[iterator]->moves[y], CurrentBoard.WPieces);
if(WKingCheckControl(CurrentBoard, chosenMove)) {
continue;
}
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
if(id == 0) {
numOfChilds++;
data.Initialize(0, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, -1);
Start();
} else {
thisScore += AIChooseNextMove(0, plieCntr+1, chosenMove, CurrentBoard, score, id);
}
}
}
}
if(CurrentBoard.WQueensideCastling) {
if(CurrentBoard.WRooks.position & A1) {
if(!(CurrentBoard.Board & (B1 | C1 | D1))) {
battacks = AllAttacks(BPieces, CurrentBoard);
if(!(battacks & (CurrentBoard.WKing.position | D1 | C1))) {
//queenside castling
CurrentBoard.WQueensideCastling = false;
CurrentBoard.WKingsideCastling = false;
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
chosenMove.Initialize(&CurrentBoard.WKing,0,0,0,0);
if(id == 0) {
numOfChilds++;
data.Initialize(0, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, 0);
Start();
} else {
thisScore += AIChooseNextMove(0, plieCntr+1, chosenMove, CurrentBoard, score, id, 0);
}
CurrentBoard.WKingsideCastling = true;
}
}
}
}
if(CurrentBoard.WKingsideCastling) {
if(CurrentBoard.WRooks.position & H1) {
if(!(CurrentBoard.Board & (F1 | G1))) {
battacks = AllAttacks(BPieces, CurrentBoard);
if(!(battacks & (CurrentBoard.WKing.position | F1 | G1))) {
//kingside castling
CurrentBoard.WQueensideCastling = false;
CurrentBoard.WKingsideCastling = false;
if(info == 1) {
pthread_mutex_lock(&mutex);
moveCounter++;
pthread_mutex_unlock(&mutex);
}
chosenMove.Initialize(&CurrentBoard.WKing,0,0,0,1);
if(id == 0) {
numOfChilds++;
data.Initialize(0, plieCntr+1, chosenMove, CurrentBoard, score, numOfChilds, 1);
Start();
} else {
thisScore += AIChooseNextMove(0, plieCntr+1, chosenMove, CurrentBoard, score, id, 1);
}
CurrentBoard.WQueensideCastling = true;
}
}
}
}
}
if(id == 0) {
void ** threadResults = Wait();
for(int i = 0; i < threadCounter; i++) {
if(info == 1) {
cout << "Thread[" << i << "] score: " << (*((pair<double,Move>*)threadResults[i])).first << endl;
}
results.insert(*((pair<double,Move>*)threadResults[i]));
}
if(info == 1) {
cout << "Number of probed moves: " << moveCounter << endl;
}
delete [] threadResults;
}
return score+thisScore;
}