Skip to content

Commit fe2ee14

Browse files
authored
inBasicDraw with bishops of different color
inBasicDraw with bishops of different color
2 parents cb88b88 + 61ef739 commit fe2ee14

File tree

2 files changed

+72
-81
lines changed

2 files changed

+72
-81
lines changed

Tests/ChessGameTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,14 @@ public function testIsInBasicDrawKingBishopVersusKingBishopWithBishopsOnSameColo
541541
$this->assertBasicDraw();
542542
}
543543

544+
public function testIsInBasicDrawKingBishopVersusKingBishopWithBishopsOnDifferentColor()
545+
{
546+
$startFen = '8/8/8/6k1/1K6/8/2B4b/8 w - -';
547+
548+
$this->game->resetGame($startFen);
549+
$this->assertBasicDraw();
550+
}
551+
544552
public function testIsInBasicDrawKingVersusKing()
545553
{
546554
$startFen = '8/5k2/8/8/6K1/8/8/8 w - -';
@@ -559,6 +567,14 @@ public function testIsInBasicDrawKingVersusKingWithBishopOrKnight()
559567
}
560568
}
561569

570+
public function testIsInBasicDrawKingAndTwoKnightsVsKing()
571+
{
572+
$startFen = '8/8/4K3/8/4k3/3nn3/8/8 w - -';
573+
574+
$this->game->resetGame($startFen);
575+
$this->assertTrue($this->game->inBasicDraw());
576+
}
577+
562578
public function testIsInCheckmate()
563579
{
564580
$startFen = '3k2R1/8/3K4/8/8/8/8/8 b - -';
@@ -621,14 +637,6 @@ public function testIsInThreefoldRepetitionDraw()
621637
$this->assertRepetitionDraw();
622638
}
623639

