-
Notifications
You must be signed in to change notification settings - Fork 1
/
chess.py
246 lines (215 loc) · 8.41 KB
/
chess.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
__filename__ = "chess.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.0.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Tabletop Games"
import re
initial = [
'rnbqkbnr',
'pppppppp',
'........',
'........',
'........',
'........',
'PPPPPPPP',
'RNBQKBNR'
]
uni_pieces = {
'R': '♜', 'N': '♞', 'B': '♝', 'Q': '♛', 'K': '♚', 'P': '♟',
'r': '♖', 'n': '♘', 'b': '♗', 'q': '♕', 'k': '♔', 'p': '♙', '.': '·'
}
uni_pieces_html = {
'r': 'black_rook', 'n': 'black_knight', 'b': 'black_bishop',
'q': 'black_queen', 'k': 'black_king', 'p': 'black_pawn',
'R': 'white_rook', 'N': 'white_knight', 'B': 'white_bishop',
'Q': 'white_queen', 'K': 'white_king', 'P': 'white_pawn'
}
def show_chess_board(board_name: str, game_state: [],
id: int, mud, turn: str) -> None:
"""Shows the chess board
"""
if mud.player_using_web_interface(id):
_show_chess_board_as_html(board_name, game_state, id, mud, turn)
return
mud.send_message(id, '<r>\n')
board_square = '<f255><f15>░'
board_square_empty = '<r><f255> '
board_str = ''
# top and bottom border
border_line = ' '
for x_coord in range(0, 20):
border_line += board_square
border_line = border_line + '\n'
board_str += border_line
i = 0
if turn == 'white':
for row in game_state:
board_row_str = \
' ' + str(8 - i) + ' ' + board_square + board_square
x_coord = 0
for pic in row:
piece_str = uni_pieces[pic]
square_str = board_square_empty
if (x_coord + (i % 2)) % 2 == 0:
square_str = board_square
if pic == '.':
piece_str = square_str
board_row_str += piece_str + square_str
x_coord += 1
board_str += board_row_str + board_square + board_square + '\n'
i += 1
# bottom border
board_str += border_line
board_str += '<r> a b c d e f g h \n\n'
else:
for row in game_state:
board_row_str = ''
x_coord = 0
for pic in row:
piece_str = uni_pieces[pic]
square_str = board_square_empty
if (x_coord + (i % 2)) % 2 == 0:
square_str = board_square
if pic == '.':
piece_str = square_str
board_row_str = piece_str + square_str + board_row_str
x_coord += 1
board_str = \
' ' + str(8 - i) + ' ' + \
board_square + board_square + board_row_str + \
board_square + board_square + '\n' + board_str
i += 1
board_str += '\n<r> h g f e d c b a \n\n'
board_str = border_line + board_str
mud.send_game_board(id, board_str)
def _show_chess_board_as_html(board_name: str, game_state: [], id: int,
mud, turn: str) -> None:
"""Shows the chess board as html for the web interface
"""
if not board_name:
print('No chess board name is specified for this room')
return
board_dir = 'chessboards/' + board_name + '/'
board_html = '<table id="chess">'
i = 0
if turn == 'white':
for row in game_state:
board_html += '<tr>'
# show row number
board_html += '<td>'
board_html += '<label class="coord">' + str(8 - i) + '</label>'
board_html += '</td>'
j = 0
for pic in row:
board_html += '<td><div class="parent">'
if (j + (i % 2)) % 2 == 0:
board_html += '<img class="board" src="' + \
board_dir + 'black_square.png" />'
else:
board_html += '<img class="board" src="' + \
board_dir + 'white_square.png" />'
if pic != '.':
board_html += '<img class="boardpiece" src="' + \
board_dir + uni_pieces_html[pic] + '.png" />'
board_html += '</div></td>'
j += 1
board_html += '</tr>'
i += 1
board_html += '<tr><td> </td>' + \
'<td><label class="coord"><center>a</center></label></td>' + \
'<td><label class="coord"><center>b</center></label></td>' + \
'<td><label class="coord"><center>c</center></label></td>' + \
'<td><label class="coord"><center>d</center></label></td>' + \
'<td><label class="coord"><center>e</center></label></td>' + \
'<td><label class="coord"><center>f</center></label></td>' + \
'<td><label class="coord"><center>g</center></label></td>' + \
'<td><label class="coord"><center>h</center></label></td>' + \
'</tr></table>'
else:
board_html = ''
for row in game_state:
board_row_str = ''
j = 0
for pic in row:
piece_str = '<td><div class="parent">'
if (j + (i % 2)) % 2 == 0:
piece_str += '<img class="board" src="' + \
board_dir + 'black_square.png" />'
else:
piece_str += '<img class="board" src="' + \
board_dir + 'white_square.png" />'
if pic != '.':
piece_str += '<img class="boardpiece" src="' + \
board_dir + uni_pieces_html[pic] + '.png" />'
piece_str += '</div></td>'
board_row_str = piece_str + board_row_str
j += 1
# show row number
board_row_str = '<tr><td><label class="coord">' + \
str(8 - i) + '</label></td>' + \
board_row_str + '</tr>'
board_html = board_row_str + board_html
i += 1
board_html = '<table id="chess">' + board_html
board_html += '<tr><td> </td>' + \
'<td><label class="coord"><center>h</center></label></td>' + \
'<td><label class="coord"><center>g</center></label></td>' + \
'<td><label class="coord"><center>f</center></label></td>' + \
'<td><label class="coord"><center>e</center></label></td>' + \
'<td><label class="coord"><center>d</center></label></td>' + \
'<td><label class="coord"><center>c</center></label></td>' + \
'<td><label class="coord"><center>b</center></label></td>' + \
'<td><label class="coord"><center>a</center></label></td>' + \
'</tr></table>'
mud.send_game_board(id, board_html + '\n')
def initial_chess_board() -> []:
"""Returns the initial state of a chess game
"""
return initial.copy()
def _chess_piece_at(game_state: [], coord: str) -> str:
"""Return the chess piece at the given coord
"""
if len(coord) != 2:
return '.'
if ord(coord[0]) < ord('a') or ord(coord[0]) > ord('h'):
return '.'
if ord(coord[1]) < ord('1') or ord(coord[1]) > ord('8'):
return '.'
return game_state[ord('8') - ord(coord[1])][ord(coord[0]) - ord('a')]
def _chess_piece_set(game_state: [], coord: str, piece: str) -> None:
"""Set the coord of a chess piece
"""
if len(coord) != 2:
return
if ord(coord[0]) < ord('a') or ord(coord[0]) > ord('h'):
return
if ord(coord[1]) < ord('1') or ord(coord[1]) > ord('8'):
return
row = game_state[ord('8') - ord(coord[1])]
col = ord(coord[0]) - ord('a')
game_state[ord('8') - ord(coord[1])] = \
row[:col] + piece + row[(col + 1):]
def move_chess_piece(move_str: str, game_state: [],
turn: str, id: int, mud) -> bool:
"""Move a chess piece
"""
movematch = re.match('([a-h][1-8])'*2, move_str.lower())
if not movematch:
return False
move_from = movematch.group(1)
move_to = movematch.group(2)
from_piece = _chess_piece_at(game_state, move_from)
if from_piece == '.':
return False
if turn == 'white':
if from_piece.upper() != from_piece:
return False
else:
if from_piece.lower() != from_piece:
return False
_chess_piece_set(game_state, move_from, '.')
_chess_piece_set(game_state, move_to, from_piece)
return True