Skip to content
Permalink
Browse files
Basic implementation of input to handle the moves input
  • Loading branch information
everyonesdesign committed Nov 21, 2017
0 parents commit cc2ef1250ebb4c46e16baa6b5fafc36ba0ac06bf
Showing 1 changed file with 35 additions and 0 deletions.
@@ -0,0 +1,35 @@
function init() {
var container = document.querySelector('.chess-board-container');
if (container) {
var input = document.createElement('input');
input.addEventListener('keydown', (e) => {
if (e.keyCode === 13) {
go(input.value);
input.value = '';
input.focus();
}
});
container.appendChild(input);
}
}

function go(input) {
try {
makeMove(myEvent.capturingBoard, input.slice(0, 2), input.slice(2, 4));
} catch(e) {
alert('Move is illegal');
}
}

function makeMove(board, fromField, toField) {
if (board.gameRules.isLegalMove(board.gameSetup, fromField, toField)) {
board.fireEvent('onDropPiece', {
fromAreaId: fromField,
targetAreaId: toField,
});
} else {
throw new Error('Move is illegal');
}
}

init();

0 comments on commit cc2ef12

Please sign in to comment.