Skip to content

Commit

Permalink
Issue/728 refactor the chess eval discovered check eval class in term…
Browse files Browse the repository at this point in the history
…s of lines (#732)

* Refactored Chess\Eval\DiscoveredCheckEval

* Deleted diffPieces()
  • Loading branch information
programarivm authored Jan 29, 2025
1 parent 6832e49 commit 03710ee
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
18 changes: 8 additions & 10 deletions src/Eval/DiscoveredCheckEval.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Chess\Phrase\ColorPhrase;
use Chess\Phrase\PiecePhrase;
use Chess\Variant\AbstractBoard;
use Chess\Variant\AbstractLinePiece;
use Chess\Variant\Classical\PGN\Piece;

/**
Expand Down Expand Up @@ -48,18 +49,15 @@ public function __construct(AbstractBoard $board)

foreach ($this->board->pieces() as $piece) {
if ($piece->id !== Piece::K) {
$before = $this->board->piece($piece->oppColor(), Piece::K)->attacking();
$this->board->detach($piece);
$this->board->refresh();
$after = $this->board->piece($piece->oppColor(), Piece::K)->attacking();
foreach ($this->board->diffPieces($before, $after) as $diffPiece) {
if ($diffPiece->color === $piece->color) {
$this->result[$piece->color] += self::$value[$piece->id];
$this->toElaborate[] = $piece;
foreach ($piece->defending() as $defending) {
if (is_a($defending, AbstractLinePiece::class)) {
$king = $this->board->piece($piece->oppColor(), Piece::K);
if ($piece->isBetween($king, $defending) && $piece->isEmpty($piece->line($king->sq))) {
$this->result[$piece->color] += self::$value[$piece->id];
$this->toElaborate[] = $piece;
}
}
}
$this->board->attach($piece);
$this->board->refresh();
}
}
}
Expand Down
14 changes: 0 additions & 14 deletions src/Variant/AbstractBoard.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,20 +724,6 @@ public function toFen(): string
return "{$filtered} {$this->turn} {$this->castlingAbility} {$this->enPassant()}";
}

/**
* Returns the difference of two arrays of pieces.
*
* @param array $array1
* @param array $array2
* @return array
*/
public function diffPieces(array $array1, array $array2): array
{
return array_udiff($array2, $array1, function ($b, $a) {
return $a->sq <=> $b->sq;
});
}

/**
* Returns true if the game results in a draw.
*
Expand Down

0 comments on commit 03710ee

Please sign in to comment.