Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/main/scala/game/Board.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ final class Board(

val king = getKing(this.turn)
val blockers = sliderBlockers(king)
moves.anyMatch(isSafe(king, _, blockers))
moves.exists(isSafe(king, _, blockers))

private def genNonKing(mask: Long, moves: MoveList): Unit =
genPawn(mask, moves)
Expand Down
21 changes: 6 additions & 15 deletions src/main/scala/game/Encoder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,12 @@ object Encoder:
if matcher.group(5) != null then promotion = charToRole(matcher.group(5).charAt(0))

board.legalMoves(legals)
legals.sort()
var foundMatch = false
for i <- 0 until legals.getSize() do
val legal = legals.get(i)
if legal.role == role && legal.to == to && legal.promotion == promotion && Bitboard.contains(
from,
legal.from
)
then
if foundMatch then return null
Huffman.write(i, writer)
board.play(legal)
foundMatch = true

if !foundMatch then return null
legals.find(legal => legal.role == role && legal.to == to && legal.promotion == promotion && Bitboard.contains(from, legal.from)) match
case None =>
return null
case Some(move) =>
Huffman.write(legals.rank(move), writer)
board.play(move)

writer.toArray()
end encode
Expand Down
10 changes: 7 additions & 3 deletions src/main/scala/game/MoveList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ final class MoveList(capacity: Int = 256):
buffer(size).set(board, Move.EN_PASSANT, Role.PAWN, capturer, true, to, null)
size += 1

// Cast is needed because scala arrays are not covariant.
def sort(): Unit = java.util.Arrays.sort(buffer.asInstanceOf[Array[Object]], 0, size)
def find(predicate: Move => Boolean): Option[Move] =
buffer.view.take(size).find(predicate)

def anyMatch(predicate: Move => Boolean): Boolean = buffer.take(size).exists(predicate)
def exists(predicate: Move => Boolean): Boolean =
buffer.view.take(size).exists(predicate)

def rank(move: Move): Int =
buffer.view.take(size).count(_ < move)

def retain(predicate: Move => Boolean): Unit =
var i = 0
Expand Down