Class BitBoard

java.lang.Object
ubc.team09.bitboard.BitBoard

public class BitBoard extends Object
A bitboard is a means of representing a board such that each square is represented by a single bit. This obviously only allows for binary state, but multiple bitboards can be used to represent more nuanced board data. E.g., you could have one bitboard that flags all queens, one that flags all arrows, etc.

In typical chess games, bitboards offer a tradeoff: extremely fast bit operations against increased memory consumption (due to the number of bitboards necessary to represent a complex chess state). In Amazons, the math is a little different. Bitboards in Amazons generally use less memory because there's only one kind of piece and only 8 of them, but their operations aren't quite as fast as chess operations because the 10x10 board cannot be represented by a single long.

Since bitboards are meant to be performant, this class is not meant to wrap a bitboard with a neat interface (and thereby discard sone of the performance gain); instead, it simply consists of a number of static utility functions useful for creating and interacting with bitboards.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    clear(long[] bitboard)
    Zeroes a bitboard.
    static long[]
    copy(long[] original)
    Creates a copy of a bitboard.
    static void
    copyTo(long[] src, long[] dst)
    Copies the src bitboard into the dst bitboard.
    static int
    count(long[] bitboard)
    Returns the number of flags in the bitboard.
    static long[]
    Creates a new bitboard for representing a 10x10 board (i.e., there are at least 100 bits).
    static void
    flag(long[] bitboard, byte index)
    Flags the specified index (i.e., sets it to 1 in the bitboard).
    static long[]
    flagCopy(long[] bitboard, byte index)
    Returns a copy of the given bitboard, but with the specified index flagged.
    static boolean
    flagged(long[] bitboard, byte index)
    Returns true if and only if the indexed bit is flagged (i.e., if it is 1 in the bitboard).
    static boolean
    isEmpty(long[] bitboard)
    Returns true if and only if the bitboard is all zeros.
    static byte
    lsb(long[] bitboard)
    Returns the position index (0-99) of the least-significant bit.
    static void
    move(long[] initial, byte src, byte dst, long[] updated)
    Moves a flag from one position to another.
    static long[]
    moveCopy(long[] bitboard, byte src, byte dst)
    Moves a flag from one position to another.
    static byte
    msb(long[] bitboard)
    Returns the position index (0-99) of the most-significant bit.
    static void
    not(long[] bitboard)
    Inverts all bits on the board.
    static long[]
    notCopy(long[] bitboard)
    Returns a new bitboard with all the bits opposite of the original, which is left unchanged.
    static byte
    poll(long[] bitboard)
    Removes the least-significant bit from the board and returns its index.
    static void
    unflag(long[] bitboard, byte index)
    Un-flags the specified index (i.e., sets it to 0 in the bitboard).
    static long[]
    unflagCopy(long[] bitboard, byte index)
    Returns a copy of the given bitboard, but with the specified index un- flagged.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static long[] create()
      Creates a new bitboard for representing a 10x10 board (i.e., there are at least 100 bits).
    • copy

      public static long[] copy(long[] original)
      Creates a copy of a bitboard.
    • copyTo

      public static void copyTo(long[] src, long[] dst)
      Copies the src bitboard into the dst bitboard.
    • flag

      public static void flag(long[] bitboard, byte index)
      Flags the specified index (i.e., sets it to 1 in the bitboard).
    • flagCopy

      public static long[] flagCopy(long[] bitboard, byte index)
      Returns a copy of the given bitboard, but with the specified index flagged.
    • unflag

      public static void unflag(long[] bitboard, byte index)
      Un-flags the specified index (i.e., sets it to 0 in the bitboard).
    • unflagCopy

      public static long[] unflagCopy(long[] bitboard, byte index)
      Returns a copy of the given bitboard, but with the specified index un- flagged.
    • moveCopy

      public static long[] moveCopy(long[] bitboard, byte src, byte dst)
      Moves a flag from one position to another. This function creates and returns a copy that reflects the new board state, rather than mutating the original.
      Parameters:
      bitboard - The original bitboard. This will be unchanged.
      src - The position index to remove the bit from.
      dst - The position index to move the bit to.
      Returns:
      A copy of the original bitboard with the move applied.
    • move

      public static void move(long[] initial, byte src, byte dst, long[] updated)
      Moves a flag from one position to another. This function will reflect the updated boardstate in updated, rather than mutating the original bitboard.
      Parameters:
      initial - The original bitboard. This will be unchanged.
      src - The position index to remove the bit from.
      dst - The position index to move the bit to.
      updated - The bitboard that will reflect the new state.
    • flagged

      public static boolean flagged(long[] bitboard, byte index)
      Returns true if and only if the indexed bit is flagged (i.e., if it is 1 in the bitboard).
    • clear

      public static void clear(long[] bitboard)
      Zeroes a bitboard.
    • not

      public static void not(long[] bitboard)
      Inverts all bits on the board.
    • notCopy

      public static long[] notCopy(long[] bitboard)
      Returns a new bitboard with all the bits opposite of the original, which is left unchanged.
    • lsb

      public static byte lsb(long[] bitboard)
      Returns the position index (0-99) of the least-significant bit. If there is no such bit (i.e., the bitboard is zero), then -1 is returned.
    • msb

      public static byte msb(long[] bitboard)
      Returns the position index (0-99) of the most-significant bit. If there is no such bit (i.e., the bitboard is zero), then -1 is returned.
    • isEmpty

      public static boolean isEmpty(long[] bitboard)
      Returns true if and only if the bitboard is all zeros.
    • poll

      public static byte poll(long[] bitboard)
      Removes the least-significant bit from the board and returns its index. If the bitboard is empty, this returns -1.
    • count

      public static int count(long[] bitboard)
      Returns the number of flags in the bitboard. The bitboard will not be mutated.