Skip to content
This repository was archived by the owner on Jul 2, 2024. It is now read-only.

Commit ab3f91e

Browse files
committed
Updated str for pieces
Added unicode symbols for pieces string versions. Signed-off-by: Serhii Horodilov <sgorodil@gmail.com>
1 parent fda605e commit ab3f91e

File tree

3 files changed

+90
-44
lines changed

3 files changed

+90
-44
lines changed

src/chess/piece.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
"""
2+
Chess pieces implementations
3+
4+
"""
5+
16
import logging
27
from typing import Tuple
38

9+
from chess import symbols
410
from chess.func import within_board
511

612
logger = logging.getLogger(__name__)
@@ -9,17 +15,13 @@
915
class Piece:
1016
"""Chess piece model
1117
12-
:ivar name: the name of a piece (e.g. "king", "pawn")
13-
:type name: str
1418
:ivar position: the position on a chess board
1519
:type position: tuple
1620
:ivar is_white: a flag indicating if a chess piece is white
1721
:type is_white: bool
1822
1923
"""
2024

21-
name: str = "piece"
22-
2325
def __init__(self,
2426
is_white: bool = True,
2527
position: Tuple[int, int] = (0, 0)) -> None:
@@ -40,13 +42,6 @@ def __repr__(self) -> str:
4042

4143
return f"{self.__class__.__name__}({self.is_white}, {self.position})"
4244

43-
def __str__(self) -> str:
44-
"""Return a string version of an instance"""
45-
46-
color = "white" if self.is_white else "black"
47-
48-
return f"{color} {self.name} at {self.position}"
49-
5045
def swap_color(self) -> None:
5146
"""Change the piece color to the opposite one"""
5247

@@ -96,7 +91,8 @@ def get_delta(self, position: Tuple[int, int]) -> Tuple[int, int]:
9691

9792

9893
class King(Piece): # pylint: disable=C0115
99-
name = "king"
94+
def __str__(self) -> str:
95+
return symbols.WHITE_KING if self.is_white else symbols.BLACK_KING
10096

10197
def can_move(self, position: Tuple[int, int]) -> bool:
10298
delta_x, delta_y = self.get_delta(position)
@@ -107,7 +103,8 @@ def can_move(self, position: Tuple[int, int]) -> bool:
107103

108104

109105
class Queen(Piece): # pylint: disable=C0115
110-
name = "queen"
106+
def __str__(self) -> str:
107+
return symbols.WHITE_QUEEN if self.is_white else symbols.BLACK_QUEEN
111108

112109
def can_move(self, position: Tuple[int, int]) -> bool:
113110
delta_x, delta_y = self.get_delta(position)
@@ -118,7 +115,9 @@ def can_move(self, position: Tuple[int, int]) -> bool:
118115

119116

120117
class Bishop(Piece): # pylint: disable=C0115
121-
name = "bishop"
118+
119+
def __str__(self) -> str:
120+
return symbols.WHITE_BISHOP if self.is_white else symbols.BLACK_BISHOP
122121

123122
def can_move(self, position: Tuple[int, int]) -> bool:
124123
delta_x, delta_y = self.get_delta(position)
@@ -129,7 +128,9 @@ def can_move(self, position: Tuple[int, int]) -> bool:
129128

130129

131130
class Knight(Piece): # pylint: disable=C0115
132-
name = "knight"
131+
132+
def __str__(self) -> str:
133+
return symbols.WHITE_KNIGHT if self.is_white else symbols.BLACK_KNIGHT
133134

134135
def can_move(self, position: Tuple[int, int]) -> bool:
135136
delta_x, delta_y = self.get_delta(position)
@@ -140,7 +141,9 @@ def can_move(self, position: Tuple[int, int]) -> bool:
140141

141142

142143
class Rook(Piece): # pylint: disable=C0115
143-
name = "rook"
144+
145+
def __str__(self) -> str:
146+
return symbols.WHITE_ROOK if self.is_white else symbols.BLACK_ROOK
144147

145148
def can_move(self, position: Tuple[int, int]) -> bool:
146149
delta_x, delta_y = self.get_delta(position)
@@ -151,7 +154,9 @@ def can_move(self, position: Tuple[int, int]) -> bool:
151154

152155

153156
class Pawn(Piece): # pylint: disable=C0115
154-
name = "pawn"
157+
158+
def __str__(self) -> str:
159+
return symbols.WHITE_PAWN if self.is_white else symbols.BLACK_PAWN
155160

156161
def can_move(self, position: Tuple[int, int]) -> bool:
157162
delta_x, delta_y = self.get_delta(position)

