-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchessgame.py
448 lines (394 loc) · 17.2 KB
/
chessgame.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# -*- coding: utf-8 -*-
"""
Created on Thu Feb 6 2016
@author: Kirby Urner
Ideas for facilitating chess on a web page.
In manual mode (no server), the Game.play_game method alternately prompts
black and white to move. Players enter start and end positions in row
column format (rows and columns numbered 0-7). Keeps track of captured
pieces. Makes no attempt to validate the legality of moves.
In server mode, the board with all pieces in starting positions is
displayed and clicking in each square checks with the server for
what it contains. play_game is not implemented in the server version.
The goal was to get as far as having AJAXy GET lookups of the board
on the server, demonstrating 2-way communication without page reloading.
Could be enhanced to:
* provide a server mode that allows actual moves
* persist moves-per-game in a sqlite3 DB or other database for playback
* prevent illegal moves by computing squares legally within range of any piece
* implement more rules e.g. pawn promotion, castle, en passant
"""
from collections import namedtuple
Piece = namedtuple('Piece', 'type color position unicode')
Move = namedtuple('Move', 'Piece start end')
# basic skeleton for HTML. Because setup to use placeholders for
# str.format(), any literal curly braces need to be doubled.
page_template = ("""\
<!DOCTYPE html>
<html>
<head>
<style>table, th, td {{border: 1px solid black;}}
td {{font-size: 30px;}}
td.square {{overflow:hidden; width:40px; height:40px;}}
</style>
<title>Chess Game</title>
</head>
<body onload = "detectServer()">
<div align='center'>Piece selected:<br />
<span id='from_server'>None Selected</span></div>
<br />
<div align='center'>{0}</div>
<br />
<div align="center" id="server_status">{1}</div>
<script type="text/javascript">
this.prevCellId = nil;
this.prevLegalMoves = nil;
// runs when the body loads.Server's sig == tip-off running in server mode
this.live_server = false; // only set to true if Flask left a message.
function detectServer() {{
server_says = document.getElementById("server_status").innerHTML;
if (server_says.indexOf("Flask") != -1){{
this.live_server = true;
}}
}}
// If in server mode, talk with server to find out what if any chess
// piece is in the current location or if selected cell is already green.
// XMLHttpRequest object used for this.
// if cell not already green, turns a clicked-on cell red while
// returning the last clicked cell to its original color based
// on a row-column computation (same used to get original coloring)
// previous greens returned to board coloring. Says what piece was
// clicked on if any due to a GET ?cell request.
// turns on legal next positions if piece select and not already moving
// if clicking on green square, then moving, so move piece with PUT ?move
// requiest, and then turn off all red or green coloring, showing no
// piece is selected, with board updated
function showCell(theCell) {{
oldcolor = theCell.bgColor;
moving = false;
if (oldcolor == "00E800") {{
moving = true;
}}
found = Boolean(this.prevCellId);
// alert("prev id: " + this.prevCellId + " " + String(found));
if (found) {{
oldCell = document.getElementById(this.prevCellId);
// extract row,column of old cell and set bgcolor to dark or light
row = Number(prevCellId.charAt(1));
column = Number(prevCellId.charAt(3));
oldCell.bgColor = (row + column)%2==0 ? ('#ffffff') : ('#a9a9a9');
}}
theCell.bgColor = "E80000";
this.prevCellId = theCell.id;
// alert("old cell_id " + this.prevCellId);
if (this.live_server) {{
// AJAXy stuff here
xhttp = new this.XMLHttpRequest();
xhttp.onreadystatechange = function() {{
// alert("readyState: " + xhttp.readyState);
if (xhttp.readyState == 4 && xhttp.status == 200) {{
data_dict = JSON.parse(xhttp.responseText);
display_text = data_dict['response_text'];
legal_moves = data_dict['legal_moves'];
piece_symbol = data_dict['unicode'];
piece_pos = data_dict['position'];
got_piece = data_dict['has_piece'];
document.getElementById("from_server").innerHTML = display_text;
if ((!moving) && got_piece) {{
for (r = 0; r <= 7; ++r) {{
for (c = 0; c <= 7; ++c ) {{
the_id = "r" + String(r) + "c" + String(c);
theCell = document.getElementById(the_id);
if (theCell.bgColor == "00E800") {{
theCell.bgColor = (r + c)%2==0 ? ('#ffffff') : ('#a9a9a9');
}}
}}
}}
for (cell_id of legal_moves) {{
the_cell = document.getElementById(cell_id);
the_cell.bgColor = "00E800";
}}
}}
if ((!moving) && !got_piece){{
for (r = 0; r <= 7; ++r) {{
for (c = 0; c <= 7; ++c ) {{
the_id = "r" + String(r) + "c" + String(c);
theCell = document.getElementById(the_id);
if (theCell.bgColor == "00E800") {{
theCell.bgColor = (r + c)%2==0 ? ('#ffffff') : ('#a9a9a9');
}}
}}
}}
}}
if (moving) {{
for (r = 0; r <= 7; ++r) {{
for (c = 0; c <= 7; ++c ) {{
the_id = "r" + String(r) + "c" + String(c);
theCell = document.getElementById(the_id);
theCell.bgColor = (r + c)%2==0 ? ('#ffffff') : ('#a9a9a9');
}}
}}
the_id = "r" + String(piece_pos[0]) + "c" + String(piece_pos[1]);
newCell = document.getElementById(the_id);
newCell.innerHTML = piece_symbol;
oldCell.innerHTML = '';
this.prevCellId = nil;
}}
}}
}}
if (moving) {{
row = theCell.id.charAt(1);
column = theCell.id.charAt(3);
putvals = "/move?r=" + row + "&c=" + column;
// alert("PUT happens: " + putvals);
xhttp.open("PUT", putvals, true);
xhttp.send();
}} else {{
row = theCell.id.charAt(1);
column = theCell.id.charAt(3);
getvals = "/cell?r=" + row + "&c=" + column;
// alert("GET happens: " + getvals);
xhttp.open("GET", getvals, true);
xhttp.send();
}}
}}
}}
</script>
</body>
</html>""")
class Board:
def __init__(self, **kwargs): # name="a phrase mentioning Flask" if serving
self.grid = [ ]
for r in range(8):
self.grid.append([None, None, None, None,
None, None, None, None])
self._reset()
# arbitary named arguments become Board object attributes
# an attribute named 'name' is vital for detecting server mode
for attrib,val in kwargs.items():
self.__dict__[attrib]=val
def _reset(self):
self.grid[7] = [
Piece("Rook" , "black", [7,0], "♜"),
Piece("Knight" , "black", [7,1], "♞"),
Piece("Bishop" , "black", [7,2], "♝"),
Piece("Queen" , "black", [7,3], "♛"),
Piece("King" , "black", [7,4], "♚"),
Piece("Bishop" , "black", [7,5], "♝"),
Piece("Knight" , "black", [7,6], "♞"),
Piece("Rook" , "black", [7,7], "♜") ]
self.grid[6] = [ ]
for c in range(8):
self.grid[6].append(Piece("Pawn", "black", [6,c], "♟"))
self.grid[0] = [
Piece("Rook" , "white", [0,0], "♖"),
Piece("Knight" , "white", [0,1], "♘"),
Piece("Bishop" , "white", [0,2], "♗"),
Piece("Queen" , "white", [0,3], "♕"),
Piece("King" , "white", [0,4], "♔"),
Piece("Bishop" , "white", [0,5], "♗"),
Piece("Knight" , "white", [0,6], "♘"),
Piece("Rook" , "white", [0,7], "♖") ]
self.grid[1] = [ ]
for c in range(8):
self.grid[1].append(Piece("Pawn", "white", [1,c], "♙"))
def __str__(self):
global page_template
table = "\n<table name='board' height='400' width='400' cellpadding='15'>\n"
new_row = ""
for r, row in enumerate(self.grid):
new_row = "<tr class='row'>{0}</tr>\n"
squares = ""
for c, content in enumerate(row):
the_id = "r{}c{}".format(r,c)
symbol = content.unicode if isinstance(content, Piece) else " "
if (r+c)%2:
squares += \
"\n<td bgcolor='#a9a9a9' class='square' " + \
"onclick='showCell(this)' id='{0}'>{1}</td>"\
.format(the_id, symbol)
else:
squares += \
"\n<td bgcolor='#ffffff' class='square' " + \
"onclick='showCell(this)'" + \
" id='{0}'>{1}</td>" \
.format(the_id, symbol)
new_row = new_row.format(squares)
table += new_row
table += "\n</table>\n"
sig = " " # nada
if hasattr(self, 'name'):
sig="Brought to you by: " + self.name
render = page_template.format(table, sig)
return render
def _check_for_piece(self, r,c):
piece = self.grid[r][c]
if isinstance(piece, Piece):
return piece
else:
return False
class Player:
def __init__(self, color, game):
self.color = color
self.game = game
self.num_moves = 0
board = self.game.board.grid
if self.color == "black":
self.on_board = board[7] + board[6]
else:
self.on_board = board[0] + board[1]
self.captured = [ ]
self.lost = [ ]
def move(self):
while True: # from here...
try:
print("Enter 'status' or 'resign' or...")
user = input("Pick up a {0.color} piece at: ".format(self))
if user == 'status':
print(self.game.status()) # <-- check status
continue
elif user == "resign":
return self.color + " resigns." # <-- Resigns!
r, c = tuple(map(int, user.split()))
except:
print("Please enter two ints separated by \
a space, status or resign")
continue
my_piece = self.game.board._check_for_piece(r,c)
if not my_piece:
print("No piece in that place.")
continue
elif my_piece.color != self.color:
print("That piece belongs to the other player!")
continue
print("Move a {0.color} {0.type} at {1}".format(my_piece,(r,c)))
ok = input("Is that right? (y/n) :")
if ok == "n":
continue
break
while True: # ... to there
taking = False # becomes True if target is other player piece
try:
newr, newc = \
tuple(map(int,(input("Move {0.color} {0.type} to...?: ".
format(my_piece)).split()))) # new row, new column
except:
print("Please enter two ints separated by a space")
continue
other_piece = self.game.board._check_for_piece(newr, newc)
if not other_piece:
pass
elif other_piece.color != self.color:
taking = True
elif other_piece.color == self.color:
print("One of your own pieces is on that square!")
continue
print("OK, moving the {0.color} {0.type} to {1}.".
format(my_piece,(newr, newc)))
if taking:
print("Taking a {0.color} {0.type}!".format(other_piece))
self._lose_piece(other_piece)
break
my_piece.position[0] = newr
my_piece.position[1] = newc
self.game.board.grid[r][c] = None
self.game.board.grid[newr][newc] = my_piece
self.num_moves += 1
return Move(my_piece, (r, c), (newr, newc))
def _lose_piece(self, piece):
try:
self.other.on_board.remove(piece)
except ValueError:
print("Error: ")
print(repr(self.other))
print("Other player's pieces:", self.other.on_board)
print("To take: ", piece)
print("This player's pieces:", self.on_board)
raise # piece not found
r,c = piece.position
self.game.board.grid[r][c] = None # remove piece
piece.position[0] = None # no location
piece.position[1] = None # no location
self.other.lost.append(piece) # lost to opponent
self.captured.append(piece) # POWs
def __repr__(self):
return "Player: {} Moves: {}".format(self.color, self.num_moves)
class Game:
def __init__(self, **kwargs):
self.server = False # may override with server = "Flask app...."
for attrib,val in kwargs.items():
self.__dict__[attrib]=val
if self.server: # server mode if Flask app is in control
self.board = Board(name=self.server) # passing through
else:
self.board = Board() # manual mode, write html direct to file
with open("chessgame.html", "w") as output:
print(self.board, file = output)
self.player1 = Player("white", self)
self.player2 = Player("black", self)
self.player1.other = self.player2
self.player2.other = self.player1
self.the_moves = [ ] # history of moves
self.num_turns = 0
if not self.server: # not implemented in server version
self.play_game() # manual mode event loop
def _save_move(self, the_move):
self.the_moves.append(the_move)
def play_game(self): # for manual in-console control
turn = self.player1 # white goes first
game_over = False
while not game_over:
if turn == self.player1:
print(" [{}] White's turn...".format(self.player1.num_moves))
the_move = self.player1.move()
if "resigns" in the_move:
game_over = True
self._save_move(the_move)
turn = self.player2
else:
print(" [{}] Black's turn...".format(self.player2.num_moves))
the_move = self.player2.move()
if "resigns" in the_move:
game_over = True
self._save_move(the_move)
turn = self.player1
print("Printing board...")
with open("chessgame.html", "w") as output:
print(self.board, file = output)
print("=================\n")
self.num_turns += 1
if self.num_turns > 5: # optional max number of turns
game_over = True
else:
print("Lets play again someday")
# this would be a place to possibly save all the moves
# in permanent storage e.g. a sqlite3 DB
def reset(self):
self.board = Board()
self.player1 = Player("white", self.board)
self.player2 = Player("black", self.board)
def status(self):
output = ("Status Report\n" + \
"Turn: [{}]".format(self.num_turns) + \
"Player one (white):\n" + \
"Lost: {}\n".format(self.player1.lost) + \
"Captured: {}\n".format(self.player1.captured))
output = (output + "\n" + \
"Player two (black):\n" + \
"Lost: {}\n".format(self.player2.lost) + \
"Captured: {}\n".format(self.player2.captured))
return output
def legal(self, the_piece):
moves = []
if the_piece.type == "Knight":
for x,y in ((1,2),(-1,2),(1,-2),(-1,-2),
(2,1),(2,-1),(-2,1),(-2,-1)):
candidate = (the_piece.position[0] + x,
the_piece.position[1] + y)
if (0 <= candidate[0] <= 7) and (0 <= candidate[1] <= 7):
p = self.board._check_for_piece(*candidate)
if (not p) or (p.color != the_piece.color):
moves.append("r{}c{}".format(*candidate))
return moves
if __name__ == "__main__":
the_game = Game()