Skip to content

Commit

Permalink
Fixed balance()
Browse files Browse the repository at this point in the history
  • Loading branch information
programarivm committed Nov 19, 2023
1 parent cfc7e7a commit 60c6469
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/Heuristics/SanHeuristics.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function getBalance(): array
*/
protected function balance(): SanHeuristics
{
$this->balance[] = (new FenHeuristics($this->board->getStartFen()))
->getBalance();
foreach ($this->sanMovetext->getMoves() as $key => $val) {
if ($val !== Move::ELLIPSIS) {
if ($this->board->play($this->board->getTurn(), $val)) {
Expand Down
45 changes: 27 additions & 18 deletions tests/unit/Heuristics/SanHeuristicsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public function get_balance_start()

$balance = (new SanHeuristics($board->getMovetext()))->getBalance();

$expected = [];
$expected = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

$this->assertSame($expected, $balance);
}
Expand All @@ -34,7 +36,8 @@ public function get_balance_e4()
$balance = (new SanHeuristics($movetext))->getBalance();

$expected = [
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

$this->assertSame($expected, $balance);
Expand All @@ -50,6 +53,7 @@ public function get_balance_e4_e5()
$balance = (new SanHeuristics($movetext))->getBalance();

$expected = [
[ 0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];
Expand All @@ -67,8 +71,9 @@ public function get_balance_e4_e6()
$balance = (new SanHeuristics($movetext))->getBalance();

$expected = [
[ 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 1.0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1.0, 0.0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.25, 0.5, 0.13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

$this->assertEquals($expected, $balance);
Expand All @@ -84,10 +89,11 @@ public function get_balance_e4_e6_d4_d5()
$balance = (new SanHeuristics($movetext))->getBalance();

$expected = [
[ 0, 0.82, 0.33, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 1.0, 0.0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1.0, 0.0, 0.86, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.55, 0.67, 0.57, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 1.0, 0.0, 0, 0, 1.0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.86, 0.2, 1.0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.21, 0.6, 0.13, 0, 0, 1.0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1.0, 0.0, 0.88, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.64, 0.4, 0.63, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

$this->assertEquals($expected, $balance);
Expand All @@ -103,12 +109,13 @@ public function get_balance_e4_e6_d4_d5_Nd2_Nf6()
$balance = (new SanHeuristics($movetext))->getBalance();

$expected = [
[ 0, 0.82, 0.17, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0.5, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 0, 0.86, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.55, 0.33, 0.57, 1, 0, 0.5, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.47, 1, 0.14, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.07, 0.17, 0.14, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.0, 0.83, 0.0, 1.0, 0, 1.0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.86, 0.17, 1.0, 1.0, 0, 1.0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.21, 0.5, 0.13, 1.0, 0, 1.0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 0.0, 0.88, 1.0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.64, 0.33, 0.63, 1.0, 0, 0.5, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.59, 1.0, 0.25, 1.0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.27, 0.17, 0.25, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

$this->assertEquals($expected, $balance);
Expand All @@ -126,9 +133,10 @@ public function get_balance_A00()
$balance = (new SanHeuristics($board->getMovetext()))->getBalance();

$expected = [
[ 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0.57, 0.2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.17, 0.43, 0.4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.67, 0, 0.8, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 1, 0.22, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 0.67, 0.2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0.17, 0.56, 0.4, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
[ 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ],
];

Expand All @@ -147,12 +155,13 @@ public function get_balance_scholar_checkmate()
$balance = (new SanHeuristics($board->getMovetext()))->getBalance();

$expected = [
[ 0, 0.21, 1, 0, 0, 0, 0.8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0, 1, 0.71, 1, 0, 0, 0.8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0, 0.21, 1, 0, 0, 0, 0.8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0, 0.65, 0.93, 0.13, 0.25, 0.25, 0.8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0, 0.36, 0.64, 0, 0.25, 0.25, 0.8, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0, 0.49, 0.36, 0.5, 1, 0.25, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0 ],
[ 0.0, 0.0, 0.0, 0.5, 0.5, 0.25, 0.0, 0.0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1.0, 0 ],
[ 0, 0, 0, 0.5, 0.5, 0.25, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ],
[ 1, 0.58, 0.14, 0.25, 0.75, 1, 0.69, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 ],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function w_e4_b_Nc6_labelled()
$end = end($balance);

$expected = [
'w' => 0,
'w' => 3,
'b' => 0,
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function B56_labelled()

$label = (new GeometricSumLabeller())->label($end);

$expected = 26.7;
$expected = 28.16;

$this->assertSame($expected, $label);
}
Expand Down

0 comments on commit 60c6469

Please sign in to comment.