Added game repr

master
Tibor Bizjak 2019-09-01 21:29:25 +02:00
parent d757eafe2f
commit dab851ca07
1 changed files with 26 additions and 1 deletions

View File

@ -1,3 +1,5 @@
from unicodedata import lookup
def cross(A, B): def cross(A, B):
return tuple(a+b for a in A for b in B) return tuple(a+b for a in A for b in B)
@ -14,10 +16,31 @@ init_positions = {"pawn" : cross(files, pawn_ranks),
"queen" : cross("d", home_ranks), "queen" : cross("d", home_ranks),
"king" : cross("e", home_ranks) "king" : cross("e", home_ranks)
} }
def unicode_repr(color, piece):
return lookup(color.upper() + " CHESS " + piece.upper())
class Game: class Game:
def __init__(self): def __init__(self):
self.make_board() self.make_board()
def __repr__(self):
# Unicode board representation
r = ""
for rank in ranks:
r += rank + " |"
for file in files:
piece = self.board[file+rank]
if piece == None:
r += " "
else:
r += " " + unicode_repr(*piece)
r += "\n"
r += " +" + "-"*16 + "\n"
r += " "*4 + " ".join(list(files))
return r
def make_board(self): def make_board(self):
self.board = dict((sq, None) for sq in squares) self.board = dict((sq, None) for sq in squares)
# Add pieces # Add pieces
@ -30,10 +53,12 @@ class Game:
for sq in cross(files, "78"): for sq in cross(files, "78"):
self.board[sq] = ("black", self.board[sq]) self.board[sq] = ("black", self.board[sq])
def test(): def test():
game = Game() game = Game()
assert len(squares) == 8**2 assert len(squares) == 8**2
assert sum(map(len, init_positions.values())) == 8*4 assert sum(map(len, init_positions.values())) == 8*4
print(game)
test() test()