In a previous question I asked, linked here: How can I speed up my check detection in Javascript? I was told to implement bit-boards to speed up my engine. I can say that bit-boards have improved my performance from being able to generate 500 moves/second to 10-15 thousand moves/second, but I have a question, how can I make my implementation of bit-boards faster?
Here is the code for the move generation:
const board = [
["c", "h", "b", "q", "k", "b", "h", "c"],
["p", "p", "p", "p", "p", "p", "p", "p"],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["", "", "", "", "", "", "", ""],
["P", "P", "P", "P", "P", "P", "P", "P"],
["C", "H", "B", "Q", "K", "B", "H", "C"]
];
function createMoveMask(mask, board, type, centerPosition) {
let x = centerPosition.x;
let y = centerPosition.y;
let moveMask = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
let blockedBitMask = getOccupiedSquares(board, type);
if (type == "blackPieces") {
var capturableSquares = getOccupiedSquares(board, "whitePieces");
} else {
var capturableSquares = getOccupiedSquares(board, "blackPieces");
}
switch (mask) {
case "north":
for (row = y - 1; row >= 0; row--) {
if (blockedBitMask[row][x] == 1) {
break;
} else if (capturableSquares[row][x] == 1) {
moveMask[row][x] = 1;
break;
}
moveMask[row][x] = 1;
}
break;
case "south":
for (row = y + 1; row < 8; row++) {
if (blockedBitMask[row][x] == 1) {
break;
} else if (capturableSquares[row][x] == 1) {
moveMask[row][x] = 1;
break;
}
moveMask[row][x] = 1;
}
break;
case "east":
for (col = x + 1; col < 8; col++) {
if (blockedBitMask[y][col] == 1) {
break;
} else if (capturableSquares[y][col] == 1) {
moveMask[y][col] = 1;
break;
}
moveMask[y][col] = 1;
}
break;
case "west":
for (col = x - 1; col >= 0; col--) {
if (blockedBitMask[y][col] == 1) {
break;
} else if (capturableSquares[y][col] == 1) {
moveMask[y][col] = 1;
break;
}
moveMask[y][col] = 1;
}
break;
case "northEast":
row = y - 1;
col = x + 1;
while (row >= 0 && col < 8) {
if (blockedBitMask[row][col] == 1) {
break;
} else if (capturableSquares[row][col] == 1) {
moveMask[row][col] = 1;
break;
}
moveMask[row][col] = 1;
row -= 1;
col += 1;
}
break;
case "northWest":
row = y - 1;
col = x - 1;
while (row >= 0 && col >= 0) {
if (blockedBitMask[row][col] == 1) {
break;
} else if (capturableSquares[row][col] == 1) {
moveMask[row][col] = 1;
break;
}
moveMask[row][col] = 1;
row -= 1;
col -= 1;
}
break;
case "southEast":
row = y + 1;
col = x + 1;
while (row < 8 && col < 8) {
if (blockedBitMask[row][col] == 1) {
break;
} else if (capturableSquares[row][col] == 1) {
moveMask[row][col] = 1;
break;
}
moveMask[row][col] = 1;
row += 1;
col += 1;
}
break;
case "southWest":
row = y + 1;
col = x - 1;
while (row < 8 && col >= 0) {
if (blockedBitMask[row][col] == 1) {
break;
} else if (capturableSquares[row][col] == 1) {
moveMask[row][col] = 1;
break;
}
moveMask[row][col] = 1;
row += 1;
col -= 1;
}
break;
}
return moveMask;
}
function getOccupiedSquares(board, type) {
let occupiedSquares = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
if (type == "blackPieces") {
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
if (
board[row][col] == "q" ||
board[row][col] == "k" ||
board[row][col] == "c" ||
board[row][col] == "h" ||
board[row][col] == "p" ||
board[row][col] == "b"
) {
occupiedSquares[row][col] = 1;
}
}
}
} else {
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
if (
board[row][col] == "Q" ||
board[row][col] == "K" ||
board[row][col] == "C" ||
board[row][col] == "H" ||
board[row][col] == "P" ||
board[row][col] == "B"
) {
occupiedSquares[row][col] = 1;
}
}
}
}
return occupiedSquares;
}
function castleMoves(board, position, type) {
let north = createMoveMask("north", board, type, position);
let south = createMoveMask("south", board, type, position);
let east = createMoveMask("east", board, type, position);
let west = createMoveMask("west", board, type, position);
let moves = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
moves[row][col] =
north[row][col] |
south[row][col] |
east[row][col] |
west[row][col];
}
}
return moves;
}
function bishopMoves(board, position, type) {
let northEast = createMoveMask("northEast", board, type, position);
let northWest = createMoveMask("northWest", board, type, position);
let southEast = createMoveMask("southEast", board, type, position);
let southWest = createMoveMask("southWest", board, type, position);
let moves = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
moves[row][col] =
northEast[row][col] |
southEast[row][col] |
northWest[row][col] |
southWest[row][col];
}
}
return moves;
}
function queenMoves(board, position, type) {
let northEast = createMoveMask("northEast", board, type, position);
let northWest = createMoveMask("northWest", board, type, position);
let southEast = createMoveMask("southEast", board, type, position);
let southWest = createMoveMask("southWest", board, type, position);
let north = createMoveMask("north", board, type, position);
let south = createMoveMask("south", board, type, position);
let east = createMoveMask("east", board, type, position);
let west = createMoveMask("west", board, type, position);
let moves = [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
];
for (row = 0; row < 8; row++) {
for (col = 0; col < 8; col++) {
moves[row][col] =
north[row][col] |
south[row][col] |
east[row][col] |
west[row][col] |
northEast[row][col] |
northWest[row][col] |
southEast[row][col] |
southWest[row][col];
}
}
return moves;
}
If you need any clarifications regarding the question or the code, feel free to ask.