Skip to content

Commit da114f8

Browse files
authored
Merge pull request #38 from ChessCom/AM/CV-142681-Chess-Game-Do-not-allow-pieces-in-rook-position-to-castle
CV-142681 Chess-Game Do not allow pieces in rook position to castle
2 parents d35284a + 5bf4595 commit da114f8

File tree

2 files changed

+54
-11
lines changed

2 files changed

+54
-11
lines changed

Tests/ChessGameTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,42 @@ 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+
self::assertFalse($this->game->canCastleKingside());
80+
self::assertFalse($this->game->canCastleQueenside());
81+
82+
$this->game->moveSAN('e3');
83+
84+
self::assertFalse($this->game->canCastleKingside());
85+
self::assertFalse($this->game->canCastleQueenside());
86+
87+
$startFen = 'r3k2q/pppppppp/8/8/8/8/PPPPPPPP/Q3K2R w KQkq - 0 1';
88+
$this->game->resetGame($startFen);
89+
90+
self::assertTrue($this->game->canCastleKingside());
91+
self::assertFalse($this->game->canCastleQueenside());
92+
93+
$this->game->moveSAN('e3');
94+
95+
self::assertFalse($this->game->canCastleKingside());
96+
self::assertTrue($this->game->canCastleQueenside());
97+
98+
$startFen = 'q3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2Q w KQkq - 0 1';
99+
$this->game->resetGame($startFen);
100+
101+
self::assertFalse($this->game->canCastleKingside());
102+
self::assertTrue($this->game->canCastleQueenside());
103+
104+
$this->game->moveSAN('e3');
105+
106+
self::assertTrue($this->game->canCastleKingside());
107+
self::assertFalse($this->game->canCastleQueenside());
108+
}
109+
74110
public function testCastlingBlackFromTheKingSide()
75111
{
76112
$startFen = 'rnbqk2r/pppp1ppp/5n2/2b1p3/P1P1P1P1/8/1P1P1P1P/RNBQKBNR b KQkq a3 0 4';

src/Chess/Game/ChessGame.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,23 +2193,30 @@ public function _parseFen($fen)
21932193
$this->_WCastleK = false;
21942194
$this->_BCastleQ = false;
21952195
$this->_BCastleK = false;
2196+
2197+
$wr1 = $this->getPiece('WR1');
2198+
$wr2 = $this->getPiece('WR2');
2199+
$br1 = $this->getPiece('BR1');
2200+
$br2 = $this->getPiece('BR2');
2201+
21962202
if ($splitFen[2] != '-') {
21972203
for ($i = 0; $i < 4; $i++) {
21982204
if ($i >= strlen($splitFen[2])) {
21992205
continue;
22002206
}
2207+
22012208
switch ($splitFen[2][$i]) {
22022209
case 'K' :
2203-
$this->_WCastleK = true;
2210+
$this->_WCastleK = $wr1 === 'h1' || $wr2 === 'h1';
22042211
break;
22052212
case 'Q' :
2206-
$this->_WCastleQ = true;
2213+
$this->_WCastleQ = $wr1 === 'a1' || $wr2 === 'a1';
22072214
break;
22082215
case 'k' :
2209-
$this->_BCastleK = true;
2216+
$this->_BCastleK = $br1 === 'h8' || $br2 === 'h8';
22102217
break;
22112218
case 'q' :
2212-
$this->_BCastleQ = true;
2219+
$this->_BCastleQ = $br1 === 'a8' || $br2 === 'a8';
22132220
break;
22142221
default:
22152222
return $this->raiseError(self::GAMES_CHESS_ERROR_FEN_CASTLEWRONG,
@@ -3614,8 +3621,8 @@ public function rollbackTransaction()
36143621
public function isBishop($pieceName)
36153622
{
36163623
return $pieceName[1] == 'B' ||
3617-
($pieceName[1] == 'P' &&
3618-
$this->_pieces[$pieceName][1] == 'B');
3624+
($pieceName[1] == 'P' &&
3625+
$this->_pieces[$pieceName][1] == 'B');
36193626
}
36203627

36213628
/**
@@ -3631,8 +3638,8 @@ public function isBishop($pieceName)
36313638
public function isRook($pieceName)
36323639
{
36333640
return $pieceName[1] == 'R' ||
3634-
($pieceName[1] == 'P' &&
3635-
$this->_pieces[$pieceName][1] == 'R');
3641+
($pieceName[1] == 'P' &&
3642+
$this->_pieces[$pieceName][1] == 'R');
36363643
}
36373644

36383645
/**
@@ -3648,7 +3655,7 @@ public function isRook($pieceName)
36483655
public function isPawn($pieceName)
36493656
{
36503657
return $pieceName[1] == 'P' &&
3651-
$this->_pieces[$pieceName][1] == 'P';
3658+
$this->_pieces[$pieceName][1] == 'P';
36523659
}
36533660

36543661
/**
@@ -3674,8 +3681,8 @@ public function isKing($pieceName)
36743681
public function _isKnight($pieceName)
36753682
{
36763683
return $pieceName[1] == 'N' ||
3677-
($pieceName[1] == 'P' &&
3678-
$this->_pieces[$pieceName][1] == 'N');
3684+
($pieceName[1] == 'P' &&
3685+
$this->_pieces[$pieceName][1] == 'N');
36793686
}
36803687

36813688
/**

0 commit comments

Comments
 (0)