src/chess/symbols.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Chess pieces unicode symbols
3+
"""
4+
5+
WHITE_KING = chr(0x2654)
6+
BLACK_KING = chr(0x265A)
7+
WHITE_QUEEN = chr(0x2655)
8+
BLACK_QUEEN = chr(0x265B)
9+
WHITE_ROOK = chr(0x2656)
10+
BLACK_ROOK = chr(0x265C)
11+
WHITE_BISHOP = chr(0x2657)
12+
BLACK_BISHOP = chr(0x265D)
13+
WHITE_KNIGHT = chr(0x2658)
14+
BLACK_KNIGHT = chr(0x265E)
15+
WHITE_PAWN = chr(0x2659)
16+
BLACK_PAWN = chr(0x265F)

tests/chess/test_pieces.py

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
import sys
22
import unittest
33

4-
from chess.piece import Bishop, King, Knight, Pawn, Piece, Queen, Rook
4+
from chess import piece
5+
from chess import symbols
56

67

78
class TestPieceModel(unittest.TestCase):
89
"""Abstract chess model test case"""
910

1011
def setUp(self) -> None:
11-
self.instance = Piece()
12+
self.instance = piece.Piece()
1213

1314
def test_default_initializer(self):
1415
self.assertIs(True, self.instance.is_white)
1516
self.assertTupleEqual((0, 0), self.instance.position)
1617

1718
def test_initializer(self):
18-
instance = Piece(False, (7, 6))
19+
instance = piece.Piece(False, (7, 6))
1920
self.assertIs(False, instance.is_white)
2021
self.assertTupleEqual((7, 6), instance.position)
2122

@@ -59,11 +60,15 @@ def test_get_delta(self):
5960

6061
class TestKingPiece(unittest.TestCase):
6162
def setUp(self) -> None:
62-
self.instance = King(True)
63-
self.instance.set_position((5, 5))
63+
self.instance = piece.King(position=(5, 5))
6464

65-
def test_str(self):
66-
self.assertEqual("white king at (5, 5)", str(self.instance))
65+
def test_str_white(self):
66+
self.instance.is_white = True
67+
self.assertEqual(symbols.WHITE_KING, str(self.instance))
68+
69+
def test_str_black(self):
70+
self.instance.is_white = False
71+
self.assertEqual(symbols.BLACK_KING, str(self.instance))
6772

6873
def test_can_move(self):
6974
positions = ((5, 6), (6, 6), (6, 5), (6, 4),
@@ -82,11 +87,15 @@ def test_cannot_move(self):
8287

8388
class TestQueenPiece(unittest.TestCase):
8489
def setUp(self) -> None:
85-
self.instance = Queen(False)
86-
self.instance.set_position((2, 3))
90+
self.instance = piece.Queen(position=(2, 3))
8791

88-
def test_str(self):
89-
self.assertEqual("black queen at (2, 3)", str(self.instance))
92+
def test_str_white(self):
93+
self.instance.is_white = True
94+
self.assertEqual(symbols.WHITE_QUEEN, str(self.instance))
95+
96+
def test_str_black(self):
97+
self.instance.is_white = False
98+
self.assertEqual(symbols.BLACK_QUEEN, str(self.instance))
9099

91100
def test_can_move(self):
92101
positions = ((2, 5), (4, 5), (7, 3), (3, 2),
@@ -104,11 +113,15 @@ def test_cannot_move(self):
104113

105114
class TestBishopPiece(unittest.TestCase):
106115
def setUp(self) -> None:
107-
self.instance = Bishop(True)
108-
self.instance.set_position((5, 5))
116+
self.instance = piece.Bishop(position=(5, 5))
117+
118+
def test_str_white(self):
119+
self.instance.is_white = True
120+
self.assertEqual(symbols.WHITE_BISHOP, str(self.instance))
109121

110-
def test_str(self):
111-
self.assertEqual("white bishop at (5, 5)", str(self.instance))
122+
def test_str_black(self):
123+
self.instance.is_white = False
124+
self.assertEqual(symbols.BLACK_BISHOP, str(self.instance))
112125

113126
def test_can_move(self):
114127
positions = ((6, 6), (7, 3), (2, 2), (3, 7))
@@ -125,11 +138,15 @@ def test_cannot_move(self):
125138

126139
class TestKnightPiece(unittest.TestCase):
127140
def setUp(self) -> None:
128-
self.instance = Knight(False)
129-
self.instance.set_position((3, 4))
141+
self.instance = piece.Knight(position=(3, 4))
130142

131-
def test_str(self):
132-
self.assertEqual("black knight at (3, 4)", str(self.instance))
143+
def test_str_white(self):
144+
self.instance.is_white = True
145+
self.assertEqual(symbols.WHITE_KNIGHT, str(self.instance))
146+
147+
def test_str_black(self):
148+
self.instance.is_white = False
149+
self.assertEqual(symbols.BLACK_KNIGHT, str(self.instance))
133150

134151
def test_can_move(self):
135152
positions = ((4, 6), (5, 5), (5, 3), (4, 2),
@@ -148,11 +165,15 @@ def test_cannot_move(self):
148165

149166
class TestRookPiece(unittest.TestCase):
150167
def setUp(self) -> None:
151-
self.instance = Rook(True)
152-
self.instance.set_position((5, 2))
168+
self.instance = piece.Rook(position=(5, 2))
153169

154-
def test_str(self):
155-
self.assertEqual("white rook at (5, 2)", str(self.instance))
170+
def test_str_white(self):
171+
self.instance.is_white = True
172+
self.assertEqual(symbols.WHITE_ROOK, str(self.instance))
173+
174+
def test_str_black(self):
175+
self.instance.is_white = False
176+
self.assertEqual(symbols.BLACK_ROOK, str(self.instance))
156177

157178
def test_can_move(self):
158179
positions = ((5, 4), (7, 2), (5, 1), (2, 2))
@@ -169,11 +190,15 @@ def test_cannot_move(self):
169190

170191
class TestPawnPiece(unittest.TestCase):
171192
def setUp(self) -> None:
172-
self.instance = Pawn(False)
173-
self.instance.set_position((6, 5))
193+
self.instance = piece.Pawn(position=(6, 5))
194+
195+
def test_str_white(self):
196+
self.instance.is_white = True
197+
self.assertEqual(symbols.WHITE_PAWN, str(self.instance))
174198

175-
def test_str(self):
176-
self.assertEqual("black pawn at (6, 5)", str(self.instance))
199+
def test_str_black(self):
200+
self.instance.is_white = False
201+
self.assertEqual(symbols.BLACK_PAWN, str(self.instance))
177202

178203
def test_can_move(self):
179204
self.instance.is_white = False

0 commit comments

Comments
 (0)