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

optimize Divider.mixedness #441

Merged
merged 5 commits into from
Jul 9, 2023
Merged
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
29 changes: 9 additions & 20 deletions src/main/scala/Divider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object Divider:
(Bitboard.firstRank & board.white).count < 4 ||
(Bitboard.lastRank & board.black).count < 4

private def score(white: Int, black: Int, y: Int): Int =
private def score(y: Int)(white: Int, black: Int): Int =
((white, black): @switch) match
case (0, 0) => 0

Expand All @@ -76,26 +76,15 @@ object Divider:

case _ => 0

private val mixednessRegions: List[List[Square]] = {
private val mixednessRegions: List[(Long, Int)] = {
val smallSquare = 0x0303L
for
lenguyenthanh marked this conversation as resolved.
Show resolved Hide resolved
y <- Rank.all.take(7)
x <- File.all.take(7)
yield {
for
dy <- 0 to 1
dx <- 0 to 1
file <- x.offset(dx)
rank <- y.offset(dy)
yield Square(file, rank)
}.toList
y <- 0 to 6
x <- 0 to 6
yield (smallSquare << (x + 8 * y), y + 1)
}.toList

private def mixedness(board: Board): Int =
mixednessRegions.foldLeft(0): (mix, region) =>
var white = 0
var black = 0
region.foreach: s =>
board(s).foreach: v =>
if v is White then white = white + 1
else black = black + 1
mix + score(white, black, region.head.rank.index + 1)
mixednessRegions.foldLeft(0):
case (acc, (region, y)) =>
acc + board.byColor.mapReduce(c => (c & region).count)(score(y))