624-
public function testIsNotInBasicDrawKingBishopVersusKingBishopBecauseBishopsOnDifferentColors()
625-
{
626-
$startFen = '6B1/8/8/8/8/6k1/1b6/5K2 w - -';
627-
628-
$this->game->resetGame($startFen);
629-
$this->assertFalse($this->game->inBasicDraw());
630-
}
631-
632640
public function testIsNotInBasicDrawTooManyBlackBishops()
633641
{
634642
$startFen = '6B1/1b6/8/8/8/6k1/1b6/5K2 w - -';

src/Chess/Game/ChessGame.php

Lines changed: 56 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,90 +1425,42 @@ public function in50MoveDraw()
14251425
*/
14261426
public function inBasicDraw()
14271427
{
1428-
$pieces = $this->getPieceTypes();
1429-
$blackPieces = array_keys($pieces['B']);
1430-
$whitePieces = array_keys($pieces['W']);
1428+
$pieces = $this->getPieces();
1429+
$blackPieces = $pieces['B'];
1430+
$whitePieces = $pieces['W'];
1431+
$piecesCount = count($blackPieces) + count($whitePieces);
14311432

1432-
if (count($blackPieces) > 2 || count($whitePieces) > 2) {
1433-
return false;
1434-
} elseif (array_key_exists('B', $pieces['W']) && is_array($pieces['W']['B']) && count($pieces['W']['B']) > 1) {
1435-
return false;
1436-
} elseif (array_key_exists('B', $pieces['B']) && is_array($pieces['B']['B']) && count($pieces['B']['B']) > 1) {
1433+
if ($piecesCount > 4) {
14371434
return false;
14381435
}
14391436

1440-
if (count($blackPieces) == 1) {
1441-
if (count($whitePieces) == 1) {
1442-
return true;
1443-
}
1444-
// XXX: The following if/else block appears to be unreachable due to the if/elseif/elseif block above
1445-
if ($whitePieces[0] == 'K') {
1446-
if (in_array($whitePieces[1], array('N', 'B'))) {
1447-
if (is_array($pieces['W']['N']) && count($pieces['W']['N']) > 1) {
1448-
return false;
1449-
} elseif (is_array($pieces['W']['B']) && count($pieces['W']['B']) > 1) {
1450-
return false;
1451-
} else {
1452-
return true;
1453-
}
1454-
} else {
1455-
return false;
1456-
}
1457-
} else {
1458-
if (in_array($whitePieces[0], array('N', 'B'))) {
1459-
if (array_key_exists('N', $pieces['W']) && is_array($pieces['W']['N']) && count($pieces['W']['N']) > 1) {
1460-
return false;
1461-
} elseif (array_key_exists('B', $pieces['W']) && is_array($pieces['W']['B']) && count($pieces['W']['B']) > 1) {
1462-
return false;
1463-
} else {
1464-
return true;
1465-
}
1466-
} else {
1467-
return false;
1468-
}
1469-
}
1437+
if ($piecesCount === 2) {
1438+
return true; //K vs K
14701439
}
14711440

1472-
if (count($whitePieces) == 1) {
1473-
if (count($blackPieces) == 1) {
1474-
return true;
1475-
}
1476-
// XXX: The following if/else block appears to be unreachable due to the if/elseif/elseif block above
1477-
if ($blackPieces[0] == 'K') {
1478-
if (in_array($blackPieces[1], array('N', 'B'))) {
1479-
if (is_array($pieces['B']['N']) && count($pieces['B']['N']) > 1) {
1480-
return false;
1481-
} elseif (is_array($pieces['B']['B']) && count($pieces['B']['B']) > 1) {
1482-
return false;
1483-
} else {
1484-
return true;
1485-
}
1486-
} else {
1487-
return false;
1488-
}
1489-
} else {
1490-
if (in_array($blackPieces[0], array('N', 'B'))) {
1491-
if (array_key_exists('N', $pieces['B']) && is_array($pieces['B']['N']) && count($pieces['B']['N']) > 1) {
1492-
return false;
1493-
} elseif (array_key_exists('B', $pieces['B']) && is_array($pieces['B']['B']) && count($pieces['B']['B']) > 1) {
1494-
return false;
1495-
} else {
1496-
return true;
1497-
}
1498-
} else {
1499-
return false;
1441+
if ($piecesCount === 4) {
1442+
if (count($whitePieces) === count($blackPieces)) {
1443+
if ((in_array($whitePieces[0], array('N', 'B')) || in_array($whitePieces[1], array('N', 'B')))
1444+
&& (in_array($blackPieces[0], array('N', 'B')) || in_array($blackPieces[1], array('N', 'B')))) {
1445+
return true; //K+minor vs K+minor
15001446
}
15011447
}
1502-
}
1503-
$wpIndex = ($whitePieces[0] == 'K') ? 1 : 0;
1504-
$bpIndex = ($blackPieces[0] == 'K') ? 1 : 0;
1505-
if ($whitePieces[$wpIndex] == 'B' && $blackPieces[$bpIndex] == 'B') {
1506-
// bishops of same color?
1507-
if ($pieces['B']['B'][0] == $pieces['W']['B'][0]) {
1508-
return true;
1448+
1449+
$playerWith3Pieces = count($whitePieces) === 3 ? $whitePieces : $blackPieces;
1450+
1451+
if (in_array($playerWith3Pieces[0], array('K', 'N'))
1452+
&& in_array($playerWith3Pieces[1], array('K', 'N'))
1453+
&& in_array($playerWith3Pieces[2], array('K', 'N'))) {
1454+
return true; //KNN vs K
15091455
}
15101456
}
15111457

1458+
$playerWith2Pieces = count($whitePieces) === 2 ? $whitePieces : $blackPieces;
1459+
1460+
if (in_array($playerWith2Pieces[0], array('N', 'B')) || in_array($playerWith2Pieces[1], array('N', 'B'))) {
1461+
return true; //K+minor vs K
1462+
}
1463+
15121464
return false;
15131465
}
15141466

@@ -3829,4 +3781,35 @@ private function getPieceTypes()
38293781

38303782
return $ret;
38313783
}
3784+
3785+
/**
3786+
* Get a list of all pieces on the board
3787+
*
3788+
* Used to determine basic draw conditions
3789+
* @return array Format:
3790+
*
3791+
* <pre>
3792+
* array(
3793+
* // white pieces
3794+
* 'W' => array('B', 'B', 'K'), //2 bishops and the knight
3795+
* // black pieces
3796+
* 'B' => array('Q', 'K'), //Queen and the knight
3797+
* </pre>
3798+
*/
3799+
private function getPieces()
3800+
{
3801+
$ret = array('W' => array(), 'B' => array());
3802+
foreach ($this->_pieces as $name => $loc) {
3803+
if (!$loc) {
3804+
continue;
3805+
}
3806+
$type = $name[1];
3807+
if (is_array($loc)) {
3808+
$type = $loc[1];
3809+
}
3810+
$ret[$name[0]][] = $type;
3811+
}
3812+
3813+
return $ret;
3814+
}
38323815
}

0 commit comments

Comments
 (0)