-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_board.py
36 lines (32 loc) · 1.55 KB
/
draw_board.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
def draw_board(board, pieces):
"""Prints a w*h formatted board.
As is, row numbers are flexible, but column letters are explicit and only go to H.
Also updates the board.squares.piece_present attribute with the piece or with None.
"""
# First row: The letters. Repeated for the last row, also
print(" ", end='') # Four spaces in the corner...
for x in range(board.width):
if x != board.width - 1:
print(" {} ".format(chr(65 + x)), end='')
else:
print(" {} ".format(chr(65 + x))) # Last one needs to have a newline!
# Print the numbers and board/piece symbols
for y in range(board.height):
print(" {} ".format(y + 1), end='')
for x in range(board.width):
for piece in pieces:
if piece.x == x and piece.y == y:
print(piece.symbol, end='')
board.square(x, y).piece_present = piece
break
else: # no break; i.e. no piece at that coordinate
print(board.square(x, y).symbol, end='')
board.square(x, y).piece_present = None # If there was a piece there, there ain't now
print(" {} ".format(y + 1)) # No end='' creates the newline
# Print the last row of letters (same as above)
print(" ", end='') # Four spaces in the corner...
for x in range(board.width):
if x != board.width - 1:
print(" {} ".format(chr(65 + x)), end='')
else:
print(" {} ".format(chr(65 + x))) # Newline is a safe bet