highlightb = {
const boardId = highlight.id;
var board,
game = new Chess();
var removeGreySquares = function () {
d3.selectAll(`#${boardId} .square-55d63`).style("background", "");
};
var greySquare = function (square) {
var squareEl = d3.selectAll(`#${boardId} .square-` + square);
var background = "#a9a9a9";
if (squareEl.classed("black-3c85d")) {
background = "#696969";
}
squareEl.style("background", background);
};
d3.select(`#${boardId}`)
.node()
.addEventListener("touchstart", (evt) => evt.preventDefault());
var onDragStart = function (source, piece) {
if (
game.game_over() === true ||
(game.turn() === "w" && piece.search(/^b/) !== -1) ||
(game.turn() === "b" && piece.search(/^w/) !== -1)
) {
return false;
}
};
var onDrop = function (source, target) {
removeGreySquares();
// see if the move is legal
var move = game.move({
from: source,
to: target,
promotion: "q" // NOTE: always promote to a queen for example simplicity
});
// illegal move
if (move === null) return "snapback";
};
var onMouseoverSquare = function (square, piece) {
// get list of possible moves for this square
var moves = game.moves({
square: square,
verbose: true
});
// exit if there are no moves available for this square
if (moves.length === 0) return;
// highlight the square they moused over
greySquare(square);
// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
greySquare(moves[i].to);
}
};
var onMouseoutSquare = function (square, piece) {
removeGreySquares();
};
var onSnapEnd = function () {
board.position(game.fen());
};
var cfg = {
pieceTheme:
"https://chessboardjs.com/img/chesspieces/wikipedia/{piece}.png",
draggable: true,
position: "start",
onDragStart: onDragStart,
onDrop: onDrop,
onMouseoutSquare: onMouseoutSquare,
onMouseoverSquare: onMouseoverSquare,
onSnapEnd: onSnapEnd
};
board = ChessBoard(boardId, cfg);
return board;
}