throwing exception if not valid move in move method

This commit is contained in:
DHTMLGoodies
2017-04-15 20:24:49 +02:00
parent 0e2544f133
commit 9490f2cd09
4 changed files with 29 additions and 4 deletions

View File

@@ -23,7 +23,7 @@ class CHESS_JSON {
const GAME_ECO = 'black';
const PGN_KEY_ACTION_ARROW = "ar";
const PGN_KEY_ACTION_HIGHLIGHT = "hl";
const PGN_KEY_ACTION_HIGHLIGHT = "sq";
protected static $jsKeys = array('MOVE_FROM', 'MOVE_TO', 'MOVE_NOTATION', 'FEN','MOVE_COMMENT',

View File

@@ -667,6 +667,10 @@ class FenParser0x88
return $this->validMoves;
}
private function validMoves(){
$validMovesAndResult = $this->getValidMovesAndResult();
return $validMovesAndResult["moves"];
}
private function excludeInvalidSquares($squares, $validSquares)
{
@@ -1324,7 +1328,7 @@ class FenParser0x88
if (!isset($ret['from'])) {
$msg = "Fen: " . $this->fen . "\ncolor: " . $color . "\nnotation: " . $notation . "\nRank:" . $fromRank . "\nFile:" . $fromFile . "\n" . count($foundPieces) . ", " . implode(",", $foundPieces);
throw new Exception($msg);
throw new FenParser0x88Exception($msg);
}
$ret['from'] = Board0x88Config::$numberToSquareMapping[$ret['from']];
$ret['to'] = Board0x88Config::$numberToSquareMapping[$ret['to']];
@@ -1443,6 +1447,10 @@ class FenParser0x88
$move = $this->getFromAndToByNotation($move);
}
if(!$this->canMoveFromTo($move["from"], $move["to"])){
throw new FenParser0x88Exception("Invalid move " . json_encode($move));
}
$this->fen = null;
$this->validMoves = null;
$this->piecesInvolved = $this->getPiecesInvolvedInMove($move);
@@ -1458,7 +1466,19 @@ class FenParser0x88
$this->notation .= '+';
}
}
}
private function canMoveFromTo($from, $to){
$validMoves = $this->validMoves();
$from = Board0x88Config::$mapping[$from];
$to = Board0x88Config::$mapping[$to];
if(empty($validMoves[$from]) || !in_array($to, $validMoves[$from])){
return false;
}
return true;
}
@@ -1776,3 +1796,8 @@ class FenParser0x88
return $fen . " " . $this->getColorCode() . " " . $this->getCastle() . " " . $this->fenParts['enPassant'] . " " . $this->getHalfMoves() . " " . $this->getFullMoves();
}
}
class FenParser0x88Exception extends Exception{
}

Binary file not shown.

View File

@@ -10,9 +10,9 @@
[LiveChessVersion "1.4.8"]
[ECO "A04"]
{[%ar e2e4,e4e5] [%hl e2,e4]}1. Nf3 c5 2. c4 Nc6
{[%ar e2e4,e4e5] [%sq e2,e4]}1. Nf3 c5 2. c4 Nc6
3. Nc3 e5 4. e3 {[%ar a1a8,a8h8]}
Nf6 5. Be2 d5 {[%ar h1h8;#f00,h4a4] [%hl h1,h8,h4]}
Nf6 5. Be2 d5 {[%ar h1h8;#f00,h4a4] [%sq h1,h8,h4]}
6. d4 cxd4 7. exd4 e4
8. Ne5 dxc4 9. Bxc4
Nxe5 10. dxe5 Qxd1+