Skip to content

Commit

Permalink
Code tweak & clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jul 16, 2023
1 parent bdda4fa commit 7b931eb
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 39 deletions.
8 changes: 0 additions & 8 deletions src/main/scala/Castles.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ object Castles:
inline infix def ^(inline o: Long): Castles = c ^ o
inline infix def |(inline o: Long): Castles = c | o

@targetName("and")
inline infix def &(o: Castles): Castles = c & o
@targetName("xor")
inline infix def ^(o: Castles): Castles = c ^ o
@targetName("or")
inline infix def |(o: Castles): Castles = c | o

@targetName("andB")
inline infix def &(o: Bitboard): Castles = c & o.value
@targetName("xorB")
Expand Down Expand Up @@ -112,5 +105,4 @@ object Castles:
case _ => None

val all: Castles = 0x8100000000000081L
val init: Castles = all
val none: Castles = 0L
4 changes: 2 additions & 2 deletions src/main/scala/Situation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,9 @@ case class Situation(board: Board, color: Color):
// can castle but which side?
if !history.castles.can(color) || king.rank != color.backRank then Nil
else
val rooks = history.unmovedRooks & Bitboard.rank(color.backRank).value & board.rooks.value
val rooks = Bitboard.rank(color.backRank) & board.rooks & history.unmovedRooks.value
for
rook <- rooks.bb
rook <- rooks
toKingFile = if rook < king then File.C else File.G
toRookFile = if rook < king then File.D else File.F
kingTo = Square(toKingFile, king.rank)
Expand Down
14 changes: 3 additions & 11 deletions src/main/scala/UnmovedRooks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ object UnmovedRooks:
val default: UnmovedRooks = UnmovedRooks(Bitboard.rank(Rank.First) | Bitboard.rank(Rank.Eighth))
val corners: UnmovedRooks = 0x8100000000000081L
val none: UnmovedRooks = 0L
val empty: UnmovedRooks = 0L

@targetName("applyUnmovedRooks")
def apply(b: Bitboard): UnmovedRooks = b.value
def apply(l: Long): UnmovedRooks = l
inline def apply(inline xs: Iterable[Square]): UnmovedRooks = xs.foldLeft(empty)((b, s) => b | s.bl)
inline def apply(inline xs: Iterable[Square]): UnmovedRooks = xs.foldLeft(none)((b, s) => b | s.bl)

// guess unmovedRooks from board
// we assume rooks are on their initial position
Expand Down Expand Up @@ -55,15 +54,8 @@ object UnmovedRooks:
inline infix def |(inline o: Long): UnmovedRooks = ur | o

@targetName("and")
inline infix def &(o: UnmovedRooks): UnmovedRooks = ur & o
@targetName("xor")
inline infix def ^(o: UnmovedRooks): UnmovedRooks = ur ^ o
@targetName("or")
inline infix def |(o: UnmovedRooks): UnmovedRooks = ur | o

@targetName("andB")
inline infix def &(o: Bitboard): UnmovedRooks = ur & o.value
@targetName("xorB")
@targetName("xor")
inline infix def ^(o: Bitboard): UnmovedRooks = ur ^ o.value
@targetName("orB")
@targetName("or")
inline infix def |(o: Bitboard): UnmovedRooks = ur | o.value
22 changes: 9 additions & 13 deletions src/main/scala/bitboard/Bitboard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,24 @@ object Bitboard:
KNIGHT_ATTACKS(s.value)

private def distance(a: Int, b: Int): Int =
inline def file(p: Int) = p & 7
inline def rank(p: Int) = p >>> 3
inline def file(s: Int) = s & 7
inline def rank(s: Int) = s >>> 3
Math.max(Math.abs(file(a) - file(b)), Math.abs(rank(a) - rank(b)))

extension (a: Bitboard)
inline def value: Long = a
inline def unary_~ : Bitboard = (~a)
inline infix def &(inline o: Long): Bitboard = (a & o)
inline infix def ^(inline o: Long): Bitboard = (a ^ o)
inline infix def |(inline o: Long): Bitboard = (a | o)
inline infix def <<(inline o: Long): Bitboard = (a << o)
inline infix def >>>(inline o: Long): Bitboard = (a >>> o)
inline def value: Long = a
inline def unary_~ : Bitboard = (~a)
inline infix def &(inline o: Long): Bitboard = (a & o)
inline infix def ^(inline o: Long): Bitboard = (a ^ o)
inline infix def |(inline o: Long): Bitboard = (a | o)
inline infix def <<(inline o: Int): Bitboard = (a << o)
inline infix def >>>(inline o: Int): Bitboard = (a >>> o)
@targetName("and")
inline infix def &(o: Bitboard): Bitboard = (a & o)
@targetName("xor")
inline infix def ^(o: Bitboard): Bitboard = (a ^ o)
@targetName("or")
inline infix def |(o: Bitboard): Bitboard = (a | o)
@targetName("shiftLeft")
inline infix def <<(o: Bitboard): Bitboard = (a << o)
@targetName("shiftRight")
inline infix def >>>(o: Bitboard): Bitboard = (a >>> o)

def contains(square: Square): Boolean =
(a & (1L << square.value)) != 0L
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/format/FenReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ trait FenReader:
val situation = Situation(board, if variant.atomic then fen.color else board.checkColor | fen.color)
// todo verify unmovedRooks vs board.rooks
val (castles, unmovedRooks) =
if !variant.allowsCastling then (Castles.none -> UnmovedRooks.empty)
if !variant.allowsCastling then (Castles.none -> UnmovedRooks.none)
else
fen.castling
.foldLeft(Castles.none -> UnmovedRooks.none) { case ((c, r), ch) =>
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/variant/Atomic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ case object Atomic

val rooksToExploded = squaresToExplode & afterBoard.rooks

val castles = afterBoard.castles & ~rooksToExploded.value
val unMovedRooks = afterBoard.unmovedRooks & ~rooksToExploded.value
val castles = afterBoard.castles & ~rooksToExploded
val unMovedRooks = afterBoard.unmovedRooks & ~rooksToExploded
val newBoard = afterExplosions.updateHistory(_.copy(castles = castles, unmovedRooks = unMovedRooks))
move withAfter newBoard
else move
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/variant/Standard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ case object Standard
// Checks by these sliding pieces can maybe be blocked.
val sliders = checkers & (board.sliders)
val attacked =
sliders.fold(Bitboard.empty)((a, s) => a | (Bitboard.ray(king, s) ^ s.bb))
sliders.fold(Bitboard.empty)((a, s) => a | (Bitboard.ray(king, s) ^ s.bl))
val safeKings = genSafeKing(~us & ~attacked)
val blockers =
checkers.singleSquare.map(c => genNonKing(Bitboard.between(king, c) | checkers)).getOrElse(Nil)
Expand Down
1 change: 0 additions & 1 deletion src/test/scala/UnmovedRooksTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import chess.format.Fen
import cats.syntax.all.*

import bitboard.Board as BBoard
import bitboard.Bitboard.contains

class UnmovedRooksTest extends ChessTest:

Expand Down

0 comments on commit 7b931eb

Please sign in to comment.