summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorame <[email protected]>2024-07-16 00:01:57 -0500
committerame <[email protected]>2024-07-16 00:01:57 -0500
commitdad88be1dc6a702f6fe13e10efbe2e3d1f221dda (patch)
treed23ce29ec369d16ca714ce809cd6593cd2c70812
parent892037d22c784f4a0c2c91ddd2f5a8b21f673ed6 (diff)
optimize movement and highlighting
-rw-r--r--public/board.js25
1 files changed, 9 insertions, 16 deletions
diff --git a/public/board.js b/public/board.js
index 0c07d80..625be4a 100644
--- a/public/board.js
+++ b/public/board.js
@@ -4,7 +4,7 @@ let squares = document.getElementsByClassName('square')
sync_board()
-let previously_highlighted_squares = ['dummy']
+let highlighted_squares = []
for (let i = 0; i < squares.length; i++) {
squares[i].onclick = handle_highlight(squares[i].id)
@@ -15,9 +15,9 @@ function handle_highlight(id) {
let moves = move(board, id.charCodeAt(0) - 48, id.charCodeAt(1) - 48, to_move)
- for (let i = 0; i < squares.length; i++) {
- squares[i].classList.remove('highlight')
- squares[i].onclick = handle_highlight(squares[i].id)
+ 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++) {
@@ -26,14 +26,13 @@ function handle_highlight(id) {
hlsquare.classList.add('highlight')
hlsquare.onclick = handle_move(id, hlsquare.id)
- previously_highlighted_squares.push(`${moves[i][0]}${moves[i][1]}`)
+ 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)
@@ -49,22 +48,16 @@ function handle_move(from_id, to_id) {
to_element.removeChild(to_element.firstChild)
}
- for (let i = 0; i < previously_highlighted_squares.length; i++) {
- let prev = document.getElementById(previously_highlighted_squares[i])
- prev.classList.remove('highlight')
- prev.onclick = handle_highlight
- }
-
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 < squares.length; i++) {
- squares[i].onclick = handle_highlight(squares[i].id)
+ 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
}
-}
-
+} \ No newline at end of file