p5.bitboard
Version:
A bitboard-based addon for p5.quadrille.js to teach and render grid-based logic in p5.js.
299 lines (298 loc) • 9.32 kB
TypeScript
export default Bitboard;
declare class Bitboard {
static VERSION: string;
/**
* Returns a new bitboard with bitwise AND of a and b.
* @param {Bitboard} a
* @param {Bitboard} b
* @returns {Bitboard}
*/
static and(a: Bitboard, b: Bitboard): Bitboard;
/**
* Returns a new bitboard with bitwise OR of a and b.
* @param {Bitboard} a
* @param {Bitboard} b
* @returns {Bitboard}
*/
static or(a: Bitboard, b: Bitboard): Bitboard;
/**
* Returns a new bitboard with bitwise XOR of a and b.
* @param {Bitboard} a
* @param {Bitboard} b
* @returns {Bitboard}
*/
static xor(a: Bitboard, b: Bitboard): Bitboard;
/**
* Returns a new bitboard with bitwise NOT of a.
* @param {Bitboard} a
* @returns {Bitboard}
*/
static not(a: Bitboard): Bitboard;
/**
* Creates a new Bitboard.
* Accepts up to four arguments in any of the following combinations:
* - `bitboard` (bigint or binary string)
* - `bitboard`, `width`
* - `bitboard`, `width`, `height`
* - `bitboard`, `width`, `height`, `littleEndian`
* If `height` is omitted, it is inferred from bit length and width.
* Binary strings may be prefixed with "0b" or passed raw.
* @param {...(bigint|string|number|boolean)} args Bitboard value, dimensions, or endianness.
*/
constructor(...args: (bigint | string | number | boolean)[]);
_bigint: bigint;
_width: number;
_height: number;
_littleEndian: boolean;
_patch(other: any): void;
_create(...args: any[]): Bitboard;
/**
* Creates a deep clone of the bitboard.
* @returns {Bitboard}
*/
clone(): Bitboard;
/**
* Returns current mouse row based on screen position.
* @returns {number}
*/
get mouseRow(): number;
/**
* Returns current mouse column based on screen position.
* @returns {number}
*/
get mouseCol(): number;
/**
* Returns the raw bigint value of the bitboard.
* @returns {bigint}
*/
get bigint(): bigint;
/**
* Returns the number of columns in the bitboard.
* @returns {number}
*/
get width(): number;
/**
* Returns the number of rows in the bitboard.
* @returns {number}
*/
get height(): number;
/**
* Returns whether the bitboard is little-endian.
* @returns {boolean}
*/
get littleEndian(): boolean;
/**
* Returns the total number of cells (width × height).
* @returns {number}
*/
get size(): number;
/**
* Counts how many bits are set to 1.
* @returns {number}
*/
get order(): number;
/**
* Returns true if (row, col) is within the bounds of the bitboard.
* @param {number} row
* @param {number} col
* @returns {boolean}
*/
isValid(row: number, col: number): boolean;
/**
* Lazily iterates in row-major order (top to bottom, left to right)
* over all matching cells in the bitboard.
* The optional `filter` is a predicate function of the form
* `({ row, col, bit }) => boolean` that selects which cells to yield.
* If omitted, all cells are yielded.
* @generator
* @param {Function|null} [filter=null] - Optional predicate function.
* @yields {{ row: number, col: number, bit: 0|1 }}
*/
cells(filter?: Function | null): Generator<{
row: number;
col: number;
bit: number;
}, void, unknown>;
/**
* Iterates over cells using `for...of`, calling the given function with each cell object.
* @param {(cell: { row: number, col: number, bit: 0|1 }) => void} callback - Function to apply to each cell.
* @param {Function|null} [filter] - Optional predicate function for filtering.
*/
visit(callback: (cell: {
row: number;
col: number;
bit: 0 | 1;
}) => void, filter?: Function | null): void;
/**
* Returns the bit index of a cell at (row, col).
* @param {number} row
* @param {number} col
* @returns {number}
*/
index(row: number, col: number): number;
/**
* Converts a bit index to its (row, col) cell position.
* @param {number|bigint} bitIndex
* @returns {{ row: number, col: number }}
*/
cell(bitIndex: number | bigint): {
row: number;
col: number;
};
/**
* Transposes the bitboard (rows become columns).
* @returns {Bitboard}
*/
transpose(): Bitboard;
/**
* Reflects the bitboard vertically.
* @returns {Bitboard}
*/
reflect(): Bitboard;
/**
* Rotates the bitboard 90° clockwise (transpose + reflect).
* @returns {Bitboard}
*/
rotate(): Bitboard;
/**
* Randomly fills or clears bits in available cells.
* @param {number} times - Positive to fill, negative to clear.
* @returns {Bitboard}
*/
rand(times?: number): Bitboard;
/**
* Clears and fills the bitboard randomly with the same number of set bits.
* @returns {Bitboard}
*/
randomize(): Bitboard;
/**
* Fills the entire board, a row, or a single cell.
* - `fill()` fills the entire bitboard.
* - `fill(row)` fills the entire row.
* - `fill(row, col)` fills a single cell.
* @param {...number} args Optional row and column indices.
* @returns {Bitboard}
*/
fill(...args: number[]): Bitboard;
/**
* Clears the entire board, a row, or a single cell.
* - `clear()` clears the entire bitboard.
* - `clear(row)` clears the entire row.
* - `clear(row, col)` clears a single cell.
* @param {...number} args Optional row and column indices.
* @returns {Bitboard}
*/
clear(...args: number[]): Bitboard;
/**
* Toggles the entire board, a row, or a single cell.
* - `toggle()` inverts all bits.
* - `toggle(row)` inverts all bits in the given row.
* - `toggle(row, col)` inverts the bit at (row, col).
* @param {...number} args Optional row and column indices.
* @returns {Bitboard}
*/
toggle(...args: number[]): Bitboard;
/**
* Checks if the cell at (row, col) is filled.
* @param {number} row
* @param {number} col
* @returns {boolean}
*/
isFilled(row: number, col: number): boolean;
/**
* Checks if the cell at (row, col) is empty.
* @param {number} row
* @param {number} col
* @returns {boolean}
*/
isEmpty(row: number, col: number): boolean;
/**
* Returns the bitboard as a binary string.
* @returns {string}
*/
toBinaryString(): string;
/**
* Returns a mask with all valid bits set.
* @returns {bigint}
*/
mask(): bigint;
/**
* Bitwise AND with another bitboard (in place).
* @param {Bitboard} other
* @returns {Bitboard}
*/
and(other: Bitboard): Bitboard;
/**
* Bitwise OR with another bitboard (in place).
* @param {Bitboard} other
* @returns {Bitboard}
*/
or(other: Bitboard): Bitboard;
/**
* Bitwise XOR with another bitboard (in place).
* @param {Bitboard} other
* @returns {Bitboard}
*/
xor(other: Bitboard): Bitboard;
/**
* Bitwise NOT (in place).
* @returns {Bitboard}
*/
not(): Bitboard;
_fit(other: any): Bitboard;
/**
* Returns a new bitboard with a ring of size radius centered at (row, col).
* @param {number} row
* @param {number} col
* @param {number} [radius=1]
* @param {boolean} [wrap=true]
* @returns {Bitboard}
*/
ring(row: number, col: number, radius?: number, wrap?: boolean): Bitboard;
/**
* Performs a left shift on the bitboard.
* @param {boolean} [wrap=true]
* @returns {Bitboard}
*/
shift(wrap?: boolean): Bitboard;
/**
* Slides all filled cells by (dx, dy) across the bitboard grid.
* Optionally wraps around edges when `wrap` is true.
* @param {number} [dx=0] - Horizontal shift (positive is right, negative is left)
* @param {number} [dy=0] - Vertical shift (positive is down, negative is up)
* @param {boolean} [wrap=true] - Whether to wrap cells around edges
* @returns {Bitboard} - The modified bitboard (for chaining)
*/
slide(dx?: number, dy?: number, wrap?: boolean): Bitboard;
/**
* Computes the bounding box of filled cells.
* @returns {{ row: number, col: number, width: number, height: number }|undefined}
*/
bounds(): {
row: number;
col: number;
width: number;
height: number;
} | undefined;
/**
* Crops a sub-region from the bitboard.
* @param {number} row
* @param {number} col
* @param {number} w
* @param {number} h
* @returns {Bitboard}
*/
crop(row: number, col: number, w: number, h: number): Bitboard;
/**
* Default iterator for the bitboard.
* Allows iteration over all cells using `for...of`.
* Equivalent to `this.cells()` with no filter.
* @generator
* @returns {IterableIterator<{ row: number, col: number, bit: 0|1 }>}
*/
[Symbol.iterator](): IterableIterator<{
row: number;
col: number;
bit: 0 | 1;
}>;
}