Skip to content

Commit

Permalink
Merge pull request #909 from DonLarry/master
Browse files Browse the repository at this point in the history
Adding flipped option to unicode representation
  • Loading branch information
niklasf committed Jul 29, 2022
2 parents aa0a04f + 5c05487 commit d783fa0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions chess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1221,16 +1221,18 @@ def __str__(self) -> str:

return "".join(builder)

def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_square: str = "⭘") -> str:
def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_square: str = "⭘", orientation:Color = WHITE) -> str:
"""
Returns a string representation of the board with Unicode pieces.
Useful for pretty-printing to a terminal.
:param invert_color: Invert color of the Unicode pieces.
:param borders: Show borders and a coordinate margin.
"""
board = self.copy()
builder = []
for rank_index in range(7, -1, -1):
indexes = range(7, -1, -1) if orientation else range(8)
for rank_index in indexes:
if borders:
builder.append(" ")
builder.append("-" * 17)
Expand All @@ -1247,7 +1249,7 @@ def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_sq
elif file_index > 0:
builder.append(" ")

piece = self.piece_at(square_index)
piece = board.piece_at(square_index)

if piece:
builder.append(piece.unicode_symbol(invert_color=invert_color))
Expand All @@ -1257,14 +1259,15 @@ def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_sq
if borders:
builder.append("|")

if borders or rank_index > 0:
if borders or (rank_index > 0 and orientation) or (rank_index < 7 and not orientation):
builder.append("\n")

if borders:
builder.append(" ")
builder.append("-" * 17)
builder.append("\n")
builder.append(" a b c d e f g h")
letters = "a b c d e f g h" if orientation else "h g f e d c b a"
builder.append(" " + letters)

return "".join(builder)

Expand Down

0 comments on commit d783fa0

Please sign in to comment.