-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEltonChessZero.py
More file actions
191 lines (166 loc) · 6.41 KB
/
EltonChessZero.py
File metadata and controls
191 lines (166 loc) · 6.41 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
import random
piece_value = { "K": 0, "Q": 10, "R": 5, "B": 3, "N": 3, "p": 1 }
piece_position_values = {
"N": [
[0.0, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.0],
[0.1, 0.3, 0.5, 0.5, 0.5, 0.5, 0.3, 0.1],
[0.2, 0.5, 0.6, 0.65, 0.65, 0.6, 0.5, 0.2],
[0.2, 0.55, 0.65, 0.7, 0.7, 0.65, 0.55, 0.2],
[0.2, 0.5, 0.65, 0.7, 0.7, 0.65, 0.5, 0.2],
[0.2, 0.55, 0.6, 0.65, 0.65, 0.6, 0.55, 0.2],
[0.1, 0.3, 0.5, 0.55, 0.55, 0.5, 0.3, 0.1],
[0.0, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.0]
],
"B": [
[0.0, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.0],
[0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.2],
[0.2, 0.4, 0.5, 0.6, 0.6, 0.5, 0.4, 0.2],
[0.2, 0.5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.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
],
"p": [
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
[0.15, 0.1, 0.2, 0.3, 0.3, 0.2, 0.1, 0.15],
[0.2, 0.05, 0.1, 0.25, 0.25, 0.1, 0.05, 0.2],
[0.2, 0.0, 0.0, 0.2, 0.2, 0.0, 0.0, 0.2],
[0.15, 0.05, 0.1, 0.0, 0.0, 0.1, 0.05, 0.15],
[0.05, 0.1, 0.1, 0.2, 0.2, 0.1, 0.1, 0.05],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
]
}
CHECKMATE = 1000
STALEMATE = 0
DEPTH = 3
#positional advantage
# - pin to the king or a piece is good
def findRandomMove(valid_moves):
"""
Picks and returns a random valid move.
"""
print("Valid moves", [move.getChessNotation() for move in valid_moves])
return valid_moves[random.randint(0, len(valid_moves) - 1)]
def findBestMove(gs, valid_moves):
"""
Find the best move using the minimax algorithm.
"""
global next_move,counter
next_move = None
random.shuffle(valid_moves)
counter = 0
findMoveNegaMaxAlphaBeta(gs, valid_moves, DEPTH, -CHECKMATE, CHECKMATE, 1 if gs.white_to_move else -1)
print("Moves gone through" ,counter)
return next_move
def findMoveNegaMaxAlphaBeta(gs, valid_moves, depth,alpha,beta, turn):
global next_move,counter
counter += 1
if depth == 0:
#score = scoreBoard(gs)
return turn * evaluateBoard(gs)
#move ordering - implement later
max_score = -CHECKMATE
for move in valid_moves:
gs.makeMove(move)
next_moves = gs.getValidMoves()
score = -findMoveNegaMaxAlphaBeta(gs, next_moves, depth - 1, -beta, -alpha, -turn)
if score > max_score:
max_score = score
if depth == DEPTH:
next_move = move
print(score, " ",next_move)
gs.undoMove()
if max_score > alpha: #prunning
alpha = max_score
if alpha >= beta:
break
return max_score
def evaluateBoard(gs):
"""
Evaluate the board and return a score.
"""
if gs.checkmate:
if gs.white_to_move:
return -CHECKMATE # black wins
else:
return CHECKMATE # white wins
elif gs.stalemate:
return STALEMATE
score = 0
for row in range(len(gs.board)):
for col in range(len(gs.board[row])):
piece = gs.board[row][col]
if piece != "--":
piece_position_value = piece_position_values.get(piece[1], [[0]*8]*8)[row][col]
#print(piece_position_value,"at",[row][col])
if piece[0] == 'w':
score += piece_value[piece[1]] + piece_position_value
elif piece[0] == 'b':
score -= piece_value[piece[1]] + piece_position_value
return score
def scoreBoard(gs):
if gs.checkmate:
if gs.white_to_move:
return -CHECKMATE
else:
return CHECKMATE
elif gs.stalemate:
return STALEMATE
score = 0
for row in range(len(gs.board)):
for col in range(len(gs.board[row])):
square = gs.board[row][col]
#Openninng - Score for opening
openningScore = 0
# Moving new pieces is good every move
#print("Here " + square)
# controlling the center with pawns is good
# moving the queen early is bad
# moving the same piece multiple times is bad
# castling is good
#Middle game - Score for middle game
middleGameScore = 0
# controlling the center is good
#End game - Score for end game
endGameScore = 0
# king safety is important
# pawn promotion is good
# king activity is good
if col == "--":
if col[0] == 'w':
score += piece_value[col[1]]
elif square[0] == 'b':
score -= piece_value[square[1]]
return score