From 8f4fa18895ebc9f50a9a733d0dd2d0e6de6560fb Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Wed, 20 Feb 2013 18:01:34 +0100 Subject: [PATCH] Fixed problem with invalid castle moves --- FenParser0x88.php | 10 +++++++++- test/ParserTest.php | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/FenParser0x88.php b/FenParser0x88.php index 9faa034..b3f58fc 100644 --- a/FenParser0x88.php +++ b/FenParser0x88.php @@ -393,6 +393,9 @@ class FenParser0x88 } if ($kingSideCastle + && !($this->cache['board'][$piece['s']+1]) + && !($this->cache['board'][$piece['s']+2]) + && ($this->cache['board'][$piece['s']+3]) && !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])) @@ -401,7 +404,12 @@ class FenParser0x88 } - if ($queenSideCastle && $piece['s'] - 2 != -1 && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s']]) && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] - 1]) && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] - 2])) { + if ($queenSideCastle && $piece['s'] - 2 != -1 + && !($this->cache['board'][$piece['s']-1]) + && !($this->cache['board'][$piece['s']-2]) + && !($this->cache['board'][$piece['s']-3]) + && ($this->cache['board'][$piece['s']-4]) + && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s']]) && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] - 1]) && !strstr($protectiveMoves, Board0x88Config::$keySquares[$piece['s'] - 2])) { $paths[] = $piece['s'] - 2; } break; diff --git a/test/ParserTest.php b/test/ParserTest.php index 115f509..9f48017 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -105,6 +105,24 @@ class ParserTest extends PHPUnit_Framework_TestCase $this->assertTrue($parser->isOnSameFile($this->getNumericSquare('a1'), $this->getNumericSquare('a2'))); } + /** + * @test + */ + public function shouldNotBeAbleToCastleInInvalidPositions(){ + // given + $fen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'; + + // when + $parser = $this->getParser($fen); + $legalMoves = $parser->getValidMovesAndResult("white"); + $moves = $legalMoves['moves']; + + // then + $this->assertEquals(array(), $moves[4]); + + + } + /** * @test */