Skip to content

Commit

Permalink
added flipVertical
Browse files Browse the repository at this point in the history
  • Loading branch information
algofield committed Oct 7, 2019
1 parent a1e22ed commit 7072f8d
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 38 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2019 Cj D'Agostino

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
39 changes: 30 additions & 9 deletions bitboards.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bitboards.js.map

Large diffs are not rendered by default.

46 changes: 35 additions & 11 deletions bitboards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
* @author Cj D'Agostino
*
* @class BitBoard
* @param {board} Array<number> [optional] --> Must be length = 2 where each number n must be: 0 <= n <= 2 ^ 32 - 1
* @param {board} Array<number> | string [optional]
* --> Must be length = 2 where each number n must be: 0 <= n <= 2 ^ 32 - 1
* --> Or a string of length 1 to 64 zeros and ones
* @exports BitBoard
*
* NOTE
Expand All @@ -18,20 +20,28 @@ class BitBoard {
private readonly BITS_PER_BUCKET: number;
private readonly MAX_BITS: number;

constructor(board?: Array<number>) {
constructor(board?: Array<number> | string) {
this.MAX_BITS = 4294967296; // (2 ^ 32)
this.BITS_PER_BUCKET = 32;
this.length = 64;
this.board;

if (board) {
if (!Array.isArray(board) || board.some(x => typeof x !== 'number')) {
throw new TypeError('board must be an array');
} else if (board.length !== 2 || board.some(x => Math.floor(x) !== x || x < 0 || x >= this.MAX_BITS)) {
throw new RangeError('inputs to board array must be two integers x where 0 <= x < 2 ^ 32 (or 4294967296)');
if (typeof board === 'string') {
if (board.split('').some(n => n !== '0' && n !== '1') || board.length > this.length) {
throw new SyntaxError('Inputs to board as a string must be between 1 and 64 zeroes and ones')
}
const left = board.length > 32 ? parseInt(board.slice(0, board.length - 32), 2) : 0;
const right = board.length > 32 ? parseInt(board.slice(32), 2) : parseInt(board, 2);
this.board = [left, right];

} else if (Array.isArray(board)) {
if (board.some(x => typeof x !== 'number') || board.length !== 2 || board.some(x => Math.floor(x) !== x || x < 0 || x >= this.MAX_BITS)) {
throw new Error('array inputs to board must be two integers x where 0 <= x < 2 ^ 32 (or 4294967296)');
}
this.board = board;
} else {
}
} else {
this.board = [0, 0];
}
}
Expand Down Expand Up @@ -128,7 +138,7 @@ class BitBoard {
}
return newBoard;
}
throw new TypeError('Invalid input. Must be of type "BitBoard" or "number"');
throw new TypeError('Invalid input. Must be of type BitBoard');
}

/**
Expand All @@ -146,7 +156,7 @@ class BitBoard {
}
return newBoard;
}
throw new TypeError('Invalid input. Must be of type "BitBoard" or "number"');
throw new TypeError('Invalid input. Must be of type BitBoard');
}

/**
Expand Down Expand Up @@ -303,6 +313,20 @@ class BitBoard {
}
throw new TypeError('Invalid input. Must be "number"');
}

filpVertical(modify: boolean = false) {
let newBoard: BitBoard = modify ? this : this.copy();
let maskA = new BitBoard([16711935, 16711935]);
// maskA --> "0000000011111111000000001111111100000000111111110000000011111111"
let maskB = new BitBoard([65535, 65535]);
// maskB --> "0000000000000000111111111111111100000000000000001111111111111111"

newBoard = newBoard.shiftRight(8).and(maskA).or(newBoard.and(maskA).shiftLeft(8));
newBoard = newBoard.shiftRight(16).and(maskB).or(newBoard.and(maskB).shiftLeft(16));
newBoard = newBoard.shiftRight(32).or(newBoard.shiftLeft(32));

return newBoard;
}
}

/**
Expand Down Expand Up @@ -368,7 +392,7 @@ interface BitBoardInputDictionary {
*/
class ChessBitBoard extends BitBoard {

constructor(input: BitBoardInputDictionary) {
constructor(input) {
if (input && input.boardType) {
switch (input.boardType) {
case 'piece':
Expand Down Expand Up @@ -404,7 +428,7 @@ class ChessBitBoard extends BitBoard {
} else if (input && input.board) {
super(input.board); // this.board = custom input
} else {
super(); // this.board = [0, 0];
super(input); // this.board = [0, 0];
}
}
}
Expand Down
17 changes: 12 additions & 5 deletions src/BitBoard.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7072f8d

Please sign in to comment.