Skip to content

Commit

Permalink
optimize Divider.mixedness
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jul 9, 2023
1 parent 59b8abd commit 3a2ecc6
Showing 1 changed file with 9 additions and 23 deletions.
32 changes: 9 additions & 23 deletions src/main/scala/Divider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chess

import cats.syntax.all.*
import bitboard.Bitboard
import bitboard.Bitboard.bb
import scala.annotation.switch

case class Division(middle: Option[Ply], end: Option[Ply], plies: Ply):
Expand Down Expand Up @@ -50,7 +51,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 +77,11 @@ object Divider:

case _ => 0

private val mixednessRegions: List[List[Square]] = {
private def mixedness(board: Board): Int = {
val smallSquare = 0x0303L.bb
for
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
}.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)
y <- 0 to 7
x <- 0 to 7
region = smallSquare << (x + 8 * y)
yield board.byColor.map(c => (c & region).count).reduce(score(y))
}.sum

0 comments on commit 3a2ecc6

Please sign in to comment.