en_pyssant.rules module¶
A module implementing all the rules of chess.
-
attacked
(position, side, square)[source]¶ Is square in check by side?
Note
Pawns that can be struck through an en passant move are also identified as being in check by this function.
Note
Just because square might be in check, it does NOT mean that the square can be legally captured. The piece attacking square might be pinned, for instance. Or more mundanely, the square is held by a piece that is friendly to the attacker.
Parameters: - position (
Position
) – Chess position. - side (
Side
) – Attacking side. - square (
Square
) – Square that is under attack or not.
Return type: Returns: Whether square is in check by side.
- position (
-
do_move
(position, move, force=False)[source]¶ Perform move on position and return the resulting position.
move need only contain the origin, destination and (possibly) promotion fields. It can also be a SAN string.
If the promotion field is not specified,
do_move()
will default to queen promotion.If force is specified, move needs all the correct fields filled out. force disables any move verification and will lead to unspecified behaviour if an invalid move is provided. Typically you might obtain valid moves from
moves()
.>>> do_move(Position(), Move(origin='a2', destination='a4')) rnbqkbnr/pppppppp/8/8/P7/8/1PPPPPPP/RNBQKBNR b KQkq a3 0 1
Parameters: Return type: Position
Returns: The position that results from performing move.
Raises: ValueError – move is invalid.
-
do_move_with_history
(position, move, force=False)[source]¶ Exactly as
do_move()
, except instead of returning the resulting position, return a tuple of the resulting position and the history record to reach that position.Parameters: Return type: Tuple
[Position
,HistoryRecord
]Returns: The position that results from performing move, and the corresponding history record.
Raises: ValueError – move is invalid.
-
is_check
(position, side=None)[source]¶ Return whether side’s king is in check.
If side is not specified, it defaults to the side to play.
Parameters: - position (
Position
) – Chess position. - side (
Optional
[Side
]) – Side to check for check.
Return type: Returns: Whether side’s king is in check.
- position (
-
is_checkmate
(position)[source]¶ Return whether position is in checkmate.
Parameters: position ( Position
) – Chess position.Return type: bool
Returns: Whether the position is in checkmate.
-
is_draw
(position, history=None)[source]¶ Return whether position is in draw.
Parameters: Return type: Returns: Whether and how the position is in draw.
-
is_fifty_move
(position)[source]¶ There have been fifty moves without a pawn advance or without a capture.
Parameters: position ( Position
) – Chess position.Return type: bool
Returns: Whether the fifty-move rule has been initiated.
-
is_gameover
(position, history=None)[source]¶ Return whether position is game-over.
Parameters: Return type: Returns: Whether and how the position is game-over.
-
is_insufficient_material
(position)[source]¶ There are not enough pieces left on the board for either player to win.
That is:
- K v. k
- K + N v. k
- K + B v. k + b where all bishops are on the same colour, any number of bishops on both sides
There are more situations where this rule might apply, but those situations are not accounted for here.
Parameters: position ( Position
) – Chess position.Return type: bool
Returns: Whether there is insufficient material.
-
is_stale
(position)[source]¶ Return whether there are no possible moves left.
Parameters: position ( Position
) – Chess position.Return type: bool
Returns: Whether the position is stale.
-
is_stalemate
(position)[source]¶ Return whether position is in stalemate.
Parameters: position ( Position
) – Chess position.Return type: bool
Returns: Whether the position is in stalemate.
-
is_threefold_repetition
(history=None)[source]¶ The same position has been played three times.
If history is not specified, return
False
.Parameters: history ( Optional
[Sequence
[HistoryRecord
]]) – History of plays.Return type: bool
Returns: Whether the threefold repetition rule has been initiated.
-
loser
(position)[source]¶ Return the loser of position.
If there is no loser, return
None
.Parameters: position ( Position
) – Chess position.Return type: Optional
[Side
]Returns: The loser.
-
moves
(position)[source]¶ Yield all legal moves for position. This includes:
- Regular moves and captures.
- En passant captures.
- Pawn promotions (each promotion yields its own move).
- Castling.
Moves that result in check are not legal.
The rules of chess can be found here:
https://en.wikipedia.org/wiki/Rules_of_chess#Gameplay
It is currently not possible to enable, disable or change certain rules.
Important
position is assumed to be correct. If the position is impossible, the behaviour of this function is undefined.
Parameters: position ( Position
) – Chess position.Return type: Iterator
[Move
]Returns: All legal moves.
-
moves_single
(position, square)[source]¶ Yield all legal moves for position from square. See
moves()
for a full description of what constitutes a legal move.Parameters: - position (
Position
) – Chess position. - square (
str
) – Square from which to calculate moves.
Return type: Iterator
[Move
]Returns: All legal moves from square.
- position (