Skip to content

Commit

Permalink
Refactored Chess\Variant\Classical\Piece\K
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Dec 13, 2024
1 parent 8dc5aba commit 7243d17
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
17 changes: 0 additions & 17 deletions src/Variant/AbstractBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,23 +164,6 @@ protected function isLegal(array $move, array $pieces): bool
return false;
}

/**
* Returns true if the king can castle as per the castling ability.
*
* @param string $type
* @return bool
*/
public function castlingAbility(string $type): bool
{
if ($type === Castle::SHORT) {
$id = $this->turn === Color::W ? Piece::K : mb_strtolower(Piece::K);
} elseif ($type === Castle::LONG) {
$id = $this->turn === Color::W ? Piece::Q : mb_strtolower(Piece::Q);
}

return str_contains($this->castlingAbility, $id);
}

/**
* Removes an element from the history.
*
Expand Down
20 changes: 10 additions & 10 deletions src/Variant/AbstractPiece.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,23 +357,23 @@ public function capture(): void
}

/**
* Updates the castle property.
* Updates the castling ability.
*
* @return \Chess\Variant\AbstractPiece
*/
public function updateCastle(): AbstractPiece
{
if ($this->board->castlingAbility(Castle::SHORT) || $this->board->castlingAbility(Castle::LONG)) {
if ($this->id === Piece::K) {
$search = $this->board->turn === Color::W ? 'KQ' : 'kq';
if ($this->id === Piece::K) {
$search = $this->board->turn === Color::W ? 'KQ' : 'kq';
$this->board->castlingAbility = str_replace($search, '', $this->board->castlingAbility)
?: CastlingRule::NEITHER;
} elseif ($this->id === Piece::R) {
if ($this->type === RType::CASTLE_SHORT) {
$search = $this->board->turn === Color::W ? 'K' : 'k';
$this->board->castlingAbility = str_replace($search, '', $this->board->castlingAbility)
?: CastlingRule::NEITHER;
} elseif ($this->id === Piece::R) {
if ($this->type === RType::CASTLE_SHORT) {
$search = $this->board->turn === Color::W ? 'K' : 'k';
} elseif ($this->type === RType::CASTLE_LONG) {
$search = $this->board->turn === Color::W ? 'Q' : 'q';
}
} elseif ($this->type === RType::CASTLE_LONG) {
$search = $this->board->turn === Color::W ? 'Q' : 'q';
$this->board->castlingAbility = str_replace($search, '', $this->board->castlingAbility)
?: CastlingRule::NEITHER;
}
Expand Down
9 changes: 8 additions & 1 deletion src/Variant/Classical/Piece/K.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Chess\Variant\AbstractPiece;
use Chess\Variant\RType;
use Chess\Variant\Classical\PGN\AN\Castle;
use Chess\Variant\Classical\PGN\AN\Color;
use Chess\Variant\Classical\PGN\AN\Piece;
use Chess\Variant\Classical\PGN\AN\Square;

Expand Down Expand Up @@ -92,7 +93,13 @@ public function lineOfAttack(): array

public function sqCastle(string $type): ?string
{
if ($this->board->castlingAbility($type)) {
if ($type === Castle::SHORT) {
$id = $this->board->turn === Color::W ? Piece::K : mb_strtolower(Piece::K);
} else {
$id = $this->board->turn === Color::W ? Piece::Q : mb_strtolower(Piece::Q);
}

if (str_contains($this->board->castlingAbility, $id)) {
$rule = $this->board->castlingRule?->rule[$this->color][Piece::K][$type];
if (
$this->board->turn === $this->color &&
Expand Down

0 comments on commit 7243d17

Please sign in to comment.