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
|
const alphabet = 'abcdefgh'
let squares = document.getElementsByClassName('square')
sync_board()
let highlighted_squares = []
for (let i = 0; i < squares.length; i++) {
squares[i].onclick = handle_highlight(squares[i].id)
}
function handle_highlight(id) {
return function() {
let moves = move(board, id.charCodeAt(0) - 48, id.charCodeAt(1) - 48, to_move)
for (let i = 0; i < highlighted_squares.length; i++) {
highlighted_squares[i].classList.remove('highlight')
highlighted_squares[i].onclick = handle_highlight(highlighted_squares[i].id)
}
for (let i = 0; i < moves.length; i++) {
let hlsquare = document.getElementById(`${moves[i][0]}${moves[i][1]}`)
hlsquare.classList.add('highlight')
hlsquare.onclick = handle_move(id, hlsquare.id)
highlighted_squares.push(hlsquare)
}
}
}
function handle_move(from_id, to_id) {
return function() {
let table = { 1: 'K', 2: 'Q', 3: 'R', 4: 'B', 5: 'N', 6: 'p' }
make_move(board, from_id.charCodeAt(0) - 48, from_id.charCodeAt(1) - 48, to_id.charCodeAt(0) - 48, to_id.charCodeAt(1) - 48)
let from_element = document.getElementById(from_id)
let to_element = document.getElementById(to_id)
while (from_element.firstChild) {
from_element.removeChild(from_element.firstChild)
}
while (to_element.firstChild) {
to_element.removeChild(to_element.firstChild)
}
let piece = document.createElement('p')
piece.classList.add(board[to_id.charCodeAt(0) - 48][to_id.charCodeAt(1) - 48] > 0 ? 'w' : 'b', 'piece')
piece.append(table[Math.abs(board[to_id.charCodeAt(0) - 48][to_id.charCodeAt(1) - 48])])
to_element.append(piece)
for (let i = 0; i < highlighted_squares.length; i++) {
highlighted_squares[i].onclick = handle_highlight(highlighted_squares[i].id)
highlighted_squares[i].classList.remove('highlight')
}
to_move = !to_move
}
}
|