Skip to content

Commit

Permalink
Merge pull request #583 from lenguyenthanh/improve-core-arbitrary
Browse files Browse the repository at this point in the history
Improve core arbitrary
  • Loading branch information
lenguyenthanh authored Oct 9, 2024
2 parents d1a820c + ebc6623 commit 80793a7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions core/src/main/scala/File.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package chess

import chess.bitboard.Bitboard

opaque type File = Int
object File:

Expand All @@ -15,6 +17,9 @@ object File:

inline def upperCaseChar: Char = (65 + a).toChar
inline def toUpperCaseString: String = upperCaseChar.toString

// the bitboard of the file
def bb: Bitboard = Bitboard.file(value)
end extension

inline def of(inline square: Square): File = square.value & 0x7
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/Rank.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package chess

import chess.bitboard.Bitboard

opaque type Rank = Int
object Rank:
extension (a: Rank)
Expand All @@ -11,6 +13,9 @@ object Rank:
inline infix def <=(inline o: Rank): Boolean = value <= o.value

inline def char: Char = (49 + a).toChar

// the bitboard of the rank
def bb: Bitboard = Bitboard.rank(value)
end extension

inline def apply(index: Int): Option[Rank] = Option.when(0 <= index && index < 8)(index)
Expand Down
42 changes: 41 additions & 1 deletion test-kit/src/main/scala/chess/CoreArbitraries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package chess
import chess.bitboard.Bitboard
import chess.format.pgn.{ Glyph, Glyphs }
import chess.format.{ Uci, UciCharPair }
import chess.variant.Variant
import chess.variant.{ Crazyhouse, Variant }
import org.scalacheck.{ Arbitrary, Cogen, Gen }

object CoreArbitraries:
Expand All @@ -28,6 +28,8 @@ object CoreArbitraries:
given Arbitrary[Castles] = Arbitrary(castlesGen)
given Arbitrary[Bitboard] = Arbitrary(Gen.long.map(Bitboard(_)))

given Arbitrary[PromotableRole] = Arbitrary(Gen.oneOf(Rook, Knight, Bishop, Queen))

given Arbitrary[Uci] = Arbitrary(Gen.oneOf(normalUciMoveGen, promotionUciMoveGen, dropUciMoveGen))

given Cogen[Color] = Cogen.cogenBoolean.contramap(_.white)
Expand All @@ -40,6 +42,21 @@ object CoreArbitraries:
b <- Arbitrary.arbitrary[A]
yield ByColor(w, b)

given Arbitrary[Crazyhouse.Pocket] = Arbitrary:
for
pawn <- Gen.oneOf(0 to 8)
knight <- Gen.oneOf(0 to 2)
bishop <- Gen.oneOf(0 to 2)
rook <- Gen.oneOf(0 to 2)
queen <- Gen.oneOf(0 to 2)
yield Crazyhouse.Pocket(pawn, knight, bishop, rook, queen)

given Arbitrary[Crazyhouse.Data] = Arbitrary:
for
pockets <- Arbitrary.arbitrary[ByColor[Crazyhouse.Pocket]]
promoted <- Arbitrary.arbitrary[Bitboard]
yield Crazyhouse.Data(pockets, promoted)

def normalUciMoveGen =
for
orig <- Arbitrary.arbitrary[Square]
Expand Down Expand Up @@ -71,3 +88,26 @@ object CoreArbitraries:
bks <- genBool
bqs <- genBool
yield Castles(wks, wqs, bks, bqs)

private def squareFromBitboardGen(bb: Bitboard): Gen[Square] =
Gen.oneOf(bb.squares)

private def corner(side: Side, color: Color): Square =
side.fold(
color.fold(Square.H1, Square.H8),
color.fold(Square.A1, Square.A8)
)

def castleGen(color: Color): Gen[Move.Castle] =
import File.*
for
side <- Arbitrary.arbitrary[Side]
kingTo = Square(side.castledKingFile, color.backRank)
rookTo = Square(side.castledRookFile, color.backRank)
king <- squareFromBitboardGen(color.backRank.bb & (~kingTo.bb & ~H.bb & ~A.bb))
cornerSquare = corner(side, color)
rook <- squareFromBitboardGen(Bitboard.ray(king, cornerSquare))
yield Move.Castle(king, kingTo, rook, rookTo)

given Arbitrary[Move.Castle] = Arbitrary:
Arbitrary.arbitrary[Color].flatMap(castleGen)

0 comments on commit 80793a7

Please sign in to comment.