Added support for move objects(from and to) in getparsed method

This commit is contained in:
DHTMLGoodies
2013-01-25 16:54:09 +01:00
parent 74900962f4
commit f117acc00e
2 changed files with 91 additions and 80 deletions

View File

@@ -16,7 +16,8 @@ class FenParser0x88
} }
} }
public function newGame($fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'){ public function newGame($fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1')
{
$this->validMoves = null; $this->validMoves = null;
$this->setFen($fen); $this->setFen($fen);
} }
@@ -43,7 +44,8 @@ class FenParser0x88
private $longNotation; private $longNotation;
public function getLongNotation(){ public function getLongNotation()
{
return $this->longNotation; return $this->longNotation;
} }
@@ -52,7 +54,8 @@ class FenParser0x88
* @param bool $shortNotation * @param bool $shortNotation
* @return string * @return string
*/ */
public function getLongNotationForAMove($move, $shortNotation) { public function getLongNotationForAMove($move, $shortNotation)
{
if (strstr($shortNotation, 'O-')) { if (strstr($shortNotation, 'O-')) {
return $shortNotation; return $shortNotation;
} }
@@ -127,7 +130,8 @@ class FenParser0x88
} }
} }
public function getPieceOnSquare($square) { public function getPieceOnSquare($square)
{
$piece = $this->cache['board'][$square]; $piece = $this->cache['board'][$square];
if (isset($piece)) { if (isset($piece)) {
return array( return array(
@@ -142,7 +146,8 @@ class FenParser0x88
return null; return null;
} }
public function isValid($move, $fen){ public function isValid($move, $fen)
{
$this->setFen($fen); $this->setFen($fen);
if (!isset($move['from'])) { if (!isset($move['from'])) {
$fromAndTo = $this->getFromAndToByNotation($move[CHESS_JSON::MOVE_NOTATION]); $fromAndTo = $this->getFromAndToByNotation($move[CHESS_JSON::MOVE_NOTATION]);
@@ -207,7 +212,8 @@ class FenParser0x88
return Board0x88Config::$colorAbbreviations[$this->fenParts['color']]; return Board0x88Config::$colorAbbreviations[$this->fenParts['color']];
} }
function switchColor() { function switchColor()
{
$this->fenParts['color'] = $this->fenParts['color'] == 'w' ? 'b' : 'w'; $this->fenParts['color'] = $this->fenParts['color'] == 'w' ? 'b' : 'w';
} }
@@ -392,7 +398,8 @@ class FenParser0x88
if ($kingSideCastle if ($kingSideCastle
&& !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s']]) && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s']])
&& ($piece['s'] < 118 && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] + 1])) && ($piece['s'] < 118 && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] + 1]))
&& ($piece['s'] < 117 && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] + 2]))) { && ($piece['s'] < 117 && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] + 2]))
) {
$paths[] = $piece['s'] + 2; $paths[] = $piece['s'] + 2;
} }
@@ -411,8 +418,7 @@ class FenParser0x88
$result = 0; $result = 0;
if ($checks && !$totalCountMoves) { if ($checks && !$totalCountMoves) {
$result = $color === 'black' ? 1 : -1; $result = $color === 'black' ? 1 : -1;
} } else if (!$checks && !$totalCountMoves) {
else if (!$checks && !$totalCountMoves) {
$result = .5; $result = .5;
} }
$this->validMoves = array('moves' => $ret, 'result' => $result, 'check' => $checks); $this->validMoves = array('moves' => $ret, 'result' => $result, 'check' => $checks);
@@ -525,8 +531,7 @@ class FenParser0x88
case 0x0E: case 0x0E:
if ($numericDistance % 16 === 0) { if ($numericDistance % 16 === 0) {
$ret[] = array('s' => $piece['s'], 'p' => $boardDistance); $ret[] = array('s' => $piece['s'], 'p' => $boardDistance);
} } else if (($piece['s'] & 240) == ($king['s'] & 240)) {
else if (($piece['s'] & 240) == ($king['s'] & 240)) {
$ret[] = array('s' => $piece['s'], 'p' => $numericDistance > 0 ? 1 : -1); $ret[] = array('s' => $piece['s'], 'p' => $numericDistance > 0 ? 1 : -1);
} }
break; break;
@@ -535,8 +540,7 @@ class FenParser0x88
case 0x0F: case 0x0F:
if ($numericDistance % 15 === 0 || $numericDistance % 17 === 0 || $numericDistance % 16 === 0) { if ($numericDistance % 15 === 0 || $numericDistance % 17 === 0 || $numericDistance % 16 === 0) {
$ret[] = array('s' => $piece['s'], 'p' => $boardDistance); $ret[] = array('s' => $piece['s'], 'p' => $boardDistance);
} } else if (($piece['s'] & 240) == ($king['s'] & 240)) {
else if (($piece['s'] & 240) == ($king['s'] & 240)) {
$ret[] = (array('s' => $piece['s'], 'p' => $numericDistance > 0 ? 1 : -1)); $ret[] = (array('s' => $piece['s'], 'p' => $numericDistance > 0 ? 1 : -1));
} }
break; break;
@@ -584,9 +588,11 @@ class FenParser0x88
return $ret; return $ret;
} }
public function getBoardCache(){ public function getBoardCache()
{
return $this->cache['board']; return $this->cache['board'];
} }
/** /**
* Return valid squares for other pieces than king to move to when in check, i.e. squares * Return valid squares for other pieces than king to move to when in check, i.e. squares
* which avoids the check * which avoids the check
@@ -806,7 +812,11 @@ class FenParser0x88
} }
public function getParsed($move){ public function getParsed($move)
{
if (is_string($move)) $move = array('m' => $move);
if (isset($move['m'])) {
if ($move['m'] == '--') { if ($move['m'] == '--') {
$this->fen = null; $this->fen = null;
$this->switchColor(); $this->switchColor();
@@ -816,6 +826,9 @@ class FenParser0x88
); );
} }
$fromAndTo = $this->getFromAndToByNotation($move['m']); $fromAndTo = $this->getFromAndToByNotation($move['m']);
}else{
$fromAndTo = $move;
}
$this->makeMove($fromAndTo); $this->makeMove($fromAndTo);
$newProperties = array( $newProperties = array(
'from' => $fromAndTo['from'], 'from' => $fromAndTo['from'],
@@ -858,7 +871,6 @@ class FenParser0x88
$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') {
@@ -937,8 +949,7 @@ class FenParser0x88
break; break;
} }
} }
} } else if ($fromFile !== null && $fromFile >= 0) {
else if ($fromFile !== null && $fromFile >= 0) {
for ($i = 0, $len = count($foundPieces); $i < $len; $i++) { for ($i = 0, $len = count($foundPieces); $i < $len; $i++) {
if ($this->isOnSameFile($foundPieces[$i], $fromFile)) { if ($this->isOnSameFile($foundPieces[$i], $fromFile)) {
$ret['from'] = $foundPieces[$i]; $ret['from'] = $foundPieces[$i];
@@ -968,7 +979,8 @@ class FenParser0x88
return $ret; return $ret;
} }
public function hasThreeFoldRepetition($fens = array()){ public function hasThreeFoldRepetition($fens = array())
{
if (!count($fens)) return false; if (!count($fens)) return false;
$shortenedFens = array(); $shortenedFens = array();
foreach ($fens as $fen) { foreach ($fens as $fen) {
@@ -982,7 +994,8 @@ class FenParser0x88
return $count[$lastFen] >= 2; return $count[$lastFen] >= 2;
} }
public function getPromoteByNotation($notation){ public function getPromoteByNotation($notation)
{
if (strstr($notation, '=')) { if (strstr($notation, '=')) {
$piece = preg_replace("/^.*?=([QRBN]).*$/", '$1', $notation); $piece = preg_replace("/^.*?=([QRBN]).*$/", '$1', $notation);
return Board0x88Config::$pieceAbbr[$piece]; return Board0x88Config::$pieceAbbr[$piece];
@@ -1024,28 +1037,26 @@ class FenParser0x88
function getPieceTypeByNotation($notation, $color = null) function getPieceTypeByNotation($notation, $color = null)
{ {
$notation = preg_replace("/\=[QRNB]/s", "", $notation);
if ($notation === 'O-O-O' || $notation === 'O-O') { if ($notation === 'O-O-O' || $notation === 'O-O') {
$notation = 'K'; $pieceType = 'K';
} else { } else {
$notation = preg_replace("/.*?([NRBQK]).*/s", '$1', $notation); $token = substr($notation, 0, 1);
if (!$notation || strlen($notation) > 1) { $pieceType = preg_match("/[NRBQK]/", $token) ? $token : 'P';
$notation = 'P';
}
} }
$notation = Board0x88Config::$pieces[$notation]; $pieceType = Board0x88Config::$pieces[$pieceType];
if ($color === 'black') { if ($color === 'black') {
$notation += 8; $pieceType += 8;
} }
return $notation; return $pieceType;
} }
private $piecesInvolved; private $piecesInvolved;
private $notation; private $notation;
private $validMoves = null; private $validMoves = null;
function move($move) function move($move)
{ {
$this->fen = null; $this->fen = null;
@@ -1092,7 +1103,8 @@ class FenParser0x88
return $this->fenParts['castle']; return $this->fenParts['castle'];
} }
function getCastleCode(){ function getCastleCode()
{
return $this->fenParts['castleCode']; return $this->fenParts['castleCode'];
} }
@@ -1306,8 +1318,7 @@ class FenParser0x88
if (array_search($move['to'], $moves) >= 0) { if (array_search($move['to'], $moves) >= 0) {
if (($square & 15) != ($move['from'] & 15)) { if (($square & 15) != ($move['from'] & 15)) {
$ret += Board0x88Config::$fileMapping[$move['from'] & 15]; $ret += Board0x88Config::$fileMapping[$move['from'] & 15];
} } else if (($square & 240) != ($move['from'] & 240)) {
else if (($square & 240) != ($move['from'] & 240)) {
$ret += Board0x88Config::$rankMapping[$move['from'] & 240]; $ret += Board0x88Config::$rankMapping[$move['from'] & 240];
} }
} }

View File

@@ -1765,7 +1765,7 @@ class ParserTest extends PHPUnit_Framework_TestCase
/** /**
* @test * @test
*/ */
public function shouldDetermine3FoldReptition(){ public function shouldDetermine3FoldRepetition(){
// given // given
$parser = new FenParser0x88(); $parser = new FenParser0x88();
// when // when