Skip to content
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
3 changes: 2 additions & 1 deletion src/main/scala/Castles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ object Castles:
@targetName("orB")
inline infix def |(o: Bitboard): Castles = c | o.value

def value: Long = c
def value: Long = c
def display: String = Bitboard(c).display // TODO: override tostring if possible
def contains(square: Square): Boolean =
(c & (1L << square.value)) != 0L

Expand Down
5 changes: 2 additions & 3 deletions src/main/scala/Move.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ case class Move(
after updateHistory { h1 =>
val h2 = h1.copy(
lastMove = Option(toUci),
unmovedRooks = before.unmovedRooks,
halfMoveClock =
if piece.is(Pawn) || captures || promotes then HalfMoveClock.initial
else h1.halfMoveClock + 1
)

var castleRights: Castles = h2.castles
var unmovedRooks: UnmovedRooks = h2.unmovedRooks
var castleRights: Castles = h1.castles
var unmovedRooks: UnmovedRooks = h1.unmovedRooks

// If the rook is captured
// remove the captured rook from unmovedRooks
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/bitboard/Bitboard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,16 @@ object Bitboard:
builder += f(b.lsb)
b &= (b - 1L)
builder.result

// TODO: nice to have, faster.
// but should only be used for debug
// TODO: override toString?
def display: String =
val builder = StringBuilder()
Rank.allReversed.foreach: r =>
File.all.foreach: f =>
val s = Square(f, r)
builder ++= (if contains(s) then "1" else ".")
if f != File.H then builder ++= " "
else if s != Square.H1 then builder ++= "\n"
builder.result
11 changes: 10 additions & 1 deletion test-kit/src/test/scala/AtomicVariantTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chess
import cats.syntax.all.*

import chess.variant.Atomic
import chess.format.EpdFen
import chess.format.{ EpdFen, Fen }
import chess.format.pgn.SanStr

class AtomicVariantTest extends ChessTest:
Expand Down Expand Up @@ -452,3 +452,12 @@ class AtomicVariantTest extends ChessTest:
.playMove(Square.B8, Square.B2)
.assertRight: game =>
assert(game.situation.legalMoves.filter(_.castles).isEmpty)

test("Unmoved rooks correctly updated after explosion, lila issue-14544"):
val sans: Vector[SanStr] =
SanStr from "e4 d5 d4 e6 Nc3 b5 Bg5 f6 Bh6 Ba3 Bxg7 h5 bxa3 c5 Qc1 Qe7 Qh6 Qg7 Qh8+ Qxh8 Rb1 cxd4 Bxb5 Nd7 Rb7 Kf8 Rxd7 Rb8 Ne2 Rb1+ Nc1 d4 O-O"
.split(' ')
.toVector
val (game, steps, error) = chess.Replay.gameMoveWhileValid(sans, Atomic.initialFen, Atomic)
assertEquals(error, None)
assertEquals(steps.size, sans.size)
17 changes: 16 additions & 1 deletion test-kit/src/test/scala/UnmovedRooksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package chess

import scala.language.implicitConversions
import Square.*
import variant.Chess960
import variant.{ Atomic, Chess960 }
import format.EpdFen
import chess.format.Fen
import cats.syntax.all.*
Expand Down Expand Up @@ -83,6 +83,21 @@ class UnmovedRooksTest extends ChessTest:
assert(g.board.history.unmovedRooks.contains(G8))
assertEquals(g.board.castles, Castles(false, true, true, true))

test("Atomic: explode an unmovedRook"):
fenToGame(
EpdFen("rnbqk1nr/p1p3pp/4pp1B/1p1p4/3PP3/b1N5/PPP2PPP/R2QKBNR w KQkq - 2 6"),
Atomic
).playMoves(H6 -> G7)
.assertRight: g =>
assert(g.board.history.unmovedRooks.contains(A1))
assert(g.board.history.unmovedRooks.contains(H1))
assert(g.board.history.unmovedRooks.contains(A8))
assertNot(g.board.history.unmovedRooks.contains(H8))
assertEquals(
g.board.castles,
Castles(whiteKingSide = true, whiteQueenSide = true, blackKingSide = false, blackQueenSide = true)
)

test("An unmovedRooks moves, white"):
fenToGame(EpdFen("qrnbkrbn/ppppp1pp/8/5p2/5P2/8/PPPPP1PP/QRNBKRBN w KQkq - 0 2"), Chess960)
.playMoves(F1 -> F2)
Expand Down