-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
304 lines (239 loc) · 8.34 KB
/
app.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python3
# coding : utf-8
from flask import Flask, Response, request, render_template, url_for
import chess, chess.pgn
import chess.engine
import traceback
import time
import collections
import json
from gevent.pywsgi import WSGIServer
class Player(object):
def __init__(self, board, game_time=300):
self.__current_board = board
def make_move(self, move):
raise NotImplementedError()
class Player1(Player):
def __init__(self, board, game_time=300):
self.__current_board = board
self.__game_time = game_time
self.__time_left = self.__game_time
self.__first_move_timestamp = None
def get_board(self):
return self.__current_board
def set_board(self, board):
self.__current_board = board
def make_move(self, move):
if self.__current_board.turn == True:
if self.__first_move_timestamp is not None:
self.__first_move_timestamp = int(time.time())
try:
self.__current_board.push_san(move)
except ValueError:
print('Not a legal move')
else:
print("Error: ****It's Blacks Turn (Player2)***")
return self.__current_board
def undo_last_move(self):
self.__current_board.pop()
return self.__current_board
def is_turn(self):
return self.__current_board.turn == True
def get_game_time(self):
return self.__game_time
def get_time_left(self):
return self.__time_left
def reset(self):
self.__current_board = None
self.__time_left = self.__game_time
self.__first_move_timestamp = None
class Player2(Player):
def __init__(self, board, game_time=300):
self.__current_board = board
self.__game_time = game_time
self.__time_left = self.__game_time
self.__first_move_timestamp = None
self.__engine = False
def get_board(self):
return self.__current_board
def set_board(self, board):
self.__current_board = board
def make_move(self, move):
if self.__current_board.turn == False:
if self.__first_move_timestamp is not None:
self.__first_move_timestamp = int(time.time())
try:
self.__current_board.push_san(move)
except ValueError:
print('Not a legal move')
else:
print("Error: ****It's White's Turn (Player1)***")
return self.__current_board
def undo_last_move(self):
self.__current_board.pop()
return self.__current_board
def is_turn(self):
return self.__current_board.turn == False
def get_game_time(self):
return self.__game_time
def get_time_left(self):
return self.__time_left
def reset(self):
self.__current_board = None
self.__time_left = self.__game_time
self.__first_move_timestamp = None
def init_stockfish(self):
self.__is_engine = True
try:
self.__engine = chess.engine.SimpleEngine.popen_uci("/usr/bin/stockfish")
return True
except Exception:
return False
def is_engine(self):
return self.__engine
def engine_move(self):
result = self.__engine.play(self.__current_board, chess.engine.Limit(time=0.100))
move = result.move
try:
self.__current_board.push(move)
except Exception:
print("Cant push move")
return self.__current_board
def board_to_game(board):
game = chess.pgn.Game()
# undo all moves
switchyard = collections.deque()
while board.move_stack:
switchyard.append(board.pop())
game.setup(board)
node = game
# Replay all moves
while switchyard:
move = switchyard.pop()
node = node.add_variation(move)
board.push(move)
game.headers["Result"] = board.result()
return game
def console_demo():
global board
board = chess.Board()
p1 = Player1(board)
p2 = Player2(board)
print(board)
print("------------------------------------------")
while True:
move_san = input('White move: ').strip()
board = p1.make_move(move_san)
print(board)
print('-'*50)
move_san = input('Black to move: ').strip()
board = p2.make_move(move_san)
print(board)
print("-"*50)
def run_game():
global board
global undo_moves_stack
undo_moves_stack = []
board = chess.Board()
Human = Player1(board)
engine = Player2(board)
engine.init_stockfish()
app = Flask(__name__, static_url_path='')
@app.route('/', methods=['GET'])
def index():
global board
return render_template('index.html', fen=board.board_fen(), pgn=str(board_to_game(board).mainline_moves()))
@app.route('/move', methods=['GET'])
def move():
global board
global undo_moves_stack
if not board.is_game_over():
move_san = request.args.get('move', default='')
if move_san is not None and move_san != '':
try:
if Human.is_turn():
print("White's turn to play:")
else:
print("Black's turn to play")
if Human.is_turn():
board = Human.make_move(str(move_san))
undo_moves_stack = [] #make undo moves stack empty if any move is done.
if engine.is_turn():
board = engine.engine_move()
print(board)
except Exception:
traceback.print_exc()
game_moves_san = [move_uci.san() for move_uci in board_to_game(board).mainline()]
print(game_moves_san)
if board.is_game_over():
resp = {'fen': board.board_fen(), 'moves': game_moves_san, 'game_over': 'true'}
else:
resp = {'fen': board.board_fen(), 'moves': game_moves_san, 'game_over': 'false'}
response = app.response_class(
response=json.dumps(resp),
status=200,
mimetype='application/json'
)
return response
else:
game_moves_san = [move_uci.san() for move_uci in board_to_game(board).mainline()]
print(game_moves_san)
resp = {'fen': board.board_fen(), 'moves': game_moves_san, 'game_over': 'true'}
response = app.response_class(
response=json.dumps(resp),
status=200,
mimetype='application/json'
)
return response
return index()
@app.route("/reset", methods=["GET"])
def reset():
global board
Human.reset()
engine.reset()
board = chess.Board()
Human.set_board(board)
engine.set_board(board)
resp = {"fen": board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves())}
response = app.response_class(
response=json.dumps(resp),
status=200,
mimetype='application/json'
)
return response
@app.route("/undo", methods=["GET"])
def undo():
global board
global undo_moves_stack
try:
undo_moves_stack.append(board.pop())
except IndexError:
print("fuck")
resp = {'fen': board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves())}
response = app.response_class(
response=json.dumps(resp),
status=200,
mimetype='application/json'
)
return response
@app.route("/redo", methods=["GET"])
def redo():
global board
global undo_moves_stack
if len(undo_moves_stack) != 0:
board.push(undo_moves_stack.pop())
else:
pass
resp = {'fen': board.board_fen(), 'pgn': str(board_to_game(board).mainline_moves())}
response = app.response_class(
response=json.dumps(resp),
status=200,
mimetype='application/json'
)
return response
http_server = WSGIServer(('0.0.0.0', 1337), app)
http_server.serve_forever()
#app.run(host='127.0.0.1', debug=True)
if __name__ == "__main__":
#console_demo()
run_game()