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

View File

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