Skip to content

Commit

Permalink
Optimized isPinned()
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Sep 27, 2024
1 parent c2c830f commit eb8575c
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/Variant/AbstractPiece.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,13 @@ public function isMovable(): bool

public function isPinned(): bool
{
$king = $this->board->piece($this->color, Piece::K);
$clone = $this->board->clone();
$clone->detach($clone->pieceBySq($this->sq));
$clone->refresh();
$newKing = $clone->piece($this->color, Piece::K);
$diffPieces = $this->board->diffPieces($king->attacking(), $newKing->attacking());
foreach ($diffPieces as $diffPiece) {
if ($diffPiece->color !== $king->color) {
return true;
}
}

return false;
$before = $this->board->piece($this->color, Piece::K)->attacking();
$this->board->detach($this);
$this->board->refresh();
$after = $this->board->piece($this->color, Piece::K)->attacking();
$this->board->attach($this);
$this->board->refresh();

return !empty($this->board->diffPieces($before, $after));
}
}

0 comments on commit eb8575c

Please sign in to comment.