Added Piece class
parent
dab851ca07
commit
8868e919b5
24
chess.py
24
chess.py
|
@ -17,9 +17,17 @@ init_positions = {"pawn" : cross(files, pawn_ranks),
|
||||||
"king" : cross("e", home_ranks)
|
"king" : cross("e", home_ranks)
|
||||||
}
|
}
|
||||||
|
|
||||||
def unicode_repr(color, piece):
|
|
||||||
return lookup(color.upper() + " CHESS " + piece.upper())
|
|
||||||
|
|
||||||
|
class Piece:
|
||||||
|
def __init__(self, color=None, piece=None):
|
||||||
|
self.color = color
|
||||||
|
self.piece = piece
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
if None in (self.piece, self.color):
|
||||||
|
return None
|
||||||
|
name = self.color.upper() + " CHESS " + self.piece.upper()
|
||||||
|
return lookup(name)
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -32,26 +40,26 @@ class Game:
|
||||||
r += rank + " |"
|
r += rank + " |"
|
||||||
for file in files:
|
for file in files:
|
||||||
piece = self.board[file+rank]
|
piece = self.board[file+rank]
|
||||||
if piece == None:
|
if piece.piece == None:
|
||||||
r += " "
|
r += " "
|
||||||
else:
|
else:
|
||||||
r += " " + unicode_repr(*piece)
|
r += " " + repr(piece)
|
||||||
r += "\n"
|
r += "\n"
|
||||||
r += " +" + "-"*16 + "\n"
|
r += " +" + "-"*16 + "\n"
|
||||||
r += " "*4 + " ".join(list(files))
|
r += " "*4 + " ".join(list(files))
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def make_board(self):
|
def make_board(self):
|
||||||
self.board = dict((sq, None) for sq in squares)
|
self.board = dict((sq, Piece()) for sq in squares)
|
||||||
# Add pieces
|
# Add pieces
|
||||||
for piece, positions in init_positions.items():
|
for piece, positions in init_positions.items():
|
||||||
for sq in positions:
|
for sq in positions:
|
||||||
self.board[sq] = piece
|
self.board[sq].piece = piece
|
||||||
# Add colors
|
# Add colors
|
||||||
for sq in cross(files, "12"):
|
for sq in cross(files, "12"):
|
||||||
self.board[sq] = ("white", self.board[sq])
|
self.board[sq].color = "white"
|
||||||
for sq in cross(files, "78"):
|
for sq in cross(files, "78"):
|
||||||
self.board[sq] = ("black", self.board[sq])
|
self.board[sq].color = "black"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue