forked from jchallenger1/Chess-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.js
77 lines (58 loc) · 1.75 KB
/
board.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Piece movement and highlighting from https://www.chessboardjs.com/examples#5003
var board = null
var game = new Chess()
var whiteSquareGrey = '#a9a9a9'
var blackSquareGrey = '#696969'
function removeGreySquares() {
$('#board .square-55d63').css('background', '')
}
function greySquare(square) {
var $square = $('#board .square-' + square)
var background = whiteSquareGrey
if ($square.hasClass('black-3c85d')) {
background = blackSquareGrey
}
$square.css('background', background)
}
function onDragStart(source, piece) {
// do not pick up pieces if the game is over
if (game.game_over()) return false
// only pick up pieces for White
if (piece.search(/^b/) !== -1) return false
}
function onDrop(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'
// bot move
window.setTimeout(makeAgentMove, 250)
// update the portable game notation
$('#pgn').text(game.pgn());
}
function onMouseoverSquare(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)
}
}
function onMouseoutSquare(square, piece) {
removeGreySquares()
}
function onSnapEnd() {
board.position(game.fen())
}