Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Got basic chessboard with click-based movement and highlighting to work?
? Struggled away on click-based movement because my current thinking for Zeno wouldn't work drag based moves (need to specify and then watch it slowly grind its way  to the destination)
  • Loading branch information
pippinbarr committed Apr 3, 2019
1 parent 280b087 commit df0bc89
Showing 1 changed file with 88 additions and 1 deletion.
89 changes: 88 additions & 1 deletion js/script.js
Expand Up @@ -7,6 +7,93 @@ Pippin Barr
******************/

let game;
let board;

let from = null;
let to = null;
let turn = 'w';

$(document).ready(function () {
let board = ChessBoard('board','start');
let config = {
draggable: false,
position: 'start',
};
board = ChessBoard('board', config);

game = new Chess();

$('.square-55d63').on('click',squareClicked);
});

function squareClicked() {
let square = $(this).attr('data-square');

console.log(square)

let piece = $(this).find('.piece-417db');

if (from === null && piece.length !== 0 && piece.attr('data-piece').indexOf(turn) !== -1) {
from = square;

var moves = game.moves({
square: from,
verbose: true
});

// exit if there are no moves available for this square
if (moves.length === 0) return;

// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
highlight(moves[i].to);
}
}
else if (from !== null) {
if (piece.length !== 0 && piece.attr('data-piece').indexOf(turn) !== -1) {
// Selecting a new piece to move
clearHighlights();
from = square;

var moves = game.moves({
square: from,
verbose: true
});

// exit if there are no moves available for this square
if (moves.length === 0) return;

// highlight the possible squares for this piece
for (var i = 0; i < moves.length; i++) {
highlight(moves[i].to);
}
}
else if ($(this).hasClass('highlight1-32417')) {

let to = $(this).attr('data-square');

let move = game.move({
from: from,
to: to,
promotion: 'q' // NOTE: always promote to a queen for example simplicity
});

clearHighlights();

board.position(game.fen(),false);

from = null;
to = null;

turn = (turn === 'w') ? 'b' : 'w';
};
}
}

function clearHighlights() {
$('.square-55d63').removeClass(`highlight1-32417`);
}

function highlight(square) {
$('.square-'+square).addClass(`highlight1-32417`);
}

0 comments on commit df0bc89

Please sign in to comment.