Skip to content

Commit

Permalink
Clean up duplicated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jul 20, 2023
1 parent e4df332 commit e47001e
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/File.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ opaque type File = Int
object File:

extension (a: File)
inline def index: Int = a
// inline def index: Int = a
inline def value: Int = a

inline infix def >(inline o: File): Boolean = a > o
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/Hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object Hash extends OpaqueInt[Hash]:
else hpieces

val hep = situation.enPassantSquare.fold(hcastling): square =>
hcastling ^ table.enPassantMasks(square.file.index)
hcastling ^ table.enPassantMasks(square.file.value)

// Hash in special three-check data.
val hchecks = board.variant match
Expand Down
1 change: 0 additions & 1 deletion src/main/scala/Rank.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ object Rank:
extension (a: Rank)

inline def value: Int = a
inline def index: Int = a

inline infix def >(inline o: Rank): Boolean = a > o
inline infix def <(inline o: Rank): Boolean = a < o
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/Situation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,13 @@ case class Situation(board: Board, color: Color):

val s2: List[Move] = for
to <- singleMoves & mask
from <- Square.at(to.value + (if isWhiteTurn then -8 else 8)).toList
from <- Square(to.value + (if isWhiteTurn then -8 else 8)).toList
move <- genPawnMoves(from, to, false)
yield move

val s3: List[Move] = for
to <- doubleMoves & mask
from <- Square.at(to.value + (if isWhiteTurn then -16 else 16))
from <- Square(to.value + (if isWhiteTurn then -16 else 16))
move <- normalMove(from, to, Pawn, false)
yield move

Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/Square.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,11 @@ object Square:
end extension

inline def apply(inline file: File, inline rank: Rank): Square = file.value + 8 * rank.value

inline def apply(index: Int): Option[Square] = Option.when(0 <= index && index < 64)(index)
private[chess] def unsafe(index: Int): Square = index

inline def at(index: Int): Option[Square] = Option.when(0 <= index && index < 64)(index)
// inline def at(index: Int): Option[Square] = Option.when(0 <= index && index < 64)(index)

inline def at(x: Int, y: Int): Option[Square] = Option.when(0 <= x && x < 8 && 0 <= y && y < 8)(x + 8 * y)

Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/bitboard/Bitboard.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ object Bitboard:
def count: Int = java.lang.Long.bitCount(a)

// the first non empty square (the least significant bit/ the rightmost bit)
def first: Option[Square] = Square.at(java.lang.Long.numberOfTrailingZeros(a))
def first: Option[Square] = Square(java.lang.Long.numberOfTrailingZeros(a))

// the last non empty square (the most significant bit / the leftmost bit)
def last: Option[Square] = Square.at(63 - java.lang.Long.numberOfLeadingZeros(a))
def last: Option[Square] = Square(63 - java.lang.Long.numberOfLeadingZeros(a))

// remove the first non empty position
def removeFirst: Bitboard = a & (a - 1L)
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/format/FenWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import ornicar.scalalib.zeros.given
*/
trait FenWriter:

private given Ordering[File] = Ordering.by[File, Int](_.index)
private given Ordering[File] = Ordering.by[File, Int](_.value)
given Ordering[Square] = Ordering.by[Square, File](_.file)

def write(situation: Situation): EpdFen = write(Situation.AndFullMoveNumber(situation, FullMoveNumber(1)))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/format/UciCharPair.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ object UciCharPair:
val promotion2charMap: Map[(File, PromotableRole), Char] = for
(role, index) <- Role.allPromotable.zipWithIndex.toMap
file <- File.all
yield (file, role) -> (charShift + square2charMap.size + index * 8 + file.index).toChar
yield (file, role) -> (charShift + square2charMap.size + index * 8 + file.value).toChar

lazy val char2promotionMap: Map[Char, (File, PromotableRole)] =
promotion2charMap.map(_.swap)
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/format/pgn/parsingModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ case class Std(
situation.board
.byPiece(situation.color - role)
.first: square =>
if compare(file, square.file.index) &&
compare(rank, square.rank.index)
if compare(file, square.file.value) &&
compare(rank, square.rank.value)
then situation.generateMovesAt(square).find(_.dest == dest)
else None
.toRight(ErrorStr(s"No move found: $this\n$situation"))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/variant/Horde.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ case object Horde
yield (Square(x, y) -> White.pawn)
val frontPawns = List(Square.B5, Square.C5, Square.F5, Square.G5).map { _ -> White.pawn }
val blackPawns = File.all.map { Square(_, Rank.Seventh) -> Black.pawn }
val blackPieces = File.all.map { x => Square(x, Rank.Eighth) -> (Black - backRank(x.index)) }
val blackPieces = File.all.map { x => Square(x, Rank.Eighth) -> (Black - backRank(x.value)) }
whitePawnsHorde ++ frontPawns ++ blackPawns ++ blackPieces toMap

override val castles = Castles("kq")
Expand Down

0 comments on commit e47001e

Please sign in to comment.