Skip to content

Commit

Permalink
Refactored isStalemate()
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Sep 28, 2024
1 parent d86d9a6 commit bf634aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Variant/AbstractBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,20 @@ public function isMate(): bool

public function isStalemate(): bool
{
return $this->isTrapped() && !$this->isCheck();
if (!$this->piece($this->turn, Piece::K)->attacking()) {
$moveSqs = [];
foreach ($this->pieces($this->turn) as $piece) {
if (!$piece->isPinned()) {
$moveSqs = [
...$moveSqs,
...$piece->moveSqs(),
];
}
}
return $moveSqs === [];
}

return false;
}

public function isFivefoldRepetition(): bool
Expand Down
13 changes: 13 additions & 0 deletions src/Variant/Losing/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,19 @@ public function play(string $color, string $pgn): bool
return false;
}

public function isStalemate(): bool
{
$moveSqs = [];
foreach ($this->pieces($this->turn) as $piece) {
$moveSqs = [
...$moveSqs,
...$piece->moveSqs(),
];
}

return $moveSqs === [];
}

public function doesWin(): bool
{
return count($this->pieces($this->turn)) === 0 || $this->isStalemate();
Expand Down

0 comments on commit bf634aa

Please sign in to comment.