Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ install: ## install vendors
docker compose exec php composer install

stan: ## static analysis via phpstan
docker compose exec php php vendor/bin/phpstan analyse -v
docker compose exec php php vendor/bin/phpstan analyse -v --memory-limit=-1

start: ## start docker image
docker compose up -d
Expand Down
9 changes: 7 additions & 2 deletions docs/board.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@ foreach ($board as $offset => $piece) {
}
```

The only method that can be directly used is `$board->reverse()`, useful to toggle sides (e.g. to see
the board from the black point of view, and then from the white again).
The following methods can be used directly:

* `reverse()`: toggle sides (e.g. to see the board from the black point of view, and then from the white again)
* `isAttacked(string $color, int $square): bool`: check if a square is attacked by any piece of the given color
(e.g. `$chess->board->isAttacked(Piece::WHITE, Board::SQUARES['e4'])`)
* `hasInsufficientMaterial(): bool`: check if material is insufficient to continue the game.
Same as `Chess::insufficientMaterial()`

The following static methods can be used if you need to deal
with [SAN](https://en.wikipedia.org/wiki/Algebraic_notation_(chess)):
Expand Down
107 changes: 107 additions & 0 deletions src/Board.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,113 @@ public function isReversed(): bool
return $this->reversed;
}

public function isAttacked(string $color, int $square): bool
{
for ($i = self::SQUARES['a8']; $i <= self::SQUARES['h1']; ++$i) {
if (($i & 0x88) > 0) {
$i += 7;
continue;
}
$piece = $this->squares[$i] ?? null;
// check empty square and color
if (null === $piece || $piece->getColor() !== $color) {
continue;
}

$difference = $i - $square;
$index = $difference + 119;

if ((self::ATTACKS[$index] & (1 << Piece::SHIFTS[$piece->getType()])) > 0) {
if ($piece->isPawn()) {
if ($difference > 0) {
if ($piece->getColor() === Piece::WHITE) {
return true;
}
} elseif ($piece->getColor() === Piece::BLACK) {
return true;
}
continue;
}

if ($piece->isKnight() || $piece->isKing()) {
return true;
}

$offset = self::RAYS[$index];
$j = $i + $offset;
$blocked = false;
while ($j !== $square) {
if (($this->squares[$j] ?? null) !== null) {
$blocked = true;
break;
}
$j += $offset;
}

if (!$blocked) {
return true;
}
}
}

return false;
}

public function hasInsufficientMaterial(): bool
{
$pieces = [
Piece::PAWN => 0,
Piece::KNIGHT => 0,
Piece::BISHOP => 0,
Piece::ROOK => 0,
Piece::QUEEN => 0,
Piece::KING => 0,
];
$bishops = [];
$numPieces = 0;
$sqColor = 0;

for ($i = self::SQUARES['a8']; $i <= self::SQUARES['h1']; ++$i) {
$sqColor = ($sqColor + 1) % 2;
if (($i & 0x88) > 0) {
$i += 7;
continue;
}

$piece = $this->squares[$i] ?? null;
if ($piece !== null) {
++$pieces[$piece->getType()];
if ($piece->isBishop()) {
$bishops[] = $sqColor;
}
++$numPieces;
}
}

// k vs k
if ($numPieces === 2) {
return true;
}

// k vs kn / k vs kb
if ($numPieces === 3 && ($pieces[Piece::BISHOP] === 1 || $pieces[Piece::KNIGHT] === 1)) {
return true;
}

// k(b){0,} vs k(b){0,} , because maybe you are a programmer we talk in regex (preg) :-p
if ($numPieces === $pieces[Piece::BISHOP] + 2) {
$sum = 0;
foreach ($bishops as $bishop) {
$sum += $bishop;
}
if ($sum === 0 || $sum === \count($bishops)) {
return true;
}
}

return false;
}

public static function rank(int $i): int
{
return $i >> 4;
Expand Down
115 changes: 9 additions & 106 deletions src/Chess.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,9 @@ protected function generateMoves(?int $square = null, bool $legal = true): array
if (
$this->board[$castlingFrom + 1] === null &&
$this->board[$castlingTo] === null &&
!$this->attacked($them, $this->kings[$us]) &&
!$this->attacked($them, $castlingFrom + 1) &&
!$this->attacked($them, $castlingTo)
!$this->board->isAttacked($them, $this->kings[$us]) &&
!$this->board->isAttacked($them, $castlingFrom + 1) &&
!$this->board->isAttacked($them, $castlingTo)
) {
self::addMove($us, $this->board, $moves, $this->kings[$us], $castlingTo, Move::BITS['KSIDE_CASTLE']);
}
Expand All @@ -543,9 +543,9 @@ protected function generateMoves(?int $square = null, bool $legal = true): array
$this->board[$castlingFrom - 1] === null &&
$this->board[$castlingFrom - 2] === null && // $castlingTo
$this->board[$castlingFrom - 3] === null && // col "b", next to rock
!$this->attacked($them, $this->kings[$us]) &&
!$this->attacked($them, $castlingFrom - 1) &&
!$this->attacked($them, $castlingTo)
!$this->board->isAttacked($them, $this->kings[$us]) &&
!$this->board->isAttacked($them, $castlingFrom - 1) &&
!$this->board->isAttacked($them, $castlingTo)
) {
self::addMove($us, $this->board, $moves, $this->kings[$us], $castlingTo, Move::BITS['QSIDE_CASTLE']);
}
Expand Down Expand Up @@ -638,59 +638,12 @@ public function moves(?int $square = null): array

protected function attacked(string $color, int $square): bool
{
for ($i = Board::SQUARES['a8']; $i <= Board::SQUARES['h1']; ++$i) {
if (($i & 0x88) > 0) {
$i += 7;
continue;
}
$piece = $this->board[$i];
// check empty square and color
if (null === $piece || $piece->getColor() !== $color) {
continue;
}

$difference = $i - $square;
$index = $difference + 119;

if ((Board::ATTACKS[$index] & (1 << Piece::SHIFTS[$piece->getType()])) > 0) {
if ($piece->isPawn()) {
if ($difference > 0) {
if ($piece->getColor() === Piece::WHITE) {
return true;
}
} elseif ($piece->getColor() === Piece::BLACK) {
return true;
}
continue;
}

if ($piece->isKnight() || $piece->isKing()) {
return true;
}

$offset = Board::RAYS[$index];
$j = $i + $offset;
$blocked = false;
while ($j !== $square) {
if ($this->board[$j] !== null) {
$blocked = true;
break;
}
$j += $offset;
}

if (!$blocked) {
return true;
}
}
}

return false;
return $this->board->isAttacked($color, $square);
}

protected function kingAttacked(string $color): bool
{
return null !== $this->kings[$color] && $this->attacked(self::swapColor($color), $this->kings[$color]);
return null !== $this->kings[$color] && $this->board->isAttacked(self::swapColor($color), $this->kings[$color]);
}

public function inCheck(): bool
Expand All @@ -710,57 +663,7 @@ public function inStalemate(): bool

public function insufficientMaterial(): bool
{
$pieces = [
Piece::PAWN => 0,
Piece::KNIGHT => 0,
Piece::BISHOP => 0,
Piece::ROOK => 0,
Piece::QUEEN => 0,
Piece::KING => 0,
];
$bishops = [];
$numPieces = 0;
$sqColor = 0;

for ($i = Board::SQUARES['a8']; $i <= Board::SQUARES['h1']; ++$i) {
$sqColor = ($sqColor + 1) % 2;
if (($i & 0x88) > 0) {
$i += 7;
continue;
}

$piece = $this->board[$i];
if ($piece !== null) {
++$pieces[$piece->getType()];
if ($piece->isBishop()) {
$bishops[] = $sqColor;
}
++$numPieces;
}
}

// k vs k
if ($numPieces === 2) {
return true;
}

// k vs kn / k vs kb
if ($numPieces === 3 && ($pieces[Piece::BISHOP] === 1 || $pieces[Piece::KNIGHT] === 1)) {
return true;
}

// k(b){0,} vs k(b){0,} , because maybe you are a programmer we talk in regex (preg) :-p
if ($numPieces === $pieces[Piece::BISHOP] + 2) {
$sum = 0;
foreach ($bishops as $bishop) {
$sum += $bishop;
}
if ($sum === 0 || $sum === \count($bishops)) {
return true;
}
}

return false;
return $this->board->hasInsufficientMaterial();
}

/* TODO: while this function is fine for casual use, a better
Expand Down
Loading
Loading