1.0.6 • Published 5 years ago

bitboards v1.0.6

Weekly downloads
3
License
ISC
Repository
github
Last release
5 years ago

JavaScript & TypeScript BitBoard Classes

Build Status

For calculations in boardgames. Contains an all purpose class, and two sub classes for chess and connect four
Has been extensively tested in node.js.

Installation and Usage

npm install --save bitboards
<script src="https://unpkg.com/bitboards@1.0.6/bitboards.min.js"></script>

Importing

node.js

// Node
const { BitBoard, ChessBitBoard, ConnectFourBitBoard } = require('bitboards');
const board = new BitBoard();

modules

import { BitBoard, ChessBitBoard, ConnectFourBitBoard } from 'bitboards';
const board = new BitBoard();

unpkg

// index.html
<script src="https://unpkg.com/bitboards@1.0.6/bitboards.min.js"></script>

// file.js
const board = new BitBoard();

BitBoard

NOTE

The last parameter to all binary operator methods is an optional boolean flag modify. When true the BitBoard calling the method will be modified as opposed to leaving the calling BitBoard unchanged and returning a new BitBoard. If a BitBoard is an argument to a method it is ALWAYS left unchanged.

Constructor BitBoard( optional array of two integers )

  • Takes in an array of two numbers n where 0 <= n <= 2 ^ 32 - 1.
  • Or a binary string of up to 64 zeros and ones

Array Input

let boardA = new BitBoard();
let boardB = new BitBoard([Math.pow(2,32) - 1, Math.pow(2, 32) - 1]);

// boardA.board --> [0, 0]
// boardB.board --> [4294967295, 4294967295]

boardA.length // --> 64
boardB.length === boardA.length // --> true

String Input

let boardA = new BitBoard('11111111');
let boardB = new BitBoard('1111111111111111111111111111111100000000000000000000000000000000');

// boardA.board --> [0, 65535];
// boardB.board --> [4294967295, 0]

let boardC = new BitBoard('1010101a') // --> Error

board.toString()

let board = BitBoard([Math.pow(2,32) - 1, 0]);
board.toString();
// length = 64
// --> "1111111111111111111111111111111100000000000000000000000000000000"

board.getIndex(n)

let board = BitBoard([Math.pow(2,32) - 1, 0]);

board.getIndex(0) // --> 0
board.getIndex(63) // --> 1
board.getIndex(64) // --> RangeError

board.copy()

let board = BitBoard([Math.pow(2,32) - 1, 0]);
let copyBoard = board.copy();

board.board === copyBoard.board // --> false

board.isEmpty()

let board = new BitBoard([Math.pow(2,32) - 1, 0]);
let defaultBoard = new BitBoard();

board.isEmpty() // --> false
defaultBoard.isEmpty() // --> true

board.and(bitBoard, modify)

let boardA = new BitBoard();
// --> "0000000000000000000000000000000000000000000000000000000000000000"

let boardB = new BitBoard([Math.pow(2,32) - 1, Math.pow(2,32) - 1]);
// --> "1111111111111111111111111111111111111111111111111111111111111111"

boardA.and(boardB)
// --> "0000000000000000000000000000000000000000000000000000000000000000"

board.or(bitBoard, modify)

let boardA = new BitBoard();
// --> "0000000000000000000000000000000000000000000000000000000000000000"

let boardB = new BitBoard([Math.pow(2,32) - 1, Math.pow(2,32) - 1]);
// --> "1111111111111111111111111111111111111111111111111111111111111111"

boardA.or(boardB)
// --> "1111111111111111111111111111111111111111111111111111111111111111"

board.xOr(bitboard, modify)

let boardA = new BitBoard();
// --> "0000000000000000000000000000000000000000000000000000000000000000"

let boardB = new BitBoard([Math.pow(2,32) - 1, Math.pow(2,32) - 1]);
// --> "1111111111111111111111111111111111111111111111111111111111111111"


boardA.xOr(boardB)
// --> "1111111111111111111111111111111111111111111111111111111111111111"

board.orNumber(shiftAmount, num, modify)

let board = new BitBoard([Math.pow(2, 32) - 1, 0]);
// --> "1111111111111111111111111111111100000000000000000000000000000000"

board.orNumber(5, 10);
// --> "1111111111111111111111111111111100000000000000000000000101000000"

board.xOrNumber(shiftAmount, num, modify)

let board = new BitBoard([Math.pow(2, 32) - 1, Math.pow(2,32) - 1]);
// --> "1111111111111111111111111111111111111111111111111111111111111111"

board.xOrNumber(2, 10);
// --> "1111111111111111111111111111111111111111111111111111111111010111"

board.not(modify)

let board = new BitBoard([Math.pow(2, 32) - 1, Math.pow(2,32) - 1]);
// --> "1111111111111111111111111111111111111111111111111111111111111111"

