Class BitBoard
java.lang.Object
ubc.team09.bitboard.BitBoard
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
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.
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 TypeMethodDescriptionstatic voidclear(long[] bitboard) Zeroes a bitboard.static long[]copy(long[] original) Creates a copy of a bitboard.static voidcopyTo(long[] src, long[] dst) Copies thesrcbitboard into thedstbitboard.static intcount(long[] bitboard) Returns the number of flags in the bitboard.static long[]create()Creates a new bitboard for representing a 10x10 board (i.e., there are at least 100 bits).static voidflag(long[] bitboard, byte index) Flags the specified index (i.e., sets it to1in the bitboard).static long[]flagCopy(long[] bitboard, byte index) Returns a copy of the given bitboard, but with the specified index flagged.static booleanflagged(long[] bitboard, byte index) Returnstrueif and only if the indexed bit is flagged (i.e., if it is1in the bitboard).static booleanisEmpty(long[] bitboard) Returnstrueif and only if the bitboard is all zeros.static bytelsb(long[] bitboard) Returns the position index (0-99) of the least-significant bit.static voidmove(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 bytemsb(long[] bitboard) Returns the position index (0-99) of the most-significant bit.static voidnot(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 bytepoll(long[] bitboard) Removes the least-significant bit from the board and returns its index.static voidunflag(long[] bitboard, byte index) Un-flags the specified index (i.e., sets it to0in the bitboard).static long[]unflagCopy(long[] bitboard, byte index) Returns a copy of the given bitboard, but with the specified index un- flagged.
-
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 thesrcbitboard into thedstbitboard. -
flag
public static void flag(long[] bitboard, byte index) Flags the specified index (i.e., sets it to1in 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 to0in 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 inupdated, 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) Returnstrueif and only if the indexed bit is flagged (i.e., if it is1in 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-1is 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-1is returned. -
isEmpty
public static boolean isEmpty(long[] bitboard) Returnstrueif 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.
-