bibi/bitboard

The bibi/bitboard module provides the ability to create and manipulate bitboards. Bitboards have a defined width and height, and an integer that represents the state of the bitboard when in binary

Suppose you are representing a game of tic-tac-toe that looks like

X | O | _
- + - + -
O | X | _
- + - + -
O | _ | X

Representing the X’s as a bitboard, it would look like

100
010
001

In binary, this would be 001010100, which translates to 84

Notice that the positions of the 1’s when the bitboard is translated into its binary integer format

The following diagram shows how the individual bits are ordered from right to left

6 7 8
3 4 5
0 1 2

Types

pub type Bitboard {
  Bitboard(width: Int, height: Int, val: Int)
}

Constructors

  • Bitboard(width: Int, height: Int, val: Int)

Functions

pub fn antidiagonal(
  bitboard: Bitboard,
  antidiagonal_no: Int,
) -> Result(Bitboard, String)
pub fn bitboard_and(
  bitboard_1: Bitboard,
  bitboard_2: Bitboard,
) -> Result(Bitboard, String)

Bitwise operations

pub fn bitboard_not(bitboard: Bitboard) -> Bitboard
pub fn bitboard_or(
  bitboard_1: Bitboard,
  bitboard_2: Bitboard,
) -> Result(Bitboard, String)
pub fn diagonal(
  bitboard: Bitboard,
  diagonal_no: Int,
) -> Result(Bitboard, String)
pub fn file(
  bitboard: Bitboard,
  file_no: Int,
) -> Result(Bitboard, String)
pub fn flip_horizontally(bitboard: Bitboard) -> Bitboard
pub fn flip_vertically(bitboard: Bitboard) -> Bitboard
pub fn from_base2(
  width: Int,
  height: Int,
  bits: String,
) -> Result(Bitboard, String)

Create a bitboard of a given width and height, and a binary string

I.e from_base2(3, 3, "000000111") –> Bitboard(width: 3, height: 3, val: 7)

pub fn from_coords(
  width: Int,
  height: Int,
  coords: Coords,
) -> Result(Bitboard, String)

Create a bitboard of a given width and height, and a Coords

I.e from_coords(3, 3, Coords(0, 0)) –> Bitboard(width: 3, height: 3, val: 1)

pub fn from_list_of_coords(
  width: Int,
  height: Int,
  coords_list: List(Coords),
) -> Result(Bitboard, String)

Create a bitboard of a given width and height, and a list of Coords

I.e from_coords(3, 3, [Coords(0, 0), Coords(1, 0)]) –> Bitboard(width: 3, height: 3, val: 3)

pub fn from_square(
  width: Int,
  height: Int,
  square: Int,
) -> Result(Bitboard, String)

Create a bitboard of a given with and height, and the nth square

I.e. from_square(3,3, 1) –> Bitboard(width: 3, height: 3, val: 2)

Squares are indexed from bottom left to top right. A 3 by 3 bitboard will be indexed as follows

6 7 8
3 4 5
0 1 2
pub fn full_mask(b: Bitboard) -> Bitboard
pub fn new(width: Int, height: Int) -> Result(Bitboard, String)

Create an empty bitboard of a given width and height

pub fn rank(
  bitboard: Bitboard,
  rank_no: Int,
) -> Result(Bitboard, String)
pub fn shift_east(
  bitboard: Bitboard,
  by i: Int,
) -> Result(Bitboard, String)
pub fn shift_north(
  bitboard: Bitboard,
  by i: Int,
) -> Result(Bitboard, String)
pub fn shift_south(
  bitboard: Bitboard,
  by i: Int,
) -> Result(Bitboard, String)
pub fn shift_west(
  bitboard: Bitboard,
  by i: Int,
) -> Result(Bitboard, String)
pub fn to_bools(b: Bitboard) -> List(Bool)

Convers a bitboard into a list of booleans representing where a square is occupied

I.e to_bools(Bitboard(width: 3, height: 3, val: 3)) –> [True, True, False,... ] (False repeats 7 times in this example)

pub fn to_squares(b: Bitboard) -> List(Int)

Converts a bitboard into a list of integers representing where a square is occupied

I.e to_squares(Bitboard(width: 3, height: 3, val: 3)) –> [0, 1]

pub fn to_string(bitboard: Bitboard) -> String

Converts a bitboard into a width * height, breakpoint separated string of 1s and 0s

I.e. to_string(Bitboard(width: 3, height: 3, val: 2)) –>

000
000
010
Search Document