board.not()
// --> "0000000000000000000000000000000000000000000000000000000000000000"

board.shiftLeft(shiftAmount, modify)

let board = new BitBoard([0, Math.pow(2, 32) - 1]);
// --> "0000000000000000000000000000000011111111111111111111111111111111"

board.shiftLeft(54)
// --> "1111111111000000000000000000000000000000000000000000000000000000"

board.shiftRight(shiftAmount, modify)

let board = new BitBoard([Math.pow(2, 32) - 1, 0]);
// --> "1111111111111111111111111111111100000000000000000000000000000000"

board.shiftRight(54);
// --> "0000000000000000000000000000000000000000000000000000001111111111"

board.flipVertical(modify)

let board = new BitBoard([65535, 0]);
// --> "0000000000000000111111111111111100000000000000000000000000000000"

board.flipVertical()
// --> "0000000000000000000000000000000011111111111111110000000000000000"

board.flipDiagonal(modify)

let board = new BitBoard([65535, 0]);
// --> "0000000000000000111111111111111100000000000000000000000000000000"

board.flipDiagonal()
// --> "0000110000001100000011000000110000001100000011000000110000001100"

board.rotate180Degrees(modify)

let board = new BitBoard([65535, 0]);
// --> "0000000000000000111111111111111100000000000000000000000000000000"

board.rotate180Degrees()
// --> "0000000000000000000000000000000011111111111111110000000000000000"

board.rotate90DegreesClockwise(modify)

let board = new BitBoard([16711935,0]);
// --> "0000000011111111000000001111111100000000000000000000000000000000"

board.rotate90DegreesClockwise()
// --> "0000101000001010000010100000101000001010000010100000101000001010"

ChessBitBoard

let whiteA1Orientation = new ChessBitBoard({ boardType: "white", lsb: "a1" });

let blackA8Orientation = new ChessBitBoard({ boardType: "black" });

// --> whiteA1Orientation.board = [0, 65535] = blackA8Orientation

boardType: string optional

  • Valid inputs are: "black", "white", "piece", "pawn", "knight", "bishop", "rook", "queen", "king"

board: Array optional

  • With length = 2
  • Each number n must satisfy: 0 <= n <= 2 ^ 32 - 1 (i.e. the largest 32 digit binary number)

lsb: string optional

  • Represents the square for the Least Significant Bit. Default is "a8". Only valid input is "a1".
  • The difference between "a1" and default is that boards for "white" and "black" swap.

NOTE ON ARGUMENTS

  • if boardType is present, board is ignored
  • boardType must be "white" or "black" for lsb to take affect
  • if neither boardType or board are present, ChessBitBoard.board = 0, 0

SHAPE OF DEFAULT BOARDS

All default boards are based on the the starting positions for pieces according to the following mapping of squares to indicies. NOTE only "white" and "black" change if lsb is set to "a1".

abcdefgh
a8: 0b8: 1c8: 2d8: 3e8: 4f8: 5g8: 6h8: 7
a7: 8b7: 9c7: 10d7: 11e7: 12f7: 13g7: 14h7: 15
a6: 16b6: 17c6: 18d6: 19e6: 20f6: 21g6: 22h6: 23
a5: 24b5: 25c5: 26d5: 27e5: 28f5: 29g5: 30h5: 31
a4: 32b4: 33c4: 34d4: 35e4: 36f4: 37g4: 38h4: 39
a3: 40b3: 41c3: 42d3: 43e3: 44f3: 45g3: 46h3: 47
a2: 48b2: 49c2: 50d2: 51e2: 52f2: 53g2: 54h2: 55
a1: 56b1: 57c1: 58d1: 59e1: 60f1: 61g1: 62h1: 63

ConnectFourBitBoard

BitBoard class with an isWin() static method.

Special thanks to denkspuren for isWin logic and the following diagram.

SHAPE OF CONNECT FOUR GAME

  6 13 20 27 34 41 48   55 62     Additional row
+---------------------+ 
| 5 12 19 26 33 40 47 | 54 61     top row
| 4 11 18 25 32 39 46 | 53 60
| 3 10 17 24 31 38 45 | 52 59
| 2  9 16 23 30 37 44 | 51 58
| 1  8 15 22 29 36 43 | 50 57
| 0  7 14 21 28 35 42 | 49 56 63  bottom row
+---------------------+

isWin(connectFourBitBoard)

const northWestToSouthEastWinBoard = new ConnectFourBitBoard([0, 2130440]);
// --> "0000000000000000000000000000000000000000001000001000001000001000"
// --> 1s at [3, 9, 15, 21]

ConnectFourBitBoard.isWin(northWestToSouthEastWinBoard);
// --> true
1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago