-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Edited the README, made the piece's player property writable, fixed a…
… bug with cancelling the turn changed event, split the checkmate result to checkmate and no kings left, and added a duck chess variant.
- Loading branch information
Showing
27 changed files
with
264 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Iterable | ||
|
||
from chessmaker.chess.base import Player | ||
from chessmaker.chess.base.move_option import MoveOption | ||
from chessmaker.chess.base.piece import Piece, BeforeGetMoveOptionsEvent | ||
from chessmaker.events import EventPriority | ||
|
||
|
||
class Duck(Piece): | ||
def __init__(self, player: Player, movable: bool = False): | ||
super().__init__(player) | ||
self.movable = movable | ||
|
||
@classmethod | ||
@property | ||
def name(cls): | ||
return "Duck" | ||
|
||
def on_join_board(self): | ||
self.board.subscribe(BeforeGetMoveOptionsEvent, self._on_before_get_move_options, EventPriority.HIGH) | ||
|
||
def _on_before_get_move_options(self, event: BeforeGetMoveOptionsEvent): | ||
if self.movable: | ||
if event.piece is not self: | ||
event.set_move_options([]) | ||
|
||
def _get_move_options(self) -> Iterable[MoveOption]: | ||
if self.movable: | ||
for square in self.board: | ||
if square.piece is None and square.position != self.position: | ||
yield MoveOption(square.position) | ||
|
||
def clone(self): | ||
return Duck(self.player, movable=self.movable) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from chessmaker.chess.base import Board | ||
from chessmaker.chess.pieces import King | ||
|
||
|
||
def no_kings(board: Board) -> str | None: | ||
for player in board.players: | ||
if not [piece for piece in board.get_player_pieces(player) if isinstance(piece, King)]: | ||
return f"No kings left - {player.name} loses" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from itertools import chain | ||
|
||
from chessmaker.chess.base import Board, as_rule, BeforeTurnChangeEvent | ||
from chessmaker.chess.pieces.duck import Duck | ||
from chessmaker.events import EventPriority | ||
|
||
|
||
def on_before_turn_change(event: BeforeTurnChangeEvent): | ||
current_player = event.board.current_player | ||
if current_player != event.next_player: | ||
duck: Duck = [piece for piece in event.board.get_pieces() if isinstance(piece, Duck)][0] | ||
if duck.movable: | ||
duck.movable = False | ||
duck.player = event.next_player | ||
else: | ||
event.board.turn_iterator = chain([event.next_player], event.board.turn_iterator) | ||
event.set_next_player(current_player) | ||
duck.movable = True | ||
|
||
|
||
def duck_chess(board: Board): | ||
board.subscribe(BeforeTurnChangeEvent, on_before_turn_change, EventPriority.HIGH) | ||
|
||
|
||
DuckChess = as_rule(duck_chess) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.