-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessAI.py
More file actions
371 lines (338 loc) · 17.1 KB
/
ChessAI.py
File metadata and controls
371 lines (338 loc) · 17.1 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
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
362
363
364
365
366
367
368
369
370
371
import Bishop
import King
import Knight
import Pawn
import random
import numpy as np
import copy
import Queen
import Rook
chess = {
'b_checker': u'\u25FB',
'b_pawn': u'\u265F',
'b_rook': u'\u265C',
'b_knight': u'\u265E',
'b_bishop': u'\u265D',
'b_king': u'\u265A',
'b_queen': u'\u265B',
'w_checker': u'\u25FC',
'w_pawn': u'\u2659',
'w_rook': u'\u2656',
'w_knight': u'\u2658',
'w_bishop': u'\u2657',
'w_king': u'\u2654',
'w_queen': u'\u2655'
}
def getchecks():
bw_row = [chess['b_checker'], chess['w_checker']] * 4
# print(bw_row)
bw_checkers = []
for i in range(8):
bw_checkers.append(bw_row if i % 2 == 0 else bw_row[::-1])
# print(bw_checkers)
bw_checkers = np.array(bw_checkers)
# print(bw_checkers)
wb_checkers = bw_checkers[::-1]
# print(wb_checkers)
board = []
for _ in range(4):
board.append(['0'] * 8)
return wb_checkers
def board_state_change(temp_board, board, temp_input, temp_output):
temp_piece = board[temp_input[0]][temp_input[1]]
board[temp_input[0]][temp_input[1]] = temp_board[temp_input[0]][temp_input[1]]
board[temp_output[0]][temp_output[1]] = temp_piece
return board
class Bot:
piece_score = {'q': 9, 'k': 0, 'n': 3, 'r': 5, 'p': 1, 'b': 3}
mate = 500
def __init__(self, color, board):
self.color = color
self.board = board
def generatemove(self, board):
c = self.color.lower()
moves = []
temp_input = []
# AI IS PLAYING BLACK COLOR
if c == 'b':
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == chess['w_pawn']:
temp_input.append(i)
temp_input.append(j)
p = Pawn.Pawn(c, temp_input, 0)
possible_moves = p.ValidMove(board)
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'p'])
possible_moves.clear()
elif board[i][j] == chess['w_rook']:
temp_input.append(i)
temp_input.append(j)
rook_obj = Rook.Rook()
possible_moves = rook_obj.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'r'])
possible_moves.clear()
elif board[i][j] == chess['w_bishop']:
temp_input.append(i)
temp_input.append(j)
obj_bishop = Bishop.Bishop()
possible_moves = obj_bishop.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'b'])
possible_moves.clear()
elif board[i][j] == chess['w_queen']:
temp_input.append(i)
temp_input.append(j)
obj_queen = Queen.Queen()
possible_moves = obj_queen.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'q'])
possible_moves.clear()
elif board[i][j] == chess['w_knight']:
temp_input.append(i)
temp_input.append(j)
obj_knight = Knight.Knight()
possible_moves = obj_knight.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'k'])
possible_moves.clear()
elif board[i][j] == chess['w_king']:
temp_input.append(i)
temp_input.append(j)
obj_king = King.King()
possible_moves = obj_king.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'kn'])
possible_moves.clear()
temp_input.clear()
count = random.randint(0, len(moves))
for i in range(len(moves)):
for j in range(len(moves[i])):
if i == count and j == 0:
return moves[i][j], moves[i][j + 1], moves[i][j + 2]
# AI IS PLAYING WHITE COLOR
elif c == 'w':
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == chess['b_pawn']:
temp_input.append(i)
temp_input.append(j)
p = Pawn.Pawn(c, temp_input, 0)
possible_moves = p.ValidMove(board)
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'p'])
possible_moves.clear()
elif board[i][j] == chess['b_rook']:
temp_input.append(i)
temp_input.append(j)
rook_obj = Rook.Rook()
possible_moves = rook_obj.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'r'])
possible_moves.clear()
elif board[i][j] == chess['b_bishop']:
temp_input.append(i)
temp_input.append(j)
obj_bishop = Bishop.Bishop()
possible_moves = obj_bishop.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'b'])
possible_moves.clear()
elif board[i][j] == chess['b_queen']:
temp_input.append(i)
temp_input.append(j)
obj_queen = Queen.Queen()
possible_moves = obj_queen.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'q'])
possible_moves.clear()
elif board[i][j] == chess['b_knight']:
temp_input.append(i)
temp_input.append(j)
obj_knight = Knight.Knight()
possible_moves = obj_knight.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'k'])
possible_moves.clear()
elif board[i][j] == chess['b_king']:
temp_input.append(i)
temp_input.append(j)
obj_king = King.King()
possible_moves = obj_king.possibleMoves(board, c, i, j)
print(possible_moves)
if possible_moves is not None:
for l in range(len(possible_moves)):
for m in range(len(possible_moves[l])):
if m == 0:
temp_output = list()
temp_output.append(possible_moves[l][m])
temp_output.append(possible_moves[l][m + 1])
moves.append([temp_input[:], temp_output[:], 'kn'])
possible_moves.clear()
temp_input.clear()
return moves
def scorePiece(self, board):
score = 0
for i in range(len(board)):
for j in range(len(board[i])):
if self.color == 'W':
if board[i][j] == chess['b_pawn']:
score += self.piece_score['p']
elif board[i][j] == chess['b_rook']:
score += self.piece_score['r']
elif board[i][j] == chess['b_bishop']:
score += self.piece_score['b']
elif board[i][j] == chess['b_knight']:
score += self.piece_score['n']
elif board[i][j] == chess['b_queen']:
score += self.piece_score['q']
elif self.color == 'B':
if board[i][j] == chess['w_pawn']:
score -= self.piece_score['p']
elif board[i][j] == chess['w_rook']:
score -= self.piece_score['r']
elif board[i][j] == chess['w_bishop']:
score -= self.piece_score['b']
elif board[i][j] == chess['w_knight']:
score -= self.piece_score['n']
elif board[i][j] == chess['w_queen']:
score -= self.piece_score['q']
return score
def score_calculate_white(self, board):
score = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == chess['b_pawn']:
score += self.piece_score['p']
elif board[i][j] == chess['b_rook']:
score += self.piece_score['r']
elif board[i][j] == chess['b_bishop']:
score += self.piece_score['b']
elif board[i][j] == chess['b_knight']:
score += self.piece_score['n']
elif board[i][j] == chess['b_queen']:
score += self.piece_score['q']
return score
def score_calculate_black(self, board):
score = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == chess['w_pawn']:
score += self.piece_score['p']
elif board[i][j] == chess['w_rook']:
score += self.piece_score['r']
elif board[i][j] == chess['w_bishop']:
score += self.piece_score['b']
elif board[i][j] == chess['w_knight']:
score += self.piece_score['n']
elif board[i][j] == chess['w_queen']:
score += self.piece_score['q']
return score
def findBestMove(self):
# WHEN AI IS BLACK, WE FIND THE MAX BEST SCORE FOR BLACK WHILE WHITE SCORE IS MIN
if self.color == 'B':
moves = self.generatemove(self.board) # input, output
temp_board1 = copy.deepcopy(self.board)
tmp = getchecks()
best_black_score = -999
best_white_score = 999
black_score = []
white_score = []
moves_white = self.generatemove(self.board)
k = 0
# CALCULATING SCORES
for i in range(len(moves)):
new_board = board_state_change(tmp, temp_board1, moves[i][k], moves[i][k + 1])
score = self.scorePiece(new_board)
white_score.append(self.score_calculate_white(new_board))
black_score.append(int(score))
# FINDING MAX BEST SCORE FOR BLACK AND MIN BEST SCORE FOR WHITE
for i in range(len(black_score)):
if black_score[i] > best_black_score:
best_score = black_score[i]
if white_score[i] < best_white_score:
best_white_score = white_score[i]
# SHUFFLING THE ARRAY
random.shuffle(moves)
# FINDING THE MOVE WHERE BLACK HAS MAX SCORE AND WHITE HAS MIN SCORE
for i in range(len(moves)):
original_board = copy.deepcopy(self.board)
new_board = board_state_change(tmp, original_board, moves[i][k], moves[i][k + 1])
score = self.scorePiece(new_board)
score_w = self.score_calculate_white(new_board)
if best_score == score and score_w == best_white_score:
return moves[i][k], moves[i][k + 1], moves[i][k + 2]