From 3a2ecc62009674fd18f4fec32246c369a4122857 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Sun, 9 Jul 2023 09:53:38 +0200 Subject: [PATCH] optimize Divider.mixedness --- src/main/scala/Divider.scala | 32 +++++++++----------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/main/scala/Divider.scala b/src/main/scala/Divider.scala index d8e151959..297252122 100644 --- a/src/main/scala/Divider.scala +++ b/src/main/scala/Divider.scala @@ -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): @@ -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 @@ -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