Skip to content

Commit

Permalink
Feature/checkability eval (#734)
Browse files Browse the repository at this point in the history
* Updated Chess\Eval\CheckabilityEval

* Updated tests
  • Loading branch information
programarivm authored Feb 2, 2025
1 parent 31673b8 commit 41344af
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 40 deletions.
30 changes: 4 additions & 26 deletions src/Eval/CheckabilityEval.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,16 @@ public function __construct(AbstractBoard $board)
"can be checked so it is vulnerable to forced moves",
];

$wKing = $this->board->piece(Color::W, Piece::K);
$bKing = $this->board->piece(Color::B, Piece::K);

if ($this->isCheckable($bKing)) {
$this->result[Color::W] = 1;
}

if ($this->isCheckable($wKing)) {
$this->result[Color::B] = 1;
}
}

/**
* Returns true if the king is checkable.
*
* @param \Chess\Variant\AbstractPiece $king
* @return bool
*/
private function isCheckable(AbstractPiece $king): bool
{
foreach ($this->board->pieces($king->oppColor()) as $piece) {
foreach ($this->board->pieces($this->board->turn) as $piece) {
foreach ($piece->moveSqs() as $sq) {
$clone = $this->board->clone();
$clone->turn = $king->oppColor();
if ($clone->playLan($king->oppColor(), "{$piece->sq}$sq")) {
if ($clone->playLan($this->board->turn, "{$piece->sq}$sq")) {
if ($clone->isCheck()) {
return true;
$this->result[$this->board->turn] = 1;
break 2;
}
}
}
}

return false;
}
}
17 changes: 9 additions & 8 deletions tests/unit/Eval/CheckabilityEvalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public function a1_checkable_w()
{
$expectedResult = [
'w' => 0,
'b' => 1,
'b' => 0,
];

$expectedExplanation = [
"White's king can be checked so it is vulnerable to forced moves.",
];

$board = FenToBoardFactory::create('1b5k/6pp/8/8/8/8/8/K7 w - -');
Expand Down Expand Up @@ -53,17 +52,18 @@ public function a1_checkable_b()
/**
* @test
*/
public function a1_and_h8_checkable_w()
public function h8_checkable_w()
{
$expectedResult = [
'w' => 1,
'b' => 1,
'b' => 0,
];

$expectedExplanation = [
"Black's king can be checked so it is vulnerable to forced moves.",
];

$board = FenToBoardFactory::create('1b5k/7p/8/8/8/8/8/K4R2 w - -');
$board = FenToBoardFactory::create('1b5k/7p/8/8/8/8/1K6/5R2 w - -');
$checkabilityEval = new CheckabilityEval($board);

$this->assertSame($expectedResult, $checkabilityEval->result);
Expand All @@ -73,17 +73,18 @@ public function a1_and_h8_checkable_w()
/**
* @test
*/
public function a1_and_h8_checkable_b()
public function b2_checkable_b()
{
$expectedResult = [
'w' => 1,
'w' => 0,
'b' => 1,
];

$expectedExplanation = [
"White's king can be checked so it is vulnerable to forced moves.",
];

$board = FenToBoardFactory::create('1b5k/7p/8/8/8/8/8/K4R2 b - -');
$board = FenToBoardFactory::create('1b5k/7p/8/8/8/8/1K6/5R2 b - -');
$checkabilityEval = new CheckabilityEval($board);

$this->assertSame($expectedResult, $checkabilityEval->result);
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/Eval/CompleteFunctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ public function absolute_skewer()
*/
public function kaufman_06()
{
$expectedNormd = [ -1.0, -0.3333, 0.0112, 0.0865, 0.0865, 0.0865, 0.0865, 0.4325, 1.0 ];
$expectedSteinitz = 5;
$expectedMean = 0.0507;
$expectedNormd = [ -1.0, -0.3333, 0.0112, 0.0865, 0.0865, 0.0865, 0.4325, 1.0 ];
$expectedSteinitz = 4;
$expectedMean = 0.0462;
$expectedMedian = 0.0865;
$expectedMode = 0.0865;
$expectedVar = 0.2561;
$expectedSd = 0.5061;
$expectedVar = 0.288;
$expectedSd = 0.5367;

$board = FenToBoardFactory::create('r5k1/3n1ppp/1p6/3p1p2/3P1B2/r3P2P/PR3PP1/2R3K1 b - -');

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Tutor/PgnEvaluationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function endgame()
"Black has a decisive protection advantage.",
"These pieces are hanging: The bishop on e6.",
"The bishop on e6 is unprotected.",
"Overall, 4 evaluation features are favoring White.",
"Overall, 3 evaluation features are favoring White.",
];

$board = FenToBoardFactory::create('8/5k2/4n3/8/8/1BK5/1B6/8 w - - 0 1');
Expand Down

0 comments on commit 41344af

Please sign in to comment.