Skip to content

Commit 6a34d5e

Browse files
committed
Add SquareSet.from_square().
1 parent 3ed4800 commit 6a34d5e

3 files changed

Lines changed: 16 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ Minor new features:
2727

2828
* Allow `chess.Board.set_piece_at(square, None)`.
2929

30+
* Added `chess.SquareSet.from_square(square)`.
31+
3032
New in v0.13.0
3133
--------------
3234

chess/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,3 +4097,13 @@ def __str__(self):
40974097

40984098
def __hash__(self):
40994099
return hash(self.mask)
4100+
4101+
@classmethod
4102+
def from_square(cls, square):
4103+
"""
4104+
Creates a SquareSet from a single square.
4105+
4106+
>>> chess.SquareSet.from_square(chess.A1) == chess.BB_A1:
4107+
True
4108+
"""
4109+
return cls(BB_SQUARES[square])

test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,10 @@ def test_mutable_set_operations(self):
12531253
self.assertEqual(squares.pop(), chess.C7)
12541254
self.assertEqual(squares, chess.BB_VOID)
12551255

1256+
def test_from_square(self):
1257+
self.assertEqual(chess.SquareSet.from_square(chess.H5), chess.BB_H5)
1258+
self.assertEqual(chess.SquareSet.from_square(chess.C2), chess.BB_C2)
1259+
12561260

12571261
class PolyglotTestCase(unittest.TestCase):
12581262

0 commit comments

Comments
 (0)