MagicBitboards

dicechess.engine.movegen.MagicBitboards

Magic Bitboards implementation for sliding pieces (Bishops, Rooks, and Queens).

Magic Bitboards provide O(1) attack-set lookup for blocking sliding pieces by encoding the current board occupancy into an index via a precomputed magic number multiplier:

index = ((occupancy & relevantMask) * magic) >>> (64 - relevantBits)
attack = table[offset + index]

When the object is first used, BishopTable (5248 entries) and RookTable (102400 entries) are populated for all 64 squares from the slow bishopAttacksClassic / rookAttacksClassic functions, run once per blocker subset of each line through a square rather than once per entry (see fillSlice). All subsequent attack lookups use the fast O(1) hash path.

The magic constants are a well-known standard set compatible with most chess engine implementations.

Attributes

Source
MagicBitboards.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type

Members list

Value members

Concrete methods

def bishopAttacks(sq: Square, occupancy: Bitboard): Bitboard

Performs an O(1) Magic lookup for all squares attacked or reachable by a Bishop.

Performs an O(1) Magic lookup for all squares attacked or reachable by a Bishop.

Masks the board occupancy, hashes it to an index using BishopMagics, and returns the precomputed attack bitboard from BishopTable.

Value parameters

occupancy

the full board piece occupancy dicechess.engine.domain.Bitboard

sq

the source dicechess.engine.domain.Square of the Bishop

Attributes

Returns

a dicechess.engine.domain.Bitboard of all attacked or reachable squares

Source
MagicBitboards.scala

Computes Bishop attacks step-by-step along its four diagonals.

Computes Bishop attacks step-by-step along its four diagonals.

This slow, non-optimized method is used exclusively during engine startup to populate the BishopTable. It slides in each direction, adding squares to the attack bitboard until it hits the board edge or another piece.

Value parameters

occupancy

the full board piece occupancy dicechess.engine.domain.Bitboard

sq

the source dicechess.engine.domain.Square of the Bishop

Attributes

Returns

a dicechess.engine.domain.Bitboard containing all squares the Bishop can attack or reach

Source
MagicBitboards.scala

Generates the candidate blocker mask for a Bishop on a given square.

Generates the candidate blocker mask for a Bishop on a given square.

In Magic Bitboards, the edges of the board are excluded from the mask because blockers on the border cannot prevent a sliding piece from reaching the border itself.

Value parameters

sq

the dicechess.engine.domain.Square position of the Bishop

Attributes

Returns

a dicechess.engine.domain.Bitboard containing all candidate blocker squares (edges excluded)

Source
MagicBitboards.scala
def queenAttacks(sq: Square, occupancy: Bitboard): Bitboard

Returns all squares attacked or reachable by a Queen by combining Rook and Bishop attacks.

Returns all squares attacked or reachable by a Queen by combining Rook and Bishop attacks.

Value parameters

occupancy

the full board piece occupancy dicechess.engine.domain.Bitboard

sq

the source dicechess.engine.domain.Square of the Queen

Attributes

Returns

a dicechess.engine.domain.Bitboard containing the union of Bishop and Rook attack paths

Source
MagicBitboards.scala
def rookAttacks(sq: Square, occupancy: Bitboard): Bitboard

Performs an O(1) Magic lookup for all squares attacked or reachable by a Rook.

Performs an O(1) Magic lookup for all squares attacked or reachable by a Rook.

Masks the board occupancy, hashes it to an index using RookMagics, and returns the precomputed attack bitboard from RookTable.

Value parameters

occupancy

the full board piece occupancy dicechess.engine.domain.Bitboard

sq

the source dicechess.engine.domain.Square of the Rook

Attributes

Returns

a dicechess.engine.domain.Bitboard of all attacked or reachable squares

Source
MagicBitboards.scala
def rookAttacksClassic(sq: Square, occupancy: Bitboard): Bitboard

Computes Rook attacks step-by-step along its ranks and files.

Computes Rook attacks step-by-step along its ranks and files.

This slow, non-optimized method is used exclusively during engine startup to populate the RookTable. It slides horizontally and vertically, adding squares until it hits the board edge or another piece.

Value parameters

occupancy

the full board piece occupancy dicechess.engine.domain.Bitboard

sq

the source dicechess.engine.domain.Square of the Rook

Attributes

Returns

a dicechess.engine.domain.Bitboard containing all squares the Rook can attack or reach

Source
MagicBitboards.scala

Generates the candidate blocker mask for a Rook on a given square.

Generates the candidate blocker mask for a Rook on a given square.

Excludes the outermost rank or file in each direction because a Rook's path cannot be blocked beyond the first blocker, and a blocker on the final edge square cannot change what squares the Rook can reach.

Value parameters

sq

the dicechess.engine.domain.Square position of the Rook

Attributes

Returns

a dicechess.engine.domain.Bitboard containing all candidate blocker squares (edges excluded)

Source
MagicBitboards.scala

Concrete fields

val BishopRelevantBits: Array[Int]

Number of relevant occupancy bits per square for bishop attack masks.

Number of relevant occupancy bits per square for bishop attack masks.

Ranges from 5 (corner squares, few diagonals) to 9 (central squares, long diagonals). Used to size the hash table slice for each square.

Attributes

Source
MagicBitboards.scala
val RookRelevantBits: Array[Int]

Number of relevant occupancy bits per square for rook attack masks.

Number of relevant occupancy bits per square for rook attack masks.

Ranges from 10 (non-corner edge squares) to 12 (corner squares, long files/ranks). Used to size the hash table slice for each square.

Attributes

Source
MagicBitboards.scala