Skip to content

Commit

Permalink
Basic implementation of input to handle the moves input
Browse files Browse the repository at this point in the history
  • Loading branch information
everyonesdesign committed Nov 21, 2017
0 parents commit cc2ef12
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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.