@@ -1425,90 +1425,42 @@ public function in50MoveDraw()
1425
1425
*/
1426
1426
public function inBasicDraw ()
1427
1427
{
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 );
1431
1432
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 ) {
1437
1434
return false ;
1438
1435
}
1439
1436
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
1470
1439
}
1471
1440
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
1500
1446
}
1501
1447
}
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
1509
1455
}
1510
1456
}
1511
1457
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
+
1512
1464
return false ;
1513
1465
}
1514
1466
@@ -3829,4 +3781,35 @@ private function getPieceTypes()
3829
3781
3830
3782
return $ ret ;
3831
3783
}
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
+ }
3832
3815
}
0 commit comments