From 57e8e55e1e5d72011971f3010fa2b584d1d8f16d Mon Sep 17 00:00:00 2001 From: IgrisBRC Date: Mon, 15 Jul 2024 10:51:50 +0530 Subject: made some changes to my godawful code --- public/board.js | 53 ++++++--------------- public/index.html | 128 +++++++++++++++++++++++++-------------------------- public/sync_board.js | 2 +- 3 files changed, 78 insertions(+), 105 deletions(-) diff --git a/public/board.js b/public/board.js index 5738eab..bc76394 100644 --- a/public/board.js +++ b/public/board.js @@ -7,14 +7,15 @@ sync_board() let previously_highlighted_squares = ['dummy'] for (let i = 0; i < squares.length; i++) { - squares[i].onclick = () => { - let square = [] - let id = squares[i].id + squares[i].onclick = handle_highlight(i) +} - square.push(8 - id.charAt(1)) - square.push(id.charAt(0).charCodeAt(0) - 97) +function handle_highlight(i) { + return function() { + + let id = squares[i].id - let moves = move(board, square[0], square[1], to_move) + let moves = move(board, id.charAt(0), id.charAt(1), to_move) for (let i = 0; i < previously_highlighted_squares.length; i++) { let prev = document.getElementById(previously_highlighted_squares[i]) @@ -22,47 +23,19 @@ for (let i = 0; i < squares.length; i++) { } for (let i = 0; i < moves.length; i++) { - - let hlsquare = document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`) + let hlsquare = document.getElementById(`${moves[i][0]}${moves[i][1]}`) hlsquare.classList.add('highlight') - hlsquare.onclick = handle_move(id, hlsquare.id) - - previously_highlighted_squares.push(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`) - } + hlsquare.onclick = handle_move(hlsquare.id) - let hlsquare = document.getElementsByClassName('highlight') - for (let i = 0; i < hlsquare.length; i++) { + previously_highlighted_squares.push(`${moves[i][0]}${moves[i][1]}`) } } } -function handle_move(from_id, id) { +function handle_move(id) { return function() { - let table = { 1: 'K', 2: 'Q', 3: 'R', 4: 'B', 5: 'N', 6: 'p' } - - make_move(board, 8 - from_id.charAt(1), from_id.charCodeAt(0) - 97, 8 - id.charAt(1), id.charCodeAt(0) - 97) - - let from_element = document.getElementById(from_id) - while (from_element.firstChild) { - from_element.removeChild(from_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 = null - } - - let new_element = document.getElementById(id) - let piece = document.createElement('p') - piece.classList.add(board[8 - id.charAt(1)][id.charCodeAt(0) - 97] > 0 ? 'w' : 'b', 'piece') - new_element.append(piece) - piece.append(table[Math.abs(board[8 - id.charAt(1)][id.charCodeAt(0) - 97])]) - - to_move = !to_move - - console.log(from_id, id) - console.log(board) + console.log(id) } } + diff --git a/public/index.html b/public/index.html index 5280cde..8440a76 100644 --- a/public/index.html +++ b/public/index.html @@ -12,98 +12,98 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
diff --git a/public/sync_board.js b/public/sync_board.js index 7044db2..739b4a6 100644 --- a/public/sync_board.js +++ b/public/sync_board.js @@ -6,7 +6,7 @@ function sync_board() { for (let j = 0; j < 8; j++) { if (board[i][j] == 0) continue - let square = document.getElementById(`${alphabet.charAt(j)}${8 - i}`) + let square = document.getElementById(`${i}${j}`) let piece = document.createElement('p') piece.classList.add(board[i][j] > 0 ? 'w' : 'b', 'piece') -- cgit v1.2.3