Skip to content

Commit d783fa0

Browse files
authored
Merge pull request #909 from DonLarry/master
Adding flipped option to unicode representation
2 parents aa0a04f + 5c05487 commit d783fa0

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

chess/__init__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,16 +1221,18 @@ def __str__(self) -> str:
12211221

12221222
return "".join(builder)
12231223

1224-
def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_square: str = "⭘") -> str:
1224+
def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_square: str = "⭘", orientation:Color = WHITE) -> str:
12251225
"""
12261226
Returns a string representation of the board with Unicode pieces.
12271227
Useful for pretty-printing to a terminal.
12281228
12291229
:param invert_color: Invert color of the Unicode pieces.
12301230
:param borders: Show borders and a coordinate margin.
12311231
"""
1232+
board = self.copy()
12321233
builder = []
1233-
for rank_index in range(7, -1, -1):
1234+
indexes = range(7, -1, -1) if orientation else range(8)
1235+
for rank_index in indexes:
12341236
if borders:
12351237
builder.append(" ")
12361238
builder.append("-" * 17)
@@ -1247,7 +1249,7 @@ def unicode(self, *, invert_color: bool = False, borders: bool = False, empty_sq
12471249
elif file_index > 0:
12481250
builder.append(" ")
12491251

1250-
piece = self.piece_at(square_index)
1252+
piece = board.piece_at(square_index)
12511253

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

1260-
if borders or rank_index > 0:
1262+
if borders or (rank_index > 0 and orientation) or (rank_index < 7 and not orientation):
12611263
builder.append("\n")
12621264

12631265
if borders:
12641266
builder.append(" ")
12651267
builder.append("-" * 17)
12661268
builder.append("\n")
1267-
builder.append(" a b c d e f g h")
1269+
letters = "a b c d e f g h" if orientation else "h g f e d c b a"
1270+
builder.append(" " + letters)
12681271

12691272
return "".join(builder)
12701273

0 commit comments

Comments
 (0)