-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.py
132 lines (109 loc) · 3.55 KB
/
bot.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
import argparse
import chess
import sys
import chess_state
import evaluators
import minimax
import time
argparser = argparse.ArgumentParser()
argparser.add_argument('--player', "-p", default="white", type=str, help="'[w]hite' or '[b]lack'; the bot player's color")
class Bot(object):
"""
Implements one player of the game.
"""
def __init__(
self,
player=chess.WHITE,
searchdepth=5,
evaluate=evaluators.MaterialEvaluator(),
transposition_tables=True):
self.state = chess_state.ChessState(
evaluate=evaluate,
memoize=transposition_tables)
self.player = player
self.searchdepth = searchdepth
self.wins = 0
self.loses = 0
self.stalemates = 0
def reset_game(self):
self.state.reset()
self.player = chess.WHITE
def choose_move(self):
"""Given the current board state, choose a best move."""
value, move = minimax.alphabeta(self.state,
player=(minimax.MAX
if self.player == chess.WHITE
else minimax.MIN),
maxdepth=self.searchdepth)
return value, move
def make_move(self, move):
"""Modify the board state by making the given move."""
if (self.state.piece_type_at(move.from_square) == chess.PAWN or
self.state.piece_at(move.to_square)):
self.state.values = dict()
self.state.push(move)
class Supervisor():
bots = []
def __init__(self, number):
for i in range(0, number):
self.bots.append(Bot())
def begin(self):
print("starting bot moves")
while True:
for bot in self.bots:
print(bot)
for other_bot in self.bots:
print(other_bot)
m1 = bot.choose_move()
bot.make_move(m1)
m2 = other_bot.choose_move()
other_bot.make_move(m2)
print(other_bot.state)
if bot.state.is_game_over() or other_bot.state.is_game_over():
print("game over!")
bot.reset_game()
other_bot.reset_game()
break
def main():
supervisor = Supervisor(3)
supervisor.begin()
def main1(player=chess.WHITE, searchdepth=5):
b = Bot(player=player, searchdepth=searchdepth)
if player == chess.WHITE:
# if the bot is white, make a first move.
value, m = b.choose_move()
print(value)
print(m.uci())
b.make_move(m)
print(b.state)
print()
while True:
# take a move as input
try:
m = chess.Move.from_uci(input())
except ValueError:
m = chess.Move.from_uci('a1a1')
# verify that it's a possible move
while not b.state.is_legal(m):
print("Illegal move! Try again.\n")
m = chess.Move.from_uci(input())
# make the move
b.make_move(m)
print(b.state)
print()
if b.state.is_game_over():
print("Game over!")
break
# Choose and make a move of our own.
value, m = b.choose_move()
print(value)
print(m.uci())
b.make_move(m)
print(b.state)
print()
if b.state.is_game_over():
print("Game over!")
break
if __name__ == "__main__":
args = argparser.parse_args()
main1()