Skip to content

Commit 1f32481

Browse files
committed
ByColor
1 parent 415ff58 commit 1f32481

3 files changed

Lines changed: 18 additions & 7 deletions

File tree

‎chess/__init__.py‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import itertools
2323
import typing
2424

25-
from typing import ClassVar, Literal, Self, SupportsInt, TypeAlias
25+
from typing import ClassVar, Literal, Protocol, Self, SupportsInt, TypeAlias, TypeVar
2626
from collections.abc import Callable, Hashable, Iterable, Iterator, Mapping
2727

2828

@@ -34,7 +34,16 @@
3434
BLACK: Color = False
3535
COLORS: list[Color] = [WHITE, BLACK]
3636
ColorName = Literal["white", "black"]
37-
COLOR_NAMES: list[ColorName] = ["black", "white"]
37+
38+
_T = TypeVar("_T")
39+
40+
class ByColor(Protocol[_T]):
41+
"""A pair of values indexed by :data:`~chess.Color`."""
42+
def __getitem__(self, color: Color, /) -> _T: ...
43+
def __setitem__(self, color: Color, value: _T, /) -> None: ...
44+
def __iter__(self) -> Iterator[_T]: ...
45+
46+
COLOR_NAMES: ByColor[ColorName] = typing.cast(list[ColorName], ["black", "white"])
3847

3948
PieceType: TypeAlias = int
4049
PAWN: PieceType = 1
@@ -679,7 +688,7 @@ def _step_attacks(square: Square, deltas: Iterable[int]) -> Bitboard:
679688

680689
BB_KNIGHT_ATTACKS: list[Bitboard] = [_step_attacks(sq, [17, 15, 10, 6, -17, -15, -10, -6]) for sq in SQUARES]
681690
BB_KING_ATTACKS: list[Bitboard] = [_step_attacks(sq, [9, 8, 7, 1, -9, -8, -7, -1]) for sq in SQUARES]
682-
BB_PAWN_ATTACKS: list[list[Bitboard]] = [[_step_attacks(sq, deltas) for sq in SQUARES] for deltas in [[-7, -9], [7, 9]]]
691+
BB_PAWN_ATTACKS: ByColor[list[Bitboard]] = [[_step_attacks(sq, deltas) for sq in SQUARES] for deltas in [[-7, -9], [7, 9]]]
683692

684693

685694
def _edges(square: Square) -> Bitboard:
@@ -957,7 +966,7 @@ class BaseBoard:
957966
queens: Bitboard
958967
kings: Bitboard
959968
promoted: Bitboard
960-
occupied_co: list[Bitboard]
969+
occupied_co: ByColor[Bitboard]
961970
occupied: Bitboard
962971

963972
def __init__(self, board_fen: str | None = STARTING_BOARD_FEN) -> None:

‎chess/variant.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ class ThreeCheckBoard(chess.Board):
755755
tbz_magic: ClassVar[bytes | None] = None
756756

757757
def __init__(self, fen: str | None = starting_fen, *, chess960: bool = False) -> None:
758-
self.remaining_checks: list[int] = [3, 3]
758+
self.remaining_checks: chess.ByColor[int] = [3, 3]
759759
self._three_check_stack: list[_ThreeCheckBoardState] = []
760760
super().__init__(fen, chess960=chess960)
761761

@@ -886,7 +886,7 @@ def apply() -> None:
886886

887887
def copy(self, *, stack: bool | int = True) -> Self:
888888
board = super().copy(stack=stack)
889-
board.remaining_checks = self.remaining_checks.copy()
889+
board.remaining_checks = list(self.remaining_checks)
890890
if stack:
891891
stack = len(self.move_stack) if stack is True else stack
892892
board._three_check_stack = self._three_check_stack[-stack:]
@@ -972,7 +972,7 @@ class CrazyhouseBoard(chess.Board):
972972
tbz_magic: ClassVar[bytes | None] = None
973973

974974
def __init__(self, fen: str | None = starting_fen, *, chess960: bool = False) -> None:
975-
self.pockets: list[CrazyhousePocket] = [CrazyhousePocket(), CrazyhousePocket()]
975+
self.pockets: chess.ByColor[CrazyhousePocket] = [CrazyhousePocket(), CrazyhousePocket()]
976976
self._crazyhouse_stack: list[_CrazyhouseBoardState] = []
977977
super().__init__(fen, chess960=chess960)
978978

‎docs/core.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Constants for the side to move or the color of a piece.
1616

1717
You can get the opposite *color* using ``not color``.
1818

19+
.. autoclass:: chess.ByColor
20+
1921
Piece types
2022
-----------
2123

0 commit comments

Comments
 (0)