board[highlightPos.y][highlightPos.x]
indexing is repeated multiple times and worth to be extracted into a variable:
let boardItem = board[elPos.y][elPos.x];
function getElPosition(e) {
return {
x: Math.floor(event.clientX / 60),
y: Math.floor(event.clientY / 60)
};
}
document.addEventListener('click', function (event) {
let elPos = getElPosition(event);
if (!hasClicked) {
if (event.clientX < 480 && event.clientY < 480 && board[elPos.y][elPos.x] != "vacant") {
if (humanPlayer.indexOf(board[elPos.y][elPos.x]) != -1) {
isHighlightPossibleMoves = true;
pieceMoves = processMoves(Object.assign({}, elPos), board);
}
hasClicked = true;
}
} else {
if (pieceMoves && pieceMoves.length) {
for (let i = 0, len = pieceMoves.moves.length; i < len; i++) {
let boardItem = board[elPos.y][elPos.x];
if (elPos.x == pieceMoves.moves[i].x && elPos.y == pieceMoves.moves[i].y) {
if (board[elPos.y][elPos.x]boardItem == blackKing || board[elPos.y][elPos.x]boardItem == whiteKing) {
isCastling = (pieceMoves.moves[i].x - 2 == elPos.x || pieceMoves.moves[i].x + 2 == elPos.x);
}
if (!isCastling) {
if ([whiteKingSideCastle, whiteQueenSideCastle, blackKingSideCastle,
blackQueenSideCastle, blackKing, whiteKingwhiteKing].includes(boardItem))
].includes(board[elPos.y][elPos.x])) {
board[elPos.y][elPos.x]boardItem.hasClicked = true;
}
board = chess.updateBoard(elPos, elPos);
chess = new Chess(board);
break;
}
board = chess.returnCastledBoard({
x: elPos.x,
y: elPos.y
}, pieceMoves.moves[i]);
chess = new Chess(board);
isCastling = false;
}
}
}
hasClicked = false;
pieceMoves = undefined;
}
});