forked from boosungkim/custom-chess-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathai_engine.py
196 lines (183 loc) · 7.08 KB
/
ai_engine.py
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
#
# The Chess AI class
# Will utilize minimax and alpha beta pruning
#
# Author: Boo Sung Kim
# Note: Code inspired from the pseudocode by Sebastian Lague
# from enums import Player
# TODO: switch undo moves to stack data structure
import chess_engine
from enums import Player
class chess_ai:
'''
call minimax with alpha beta pruning
evaluate board
get the value of each piece
'''
def minimax_white(self, game_state, depth, alpha, beta, maximizing_player, player_color):
csc = game_state.checkmate_stalemate_checker()
if maximizing_player:
if csc == 0:
return 5000000
elif csc == 1:
return -5000000
elif csc == 2:
return 100
elif not maximizing_player:
if csc == 1:
return 5000000
elif csc == 0:
return -5000000
elif csc == 2:
return 100
if depth <= 0 or csc != 3:
return self.evaluate_board(game_state, Player.PLAYER_1)
if maximizing_player:
max_evaluation = -10000000
all_possible_moves = game_state.get_all_legal_moves("black")
for move_pair in all_possible_moves:
game_state.move_piece(move_pair[0], move_pair[1], True)
evaluation = self.minimax_white(game_state, depth - 1, alpha, beta, False, "white")
game_state.undo_move()
if max_evaluation < evaluation:
max_evaluation = evaluation
best_possible_move = move_pair
alpha = max(alpha, evaluation)
if beta <= alpha:
break
if depth == 3:
return best_possible_move
else:
return max_evaluation
else:
min_evaluation = 10000000
all_possible_moves = game_state.get_all_legal_moves("white")
for move_pair in all_possible_moves:
game_state.move_piece(move_pair[0], move_pair[1], True)
evaluation = self.minimax_white(game_state, depth - 1, alpha, beta, True, "black")
game_state.undo_move()
if min_evaluation > evaluation:
min_evaluation = evaluation
best_possible_move = move_pair
beta = min(beta, evaluation)
if beta <= alpha:
break
if depth == 3:
return best_possible_move
else:
return min_evaluation
def minimax_black(self, game_state, depth, alpha, beta, maximizing_player, player_color):
csc = game_state.checkmate_stalemate_checker()
if maximizing_player:
if csc == 1:
return 5000000
elif csc == 0:
return -5000000
elif csc == 2:
return 100
elif not maximizing_player:
if csc == 0:
return 5000000
elif csc == 1:
return -5000000
elif csc == 2:
return 100
if depth <= 0 or csc != 3:
return self.evaluate_board(game_state, Player.PLAYER_2)
if maximizing_player:
max_evaluation = -10000000
all_possible_moves = game_state.get_all_legal_moves("white")
for move_pair in all_possible_moves:
game_state.move_piece(move_pair[0], move_pair[1], True)
evaluation = self.minimax_black(game_state, depth - 1, alpha, beta, False, "black")
game_state.undo_move()
if max_evaluation < evaluation:
max_evaluation = evaluation
best_possible_move = move_pair
alpha = max(alpha, evaluation)
if beta <= alpha:
break
if depth == 3:
return best_possible_move
else:
return max_evaluation
else:
min_evaluation = 10000000
all_possible_moves = game_state.get_all_legal_moves("black")
for move_pair in all_possible_moves:
game_state.move_piece(move_pair[0], move_pair[1], True)
evaluation = self.minimax_black(game_state, depth - 1, alpha, beta, True, "white")
game_state.undo_move()
if min_evaluation > evaluation:
min_evaluation = evaluation
best_possible_move = move_pair
beta = min(beta, evaluation)
if beta <= alpha:
break
if depth == 3:
return best_possible_move
else:
return min_evaluation
def evaluate_board(self, game_state, player):
evaluation_score = 0
for row in range(0, 8):
for col in range(0, 8):
if game_state.is_valid_piece(row, col):
evaluated_piece = game_state.get_piece(row, col)
evaluation_score += self.get_piece_value(evaluated_piece, player)
return evaluation_score
def get_piece_value(self, piece, player):
if player is Player.PLAYER_1:
if piece.is_player("black"):
if piece.get_name() is "k":
return 1000
elif piece.get_name() is "q":
return 100
elif piece.get_name() is "r":
return 50
elif piece.get_name() is "b":
return 30
elif piece.get_name() is "n":
return 30
elif piece.get_name() is "p":
return 10
else:
if piece.get_name() is "k":
return -1000
elif piece.get_name() is "q":
return -100
elif piece.get_name() is "r":
return -50
elif piece.get_name() is "b":
return -30
elif piece.get_name() is "n":
return -30
elif piece.get_name() is "p":
return -10
else:
if piece.is_player("white"):
if piece.get_name() is "k":
return 1000
elif piece.get_name() is "q":
return 100
elif piece.get_name() is "r":
return 50
elif piece.get_name() is "b":
return 30
elif piece.get_name() is "n":
return 30
elif piece.get_name() is "p":
return 10
else:
if piece.get_name() is "k":
return -1000
elif piece.get_name() is "q":
return -100
elif piece.get_name() is "r":
return -50
elif piece.get_name() is "b":
return -30
elif piece.get_name() is "n":
return -30
elif piece.get_name() is "p":
return -10