From fd4147d0369f79c6231f1c98f13ca8707bc0dec7 Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Tue, 12 Jan 2016 22:07:49 +0100 Subject: [PATCH] Always set En passant square when pawn moves to squares --- FenParser0x88.php | 10 +++---- test/ParserTest.php | 68 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 9 deletions(-) diff --git a/FenParser0x88.php b/FenParser0x88.php index ef2aa06..e0935f3 100644 --- a/FenParser0x88.php +++ b/FenParser0x88.php @@ -1510,12 +1510,10 @@ class FenParser0x88 } if (($move['from'] & 15) == ($move['to'] & 15) && $this->getDistance($move['from'], $move['to']) == 2) { - if ($this->cache['board'][$move['to'] - 1] || $this->cache['board'][$move['to'] + 1]) { - if ($color === 'white') { - $enPassant = Board0x88Config::$numberToSquareMapping[$move['from'] + 16]; - } else { - $enPassant = Board0x88Config::$numberToSquareMapping[$move['from'] - 16]; - } + if ($color === 'white') { + $enPassant = Board0x88Config::$numberToSquareMapping[$move['from'] + 16]; + } else { + $enPassant = Board0x88Config::$numberToSquareMapping[$move['from'] - 16]; } } } diff --git a/test/ParserTest.php b/test/ParserTest.php index cdd9220..3ede348 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -61,6 +61,64 @@ class ParserTest extends PHPUnit_Framework_TestCase $this->assertEquals('d6', $parser->getEnPassantSquare()); } + + /** + * @test + */ + public function shouldFindEnPassantInPgn(){ + $pgn = '[Event "Millionaire Chess KO 2015"] +[Site "Las Vegas USA"] +[Date "2015.10.12"] +[Round "1.1"] +[White "Yu Yangyi"] +[Black "Nakamura, Hikaru"] +[Result "1/2-1/2"] +[ECO "C67"] +[WhiteElo "2721"] +[BlackElo "2816"] +[PlyCount "44"] +[EventDate "2015.10.12"] + +1. e4 e5 2. Nf3 Nc6 3. Bb5 Nf6 4. O-O Nxe4 5. Re1 Nd6 6. Nxe5 Be7 7. Bf1 Nxe5 +8. Rxe5 O-O 9. Nc3 Ne8 10. Nd5 Bd6 11. Re1 c6 12. Ne3 Be7 13. Qe2 Bf6 14. Nf5 +d5 15. Ne7+ Kh8 16. Nxc8 Rxc8 17. Qd1 Nd6 18. c3 Bg5 19. d4 Bxc1 20. Rxc1 Rc7 +21. Bd3 Re7 22. Rxe7 Qxe7 1/2-1/2'; + $pgnParser = new PgnParser(); + $pgnParser->setPgnContent($pgn); + $game = $pgnParser->getFirstGame(); + + echo json_encode($game); + + + + + + } + /** + * @test + */ + public function shouldGenerateEnPassantSquareInFen(){ + // given + $parser = new FenParser0x88(); + $parser->newGame(); + + + // when + $parser->move("e2e4"); + $parser->move("e7e5"); + + + // then + $this->assertEquals("e6", $parser->getEnPassantSquare()); + + + $parser->move("e4e5"); + $parser->move("f7f5"); + + // then + $this->assertEquals("f6", $parser->getEnPassantSquare()); + } + /** * @test */ @@ -1363,7 +1421,7 @@ Rc8 Ne6+ 72. Kf6 d2 73. c5+ Kd7 0-1'; // when $parser->move(array('from' => 'e2', 'to' => 'e4')); $newFen = $parser->getFen(); - $expectedFen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1'; + $expectedFen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1'; // then $this->assertEquals($expectedFen, $newFen); @@ -1960,10 +2018,10 @@ Rc8 Ne6+ 72. Kf6 d2 73. c5+ Kd7 0-1'; $parser->newGame(); $parser->move(array('from' => 'e2', 'to' => 'e4')); - $this->assertEquals('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1', $parser->getFen()); + $this->assertEquals('rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1', $parser->getFen()); $parser->move(array('from' => 'e7', 'to' => 'e5')); - $this->assertEquals('rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 2', $parser->getFen()); + $this->assertEquals('rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2', $parser->getFen()); } /** @@ -2045,6 +2103,10 @@ Rc8 Ne6+ 72. Kf6 d2 73. c5+ Kd7 0-1'; } + + + + 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';