Fixed problem with some pawn moves

This commit is contained in:
DHTMLGoodies
2017-02-04 22:59:30 +01:00
parent 5fe2491d1f
commit 155af550c1
3 changed files with 63 additions and 28 deletions

View File

@@ -141,7 +141,8 @@ class FenParser0x88
* @param string $square * @param string $square
* @return array|null * @return array|null
*/ */
public function getPieceOnSquareBoardCoordinate($square){ public function getPieceOnSquareBoardCoordinate($square)
{
return $this->getPieceOnSquare(Board0x88Config::$mapping[$square]); return $this->getPieceOnSquare(Board0x88Config::$mapping[$square]);
} }
@@ -210,7 +211,8 @@ class FenParser0x88
* $whiteKing = $parser->getWhiteKingSquare(); // returns g1 * $whiteKing = $parser->getWhiteKingSquare(); // returns g1
* @return string * @return string
*/ */
public function getWhiteKingSquare(){ public function getWhiteKingSquare()
{
return $this->getKingSquareBoardCoordinates("white"); return $this->getKingSquareBoardCoordinates("white");
} }
@@ -221,11 +223,13 @@ class FenParser0x88
* $whiteKing = $parser->getBlackKingSquare(); // returns g8 * $whiteKing = $parser->getBlackKingSquare(); // returns g8
* @return string * @return string
*/ */
public function getBlackKingSquare(){ public function getBlackKingSquare()
{
return $this->getKingSquareBoardCoordinates("black"); return $this->getKingSquareBoardCoordinates("black");
} }
public function getKingSquareBoardCoordinates($color){ public function getKingSquareBoardCoordinates($color)
{
$king = $this->getKing($color); $king = $this->getKing($color);
return Board0x88Config::$numberToSquareMapping[$king["s"]]; return Board0x88Config::$numberToSquareMapping[$king["s"]];
@@ -312,7 +316,8 @@ class FenParser0x88
* $whiteCanCastle = $parser->canWhiteCastleKingSide(); * $whiteCanCastle = $parser->canWhiteCastleKingSide();
* @return bool * @return bool
*/ */
public function canWhiteCastleKingSide(){ public function canWhiteCastleKingSide()
{
return $this->canCastleKingSide("white"); return $this->canCastleKingSide("white");
} }
@@ -324,7 +329,8 @@ class FenParser0x88
* $whiteCanCastle = $parser->canBlackCastleKingSide(); * $whiteCanCastle = $parser->canBlackCastleKingSide();
* @return bool * @return bool
*/ */
public function canBlackCastleKingSide(){ public function canBlackCastleKingSide()
{
return $this->canCastleKingSide("black"); return $this->canCastleKingSide("black");
} }
@@ -367,7 +373,8 @@ class FenParser0x88
* Returns true if white can castle queen side(from current fen) * Returns true if white can castle queen side(from current fen)
* @return bool * @return bool
*/ */
public function canWhiteCastleQueenSide(){ public function canWhiteCastleQueenSide()
{
return $this->canCastleQueenSide("white"); return $this->canCastleQueenSide("white");
} }
@@ -375,7 +382,8 @@ class FenParser0x88
* Returns true if black can castle queen side (from current fen) * Returns true if black can castle queen side (from current fen)
* @return bool * @return bool
*/ */
public function canBlackCastleQueenSide(){ public function canBlackCastleQueenSide()
{
return $this->canCastleQueenSide("black"); return $this->canCastleQueenSide("black");
} }
@@ -427,16 +435,17 @@ class FenParser0x88
* the piece on "g8" * the piece on "g8"
* *
*/ */
public function getValidMovesBoardCoordinates($color = null){ public function getValidMovesBoardCoordinates($color = null)
{
$movesAndResult = $this->getValidMovesAndResult($color); $movesAndResult = $this->getValidMovesAndResult($color);
$moves = $movesAndResult["moves"]; $moves = $movesAndResult["moves"];
$ret = array(); $ret = array();
foreach($moves as $from => $toSquares){ foreach ($moves as $from => $toSquares) {
$fromSquare = Board0x88Config::$numberToSquareMapping[$from]; $fromSquare = Board0x88Config::$numberToSquareMapping[$from];
$squares = array(); $squares = array();
foreach($toSquares as $square){ foreach ($toSquares as $square) {
$squares[] = Board0x88Config::$numberToSquareMapping[$square]; $squares[] = Board0x88Config::$numberToSquareMapping[$square];
} }
@@ -451,7 +460,8 @@ class FenParser0x88
* Returns result(0 = undecided, 0.5 = draw, 1 = white wins, -1 = black wins) * Returns result(0 = undecided, 0.5 = draw, 1 = white wins, -1 = black wins)
* @return int * @return int
*/ */
public function getResult(){ public function getResult()
{
$movesAndResult = $this->getValidMovesAndResult(); $movesAndResult = $this->getValidMovesAndResult();
return $movesAndResult["result"]; return $movesAndResult["result"];
} }
@@ -810,11 +820,12 @@ class FenParser0x88
* @param string $color * @param string $color
* @return array * @return array
*/ */
public function getPinnedBoardCoordinates($color){ public function getPinnedBoardCoordinates($color)
{
$pinned = $this->getPinned($color); $pinned = $this->getPinned($color);
$ret = array(); $ret = array();
foreach($pinned as $square=>$by){ foreach ($pinned as $square => $by) {
$ret[] = array( $ret[] = array(
"square" => Board0x88Config::$numberToSquareMapping[$square], "square" => Board0x88Config::$numberToSquareMapping[$square],
"pinnedBy" => Board0x88Config::$numberToSquareMapping[$by["by"]], "pinnedBy" => Board0x88Config::$numberToSquareMapping[$by["by"]],
@@ -825,6 +836,7 @@ class FenParser0x88
return $ret; return $ret;
} }
/** /**
* Return numeric squares(0x88) of pinned pieces * Return numeric squares(0x88) of pinned pieces
* @param $color * @param $color
@@ -880,11 +892,12 @@ class FenParser0x88
* @param string $color * @param string $color
* @return array * @return array
*/ */
public function getValidSquaresOnCheckBoardCoordinates($color){ public function getValidSquaresOnCheckBoardCoordinates($color)
{
$squares = $this->getValidSquaresOnCheck($color); $squares = $this->getValidSquaresOnCheck($color);
$ret = array(); $ret = array();
foreach($squares as $square){ foreach ($squares as $square) {
$ret[] = Board0x88Config::$numberToSquareMapping[$square]; $ret[] = Board0x88Config::$numberToSquareMapping[$square];
} }
return $ret; return $ret;
@@ -1127,11 +1140,11 @@ class FenParser0x88
} }
public function getExtendedMoveInfo($move){ public function getExtendedMoveInfo($move)
{
$move = $this->getParsed($move); $move = $this->getParsed($move);
return $move; return $move;
} }
@@ -1201,17 +1214,19 @@ class FenParser0x88
$notation = preg_replace("/^(.*?)[QRBN]$/s", "$1", $notation); $notation = preg_replace("/^(.*?)[QRBN]$/s", "$1", $notation);
$pieceType = $this->getPieceTypeByNotation($notation, $color); $pieceType = $this->getPieceTypeByNotation($notation, $color);
$capture = strpos($notation, "x") > 0;
$ret['to'] = $this->getToSquareByNotation($notation); $ret['to'] = $this->getToSquareByNotation($notation);
switch ($pieceType) { switch ($pieceType) {
case 0x01: case 0x01:
case 0x09: case 0x09:
if ($color === 'black') { if ($color === 'black') {
$offsets = array(15, 17, 16); $offsets = $capture ? array(15, 17) : array(16);
if ($ret['to'] >= 64) { if ($ret['to'] >= 64) {
$offsets[] = 32; $offsets[] = 32;
} }
} else { } else {
$offsets = array(-15, -17, -16); $offsets = $capture ? array(-15, -17) : array(-16);
if ($ret['to'] < 64) { if ($ret['to'] < 64) {
$offsets[] = -32; $offsets[] = -32;
} }
@@ -1273,7 +1288,6 @@ class FenParser0x88
if (count($foundPieces) === 1) { if (count($foundPieces) === 1) {
$ret['from'] = $foundPieces[0]; $ret['from'] = $foundPieces[0];
} else { } else {
if ($fromRank !== null && $fromRank >= 0) { if ($fromRank !== null && $fromRank >= 0) {
for ($i = 0, $len = count($foundPieces); $i < $len; $i++) { for ($i = 0, $len = count($foundPieces); $i < $len; $i++) {
if ($this->isOnSameRank($foundPieces[$i], $fromRank)) { if ($this->isOnSameRank($foundPieces[$i], $fromRank)) {
@@ -1309,8 +1323,6 @@ class FenParser0x88
#} #}
if (!isset($ret['from'])) { if (!isset($ret['from'])) {
$msg = "Fen: " . $this->fen . "\ncolor: " . $color . "\nnotation: " . $notation . "\nRank:" . $fromRank . "\nFile:" . $fromFile . "\n" . count($foundPieces) . ", " . implode(",", $foundPieces); $msg = "Fen: " . $this->fen . "\ncolor: " . $color . "\nnotation: " . $notation . "\nRank:" . $fromRank . "\nFile:" . $fromFile . "\n" . count($foundPieces) . ", " . implode(",", $foundPieces);
throw new Exception($msg); throw new Exception($msg);
} }
@@ -1399,8 +1411,8 @@ class FenParser0x88
} }
function moveByLongNotation($notation)
function moveByLongNotation($notation){ {
$fromAndTo = $this->getFromAndToByLongNotation($notation); $fromAndTo = $this->getFromAndToByLongNotation($notation);
$this->move($fromAndTo); $this->move($fromAndTo);
@@ -1425,9 +1437,9 @@ class FenParser0x88
public function move($move) public function move($move)
{ {
if(is_string($move) && strlen($move) == 4){ if (is_string($move) && strlen($move) == 4) {
$move = $this->getFromAndToByLongNotation($move); $move = $this->getFromAndToByLongNotation($move);
}else if(is_string($move)){ } else if (is_string($move)) {
$move = $this->getFromAndToByNotation($move); $move = $this->getFromAndToByNotation($move);
} }
@@ -1448,7 +1460,6 @@ class FenParser0x88
} }
} }
function setNewColor() function setNewColor()

View File

@@ -2027,6 +2027,15 @@ Rc8 Ne6+ 72. Kf6 d2 73. c5+ Kd7 0-1';
$this->assertEquals((36*2)+1, count($game['moves'])); $this->assertEquals((36*2)+1, count($game['moves']));
} }
/**
* @test
*/
public function shouldParseProblemCurio(){
$pgnParser = new PgnParser("pgn/CURIO.pgn");
$games = $pgnParser->getGames();
}
/** /**
* @test * @test
*/ */

15
test/pgn/CURIO.PGN Executable file
View File

@@ -0,0 +1,15 @@
[Event ""]
[Site "Stockholm"]
[Date "1960"]
[Round ""]
[White "Rantanen"]
[Black "Horberg"]
[Result "0-1"]
[fen "4kb1r/1q3ppp/2b1r3/3p4/2PQ3P/2BBPP2/pp2K1P1/3RR3 b k - 3 28"]
1... a1Q 29.Rxa1 bxa1Q 30.Rxa1 dxc4 31.Bxc4 Re7 32.Ra6 f6 33.Qc5 Rc7
34.Qa5 Bd6 35.Rb6 Qc8 36.Ba6 Qa8 37.Qf5 Bd7 38.Qd3 Bc5 39.Bb7 Qd8
40.Ra6 O-O 41.Ba5 Qb8 42.Qd5+ Kh8 43.Bxc7 Bb5+ 44.Kf2 Qxc7 45.Ra8
Rxa8 46.Bxa8 Qe7 47.Qe4 Qxe4 48.Bxe4 g6 49.g4 Kg7 50.f4 Ba4 51.Bd5 Bd1
52.g5 Bc2 53.Kf3 Bb4 54.Ke2 f5 55.h5 gxh5 56.Kf3 h4 57.e4 Bxe4+ 58.Bxe4
fxe4+ 59.Kxe4 h3 60.Kf3 Be1 61.f5 Kf7 0-1