Skip to content

Commit f7af2b5

Browse files
committed
CV-142681 Chess-Game Do not allow pieces in rook position to castle
1 parent d35284a commit f7af2b5

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

Tests/ChessGameTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ public function testBlankBoardFen()
7171
$this->assertEquals('8/8/8/8/8/8/8/8 w KQkq - 1 1', $this->game->renderFen());
7272
}
7373

74+
public function testCastlingForbiddenIfNotRook()
75+
{
76+
$startFen = 'q3k2q/pbpqppbp/1pnp1np1/8/8/1PNP1NP1/PBPQPPBP/Q3K2Q w KQkq - 2 9';
77+
$this->game->resetGame($startFen);
78+
79+
$this->assertFalse($this->game->canCastleKingside());
80+
$this->assertFalse($this->game->canCastleQueenside());
81+
82+
$this->game->moveSAN('e3');
83+
84+
$this->assertFalse($this->game->canCastleKingside());
85+
$this->assertFalse($this->game->canCastleQueenside());
86+
}
87+
7488
public function testCastlingBlackFromTheKingSide()
7589
{
7690
$startFen = 'rnbqk2r/pppp1ppp/5n2/2b1p3/P1P1P1P1/8/1P1P1P1P/RNBQKBNR b KQkq a3 0 4';

src/Chess/Game/ChessGame.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2200,16 +2200,16 @@ public function _parseFen($fen)
22002200
}
22012201
switch ($splitFen[2][$i]) {
22022202
case 'K' :
2203-
$this->_WCastleK = true;
2203+
$this->_WCastleK = $this->getPiece('WR1') !== false;
22042204
break;
22052205
case 'Q' :
2206-
$this->_WCastleQ = true;
2206+
$this->_WCastleQ = $this->getPiece('WR2') !== false;
22072207
break;
22082208
case 'k' :
2209-
$this->_BCastleK = true;
2209+
$this->_BCastleK = $this->getPiece('BR1') !== false;
22102210
break;
22112211
case 'q' :
2212-
$this->_BCastleQ = true;
2212+
$this->_BCastleQ = $this->getPiece('BR2') !== false;
22132213
break;
22142214
default:
22152215
return $this->raiseError(self::GAMES_CHESS_ERROR_FEN_CASTLEWRONG,

0 commit comments

Comments
 (0)