Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only update castle rights when the rook involved can castle #477

Merged
merged 2 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/scala/Move.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ case class Move(
// If a Rook is moved
// Remove that rook from unmovedRooks.
// check the captured rook's side and remove it from castlingRights
if piece is Rook then
if (piece is Rook) && unmovedRooks.contains(orig) then
unmovedRooks.side(orig) match
case Some(result) =>
unmovedRooks = unmovedRooks & ~orig.bl
Expand Down
39 changes: 39 additions & 0 deletions test-kit/src/test/scala/PlayTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,49 @@ package chess

import chess.format.Visual.addNewLines
import chess.Square.*
import chess.format.{ EpdFen, Fen }
import chess.variant.Standard

class PlayTest extends ChessTest:

"playing a game" should:
"preserve castling rights" in:
"only kingside rights" in:
val game = fenToGame(
EpdFen("4k2r/8/8/6R1/6r1/3K4/8/8 b k - 3 4"),
Standard
)
game must beRight.like { game =>
game.playMoves(
G4 -> G2,
G5 -> G8,
G2 -> G8,
D3 -> E3,
G8 -> G5
) must beRight.like { game =>
val fen = Fen write game
fen must_== "4k2r/8/8/6r1/8/4K3/8/8 w k - 2 3"
}
}

"kingside and queenside rights" in:
val game = fenToGame(
EpdFen("r3k2r/8/8/6R1/6r1/3K4/8/8 b kq - 3 4"),
Standard
)
game must beRight.like { game =>
game.playMoves(
G4 -> G2,
G5 -> G8,
G2 -> G8,
D3 -> E3,
G8 -> G5
) must beRight.like { game =>
val fen = Fen write game
fen must_== "r3k2r/8/8/6r1/8/4K3/8/8 w kq - 2 3"
}
}

"opening one" in:
val game =
makeGame.playMoves(E2 -> E4, E7 -> E5, F1 -> C4, G8 -> F6, D2 -> D3, C7 -> C6, C1 -> G5, H7 -> H6)
Expand Down