-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
277 lines (252 loc) · 7.02 KB
/
data.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
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
# board = [
# ['00','00','00','00','00','00','bQ','00'],
# ['00','00','wP','00','00','00','00','00'],
# ['00','00','00','00','00','bP','00','00'],
# ['00','00','00','00','00','00','00','00'],
# ['00','wQ','00','00','00','00','00','00'],
# ['00','00','00','00','wP','00','00','00'],
# ['00','00','00','00','00','wP','bP','00'],
# ['00','00','00','00','00','00','00','00']
# ]
board = [
['bR','bN','bB','bK','bQ','bB','bN','bR'],
['bP','bP','bP','bP','bP','bP','bP','bP'],
['00','00','00','00','00','00','00','00'],
['00','00','00','00','00','00','00','00'],
['00','00','00','00','00','00','00','00'],
['00','00','00','00','00','00','00','00'],
['wP','wP','wP','wP','wP','wP','wP','wP'],
['wR','wN','wB','wK','wQ','wB','wN','wR']
]
# Positional piece square table
pawn_table = [
[ 80, 80, 80, 80, 80, 80, 80, 80],
[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]
]
knight_table = [
[-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,-90,-30,-30,-30,-30,-90,-50]
]
bishop_table = [
[-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,-90,-10,-10,-90,-10,-20]
]
rook_table = [
[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]
]
king_table = [
[-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, 70, -5,-10,-10,-20]
]
queen_table = [[-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]]
queen_endgame_table = [[-50,-40,-30,-20,-20,-30,-40,-50],
[-30,-20,-10, 0, 0,-10,-20,-30],
[-30,-10, 20, 25, 25, 20,-10,-30],
[-30,-10, 25, 30, 30, 25,-10,-30],
[-30,-10, 25, 30, 30, 25,-10,-30],
[-30,-10, 20, 25, 25, 20,-10,-30],
[-30,-30, 0, 0, 0, 0,-30,-30],
[-50,-30,-30,-30,-30,-30,-30,-50]]
# material evaluation
pawn = 100
knight = 320
bishop = 330
rook = 500
king = 900
queen = 1500
isolated_pawn = -30
doubled_pawn = -30
passed_pawn = 30
def mirror(table):
mirror_table = []
for each_row in table:
mirror_table.insert(0,each_row)
return mirror_table
def evaluateScore(board,total_move):
black_score = 0
white_score = 0
wdP = count_doubled_pawn(board,"w")
bdP = count_doubled_pawn(board,"b")
# print("white doubled: ",wdP)
# print("Black doubled: ",bdP)
wiP = count_isolated_pawn(board,"w")
biP = count_isolated_pawn(board,"b")
# print("White isolated",wiP)
# print("black isolated",biP)
wpP = count_passed_pawn(board,"w")
bpP = count_passed_pawn(board,"b")
# print("white passed pawn",wpP)
# print("black passed pawn",bpP)
for y in range(8):
for x in range(8):
item = board[y][x]
if item == "bP":
value = mirror(pawn_table)[y][x]
black_score += value
black_score += pawn
if item == "bR":
value = mirror(rook_table)[y][x]
black_score += value
black_score += rook
if item == "bN":
value = mirror(knight_table)[y][x]
black_score += value
black_score += knight
if item == "bB":
value = mirror(bishop_table)[y][x]
black_score += value
black_score += bishop
if item == "bK":
value = mirror(king_table)[y][x]
black_score += value
black_score += king
if item == "bQ":
if total_move > 60:
value = mirror(queen_endgame_table)[y][x]
else:
value = mirror(queen_table)[y][x]
black_score += value
black_score += queen
# white
if item == "wP":
value = pawn_table[y][x]
white_score += value
white_score += pawn
if item == "wN":
value = knight_table[y][x]
white_score += value
white_score += knight
if item == "wR":
value = rook_table[y][x]
white_score += value
white_score += rook
if item == "wK":
value = king_table[y][x]
white_score += value
white_score += king
if item == "wB":
value = bishop_table[y][x]
white_score += value
white_score += bishop
if item == "wQ":
if total_move>60:
value = queen_endgame_table[y][x]
else:
value = queen_table[y][x]
white_score += value
white_score += queen
black_score += biP*isolated_pawn + bdP*doubled_pawn + bpP*passed_pawn
white_score += wiP*isolated_pawn + wdP*doubled_pawn + wpP*passed_pawn
# print("black score: ",black_score)
# print("white score: ",white_score)
# print(black_score - white_score)
return black_score - white_score
def count_doubled_pawn(board,player):
doubled_pawn = []
count = 0
for y in range(8):
for x in range(8):
if board[y][x][0] == player and board[y][x][1] == "P":
if x in doubled_pawn:
count +=1
else:
doubled_pawn.append(x)
return count
def count_isolated_pawn(board,player):
isolated_pawn = []
count = 0
for y in range(8):
for x in range(8):
if board[y][x][0] == player and board[y][x][1] == "P":
isolated_pawn.append(x)
for y in range(8):
for x in range(8):
if board[y][x][0] == player and board[y][x][1] == "P":
if x!=0 and x!=7:
if x-1 in isolated_pawn or x+1 in isolated_pawn:
pass
else:
count +=1
if x==0:
if x+1 in isolated_pawn:
pass
else:
count +=1
if x == 7:
if x-1 in isolated_pawn:
pass
else:
count +=1
return count
def count_passed_pawn(board,player):
count = 0
if player == "w":
for y in range(8):
for x in range(8):
if board[y][x][0] == player and board[y][x][1] == "P" and y < 4:
flag = True
for s in range(8):
for r in range(8):
if board[s][r] != player and board[s][r][1] == "P" and s< 4:
if r==x or r == x -1 or r == x + 1:
flag = False
break
if flag:
count +=1
if player =="b":
for y in range(8):
for x in range(8):
if board[y][x][0] == player and board[y][x][1] == "P" and y >= 4:
flag = True
for s in range(8):
for r in range(8):
if board[s][r] != player and board[s][r][1] == "P" and s >=4:
if r==x or r == x -1 or r == x + 1:
flag = False
break
if flag:
count +=1
return count
def print_board(board):
for y in range(8):
for x in range(8):
print(board[y][x], end=" ")
print()