JSFiddle - React, Tailwind, and code Playground
by Douglas Ludlow
JavaScript
(function (checkers, undefined) {
checkers.Move = function (fromRow, fromCol, toRow, toCol) {
return {
isFrom = isFrom,
isValid = isValid,
perform = perform,
isJump = isJump
};
function isFrom(row, col) {
return fromRow === row && fromCol === col;
}
function isValid() {
}
function perform(board, context) {
var fromSquare = board[fromRow][fromCol];
var toSquare = board[toRow][toCol];
var piece = fromSquare.piece;
fromSquare.piece = undefined;
toSquare.piece = piece;
if (isJump()) {
rowDiff = toRow - fromRow;
colDiff = toCol - fromCol;
row = fromRow + rowDiff / 2;
col = fromCol + colDiff / 2;
board[row][col].piece = undefined;
board[row][col].draw(context, row, col);
}
fromSquare.draw(context, fromRow, fromCol);
toSquare.draw(context, toRow, toCol);
}
function isJump() {
return (Math.abs(fromRow - toRow) === 2);
}
};
})(window.checkers = window.checkers || {});