This commit is contained in:
DHTMLGoodies
2013-08-28 13:26:04 +02:00
4 changed files with 55 additions and 2 deletions

38
CHESS_JSON.php Normal file
View File

@@ -0,0 +1,38 @@
<?php
class CHESS_JSON {
const MOVE_FROM = 'from';
const MOVE_TO = 'to';
const MOVE_NOTATION = 'm';
const FEN = 'fen';
const MOVE_COMMENT = 'comment';
const MOVE_ACTION = 'action';
const MOVE_VARIATIONS = 'variations';
const MOVE_MOVES = 'moves';
const MOVE_CAPTURE = 'capture';
const MOVE_PROMOTE_TO = 'promoteTo';
const MOVE_CASTLE = 'castle';
const MOVE_PARSED = 'castle';
const GAME_METADATA = 'metadata';
const GAME_EVENT = 'event';
const GAME_WHITE = 'white';
const GAME_BLACK = 'black';
const GAME_ECO = 'black';
protected static $jsKeys = array('MOVE_FROM', 'MOVE_TO', 'MOVE_NOTATION', 'FEN','MOVE_COMMENT',
'MOVE_ACTION', 'MOVE_VARIATIONS', 'MOVE_MOVES','MOVE_CAPTURE','MOVE_PROMOTE_TO','MOVE_CASTLE',
'GAME_METADATA', 'GAME_EVENT', 'GAME_WHITE','GAME_BLACK', 'GAME_ECO',
);
public static function toJavascript(){
$ret = array();
foreach(self::$jsKeys as $key){
$ret[$key] = constant("CHESS_JSON::" . $key);
}
return 'ludo.CHESS_JSON_KEY = ' . json_encode($ret) .';';
}
}

View File

@@ -3,7 +3,7 @@ PHP class for conversion of Chess PGN files to PHP array or JSON.
This is the chess parser used in DHTML Chess at dhtml-chess.com
####License: GPL
####License: LGPL (Lesser General Public License).
Example of use:
@@ -248,4 +248,4 @@ This will give you data in this format:
]
}
]
```
```

View File

@@ -7,6 +7,7 @@ spl_autoload_register(
static $classes = null;
if ($classes === null) {
$classes = array(
'chess_json' => '/CHESS_JSON.php',
'board0x88config' => '/Board0x88Config.php',
'dgtgameparser' => '/DGTGameParser.php',
'fenparser0x88' => '/FenParser0x88.php',

View File

@@ -1836,6 +1836,20 @@ class ParserTest extends PHPUnit_Framework_TestCase
$this->assertEquals(82, count($game['moves']));
}
/**
* @test
*/
public function shouldSplitPgnIntoCorrectGames(){
// given
$pgnParser = new PgnParser("pgn/1001-brilliant-checkmates.pgn");
// when
$games = $pgnParser->getUnparsedGames();
// then
$this->assertEquals(994, count($games));
}
private function getSpasskyFischerGameWith3FoldReptition(){
$parser = $this->getParser();
$moves = 'e4,d6,d4,g6,Nc3,Nf6,f4,Bg7,Nf3,c5,dxc5,Qa5,Bd3,Qxc5,Qe2,O-O,Be3,Qa5,O-O,Bg4,Rad1,Nc6,Bc4,Nh5,Bb3,Bxc3,bxc3,Qxc3,f5,Nf6,h3,Bxf3,Qxf3,Na5,Rd3,Qc7,Bh6,Nxb3,cxb3,Qc5+,Kh1,Qe5,Bxf8,Rxf8,Re3,Rc8,fxg6,hxg6,Qf4,Qxf4,Rxf4,Nd7,Rf2,Ne5,Kh2,Rc1,Ree2,Nc6,Rc2,Re1,Rfe2,Ra1,Kg3,Kg7,Rcd2,Rf1,Rf2,Re1,Rfe2,Rf1,Re3,a6,Rc3,Re1,Rc4,Rf1,Rdc2,Ra1,Rf2,Re1,Rfc2,g5,Rc1,Re2,R1c2,Re1,Rc1,Re2,R1c2';