-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent.py
214 lines (184 loc) · 7.63 KB
/
agent.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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import random
import sys
from state import *
import chess
import chess.polyglot
PIECE_VALUES = { chess.PAWN: 1, chess.KNIGHT: 3, chess.BISHOP: 3, chess.ROOK: 5, chess.QUEEN: 9 }
class Player(object):
MAX_DEPTH = 2
INF = 1000000
def __init__(self):
self.opening_book = chess.polyglot.MemoryMappedReader('./baron30.bin')
self.reset()
def reset(self):
self.game = Chess()
self.my_player = 1
self.my_color = chess.BLACK
self.say('RDY')
def say(self, what):
sys.stdout.write(what)
sys.stdout.write('\n')
sys.stdout.flush()
def hear(self):
line = sys.stdin.readline().split()
return line[0], line[1:]
def loop(self):
while True:
cmd, args = self.hear()
if cmd == 'HEDID':
unused_move_timeout, unused_game_timeout = args[:2]
move = args[2]
self.game.update(move)
print(self.game.board)
elif cmd == 'ONEMORE':
self.reset()
continue
elif cmd == 'BYE':
break
else:
assert cmd == 'UGO'
self.my_player = 0
self.my_color = chess.WHITE
try:
opening_book_entry = self.opening_book.choice(self.game.board)
if opening_book_entry:
move = opening_book_entry.move.uci()
except:
moves = self.game.moves()
move = max(moves, key=lambda m: self.min_value(self.game.do_move(m), -Player.INF, Player.INF, 0))
self.game.update(move)
self.say('IDO ' + move)
print(self.game.board)
def evaluate_material(self, game: Chess, player: chess.Color):
res = 0
for piece, value in PIECE_VALUES.items():
for _ in game.board.pieces(piece, player):
res += value
return res
def evaluate_mobility(self, game: Chess):
if game.board.turn == self.my_color:
return game.board.legal_moves.count()
game.board.push(chess.Move.null())
res = game.board.legal_moves.count()
game.board.pop()
return res
def evaluate_center_control(self, game: Chess):
center_squares = { chess.D4, chess.D5, chess.E4, chess.E5 }
outer_center_squares = { chess.D3, chess.D6, chess.C6,
chess.C5, chess.C4, chess.C3,
chess.E6, chess.E3, chess.F6,
chess.F5, chess.F4, chess.F3 }
res = 0
for move in game.board.legal_moves:
if move.to_square in center_squares: res += 2
elif move.to_square in outer_center_squares: res += 1
for square in center_squares:
p = game.board.piece_at(square)
if p and p.color == self.my_color:
if p.piece_type in PIECE_VALUES.keys():
res += PIECE_VALUES[p.piece_type]
if p.piece_type == chess.PAWN:
res += 5
for square in outer_center_squares:
p = game.board.piece_at(square)
if p and p.color == self.my_color:
if p.piece_type in PIECE_VALUES.keys():
res += PIECE_VALUES[p.piece_type]
if p.piece_type == chess.PAWN:
res += 5
return res
def evaluate_pawn_shield(self, game: Chess):
pawn_shields_for_king = {
chess.G1: (chess.F2, chess.G2, chess.H2),
chess.H1: (chess.F2, chess.G2, chess.H2),
chess.B1: (chess.C2, chess.B2, chess.A2),
chess.A1: (chess.C2, chess.B2, chess.A2),
chess.A8: (chess.C7, chess.B7, chess.A7),
chess.B8: (chess.C7, chess.B7, chess.A7),
chess.G8: (chess.F7, chess.G7, chess.H7),
chess.H8: (chess.F7, chess.G7, chess.H7)}
res = 0
king_squares = game.board.pieces(chess.KING, self.my_color)
for king_sq in king_squares:
if king_sq in pawn_shields_for_king.keys():
for pawn_sq in pawn_shields_for_king[king_sq]:
if game.board.piece_type_at(pawn_sq) == chess.PAWN:
res += 1
return res
def evaluate_threats(self, game: Chess, player: chess.Color):
_game = copy(game)
if game.board.turn == player:
_game.board.push(chess.Move.null())
res = 0
for move in _game.board.legal_moves:
piece = _game.board.piece_at(move.to_square)
if piece and piece.color == self.my_color:
if piece.piece_type == chess.KING:
res += 20
else:
res += PIECE_VALUES[piece.piece_type]
return res
def evaluate_piece_activation(self, game: Chess, player: chess.Color):
starting_piece_positions = {
chess.WHITE: {
chess.ROOK: { chess.H1, chess.A1 },
chess.KNIGHT: { chess.G1, chess.B1 },
chess.BISHOP: { chess.F1, chess.C1 },
chess.QUEEN: { chess.D1 }
},
chess.BLACK: {
chess.ROOK: { chess.H8, chess.A8 },
chess.KNIGHT: { chess.G8, chess.B8 },
chess.BISHOP: { chess.F8, chess.C8 },
chess.QUEEN: { chess.D8 }
}}
res = 0
for piece, starting_positions in starting_piece_positions[player].items():
for square in starting_positions:
p = game.board.piece_at(square)
if p and p.color == player and p.piece_type == piece:
res += PIECE_VALUES[p.piece_type]
return res
def evaluate(self, game: Chess):
return ( 5 * (self.evaluate_material(game, self.my_color) - self.evaluate_material(game, not self.my_color))
+ 2 * self.evaluate_mobility(game)
+ 2 * self.evaluate_center_control(game)
+ 1 * self.evaluate_pawn_shield(game)
- 4 * self.evaluate_threats(game, self.my_color)
+ 4 * self.evaluate_threats(game, not self.my_color)
- 3 * self.evaluate_piece_activation(game, self.my_color))
def min_value(self, game: Chess, alpha, beta, depth):
if depth >= Player.MAX_DEPTH: return self.evaluate(game)
outcome = game.board.outcome()
if outcome:
if outcome.winner == self.my_color:
return Player.INF
elif outcome.winner == (not self.my_color):
return -Player.INF
value = Player.INF
for s in [game.do_move(m) for m in game.moves()]:
value = min(value, self.max_value(s, alpha, beta, depth + 1))
if value <= alpha:
return value
beta = min(beta, value)
return value
def max_value(self, game: Chess, alpha, beta, depth):
if depth >= Player.MAX_DEPTH: return self.evaluate(game)
outcome = game.board.outcome()
if outcome:
if outcome.winner == self.my_color:
return Player.INF
elif outcome.winner == (not self.my_color):
return -Player.INF
value = -Player.INF
for s in [game.do_move(m) for m in game.moves()]:
value = max(value, self.min_value(s, alpha, beta, depth + 1))
if value >= beta:
return value
alpha = max(alpha, value)
return value
if __name__ == '__main__':
player = Player()
player.loop()