From 76a0970db0dae23a9db502c8b7c4d79fa02ebe0b Mon Sep 17 00:00:00 2001 From: Alf Magne Kalleland Date: Sat, 23 Feb 2013 04:00:46 +0100 Subject: [PATCH 1/4] Fixed problem parsing games with square brackets in move string --- PgnParser.php | 5 ++--- test/ParserTest.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/PgnParser.php b/PgnParser.php index a025004..5674b25 100644 --- a/PgnParser.php +++ b/PgnParser.php @@ -27,9 +27,8 @@ class PgnParser $this->pgnContent = $content; } - private function cleanPgn() + public function cleanPgn($c) { - $c = $this->pgnContent; $c = preg_replace("/\\$[0-9]+/s", "", $c); $c = str_replace("({", "( {", $c); $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c); @@ -37,7 +36,7 @@ class PgnParser $c = preg_replace("/\t/s", "", $c); $c = preg_replace("/\]\s+\[/s", "]\n[", $c); $c = str_replace(" [", "[", $c); - $c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c); + $c = preg_replace("/([^\]])(\n+)\[([a-z])/si", "$1\n\n[$3", $c); $c = preg_replace("/\n{3,}/s", "\n\n", $c); $c = str_replace("-SB-", "[", $c); $c = str_replace("0-0-0", "O-O-O", $c); diff --git a/test/ParserTest.php b/test/ParserTest.php index 9f48017..2361ba0 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -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'; From 00d9268a257b04c2eb7bb41a6f4875844f1c3499 Mon Sep 17 00:00:00 2001 From: Alf Magne Kalleland Date: Tue, 21 May 2013 15:40:46 +0200 Subject: [PATCH 2/4] Updated license --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3e9fdbb..3934846 100644 --- a/README.md +++ b/README.md @@ -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: ] } ] -``` \ No newline at end of file +``` From 27dbd1d13daeb1113c19e272cbce62fa1b118240 Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Fri, 21 Jun 2013 09:38:19 +0200 Subject: [PATCH 3/4] Added missing CHESS_JSON class --- CHESS_JSON.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 CHESS_JSON.php diff --git a/CHESS_JSON.php b/CHESS_JSON.php new file mode 100644 index 0000000..0f027c1 --- /dev/null +++ b/CHESS_JSON.php @@ -0,0 +1,38 @@ + Date: Fri, 21 Jun 2013 09:39:37 +0200 Subject: [PATCH 4/4] Updated code --- PgnParser.php | 5 +++-- autoload.php | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/PgnParser.php b/PgnParser.php index 5674b25..a025004 100644 --- a/PgnParser.php +++ b/PgnParser.php @@ -27,8 +27,9 @@ class PgnParser $this->pgnContent = $content; } - public function cleanPgn($c) + private function cleanPgn() { + $c = $this->pgnContent; $c = preg_replace("/\\$[0-9]+/s", "", $c); $c = str_replace("({", "( {", $c); $c = preg_replace("/{([^\[]*?)\[([^}]?)}/s", '{$1-SB-$2}', $c); @@ -36,7 +37,7 @@ class PgnParser $c = preg_replace("/\t/s", "", $c); $c = preg_replace("/\]\s+\[/s", "]\n[", $c); $c = str_replace(" [", "[", $c); - $c = preg_replace("/([^\]])(\n+)\[([a-z])/si", "$1\n\n[$3", $c); + $c = preg_replace("/([^\]])(\n+)\[/si", "$1\n\n[", $c); $c = preg_replace("/\n{3,}/s", "\n\n", $c); $c = str_replace("-SB-", "[", $c); $c = str_replace("0-0-0", "O-O-O", $c); diff --git a/autoload.php b/autoload.php index 012ccd0..536fa8e 100644 --- a/autoload.php +++ b/autoload.php @@ -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',