diff options
author | IgrisBRC <[email protected]> | 2024-07-14 13:43:36 +0530 |
---|---|---|
committer | IgrisBRC <[email protected]> | 2024-07-14 13:43:36 +0530 |
commit | 4194b03e980ac68e0fd07e183189fbe93dfefdce (patch) | |
tree | 79bbce279fa277aa34cc53e4818eca59b54f8963 /public/board.js | |
parent | 0f1250b0f879eb764559d80bbc8a54d53c221f68 (diff) |
finnnallyyyy... added make move to the game, its a bit shitty, i'll make it prettier later
Diffstat (limited to 'public/board.js')
-rw-r--r-- | public/board.js | 36 |
1 files changed, 30 insertions, 6 deletions
diff --git a/public/board.js b/public/board.js index 2484136..ca46335 100644 --- a/public/board.js +++ b/public/board.js @@ -7,29 +7,53 @@ sync_board() let previously_highlighted_squares = ['dummy'] for (let i = 0; i < squares.length; i++) { - squares[i].addEventListener('click', () => { + squares[i].onclick = () => { let square = [] let id = squares[i].id square.push(8 - id.charAt(1)) square.push(id.charAt(0).charCodeAt(0) - 97) - let moves = move(board, square[0], square[1], to_move) - for (let i = 0; i < previously_highlighted_squares.length; i++) { let prev = document.getElementById(previously_highlighted_squares[i]) prev.classList.remove('highlight') } - for (let i = 0; i < moves.length; i++) { + let hlsquare = document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`) + + hlsquare.classList.add('highlight') + + hlsquare.onclick = handle_move(id, hlsquare.id) - document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`).classList.add('highlight') previously_highlighted_squares.push(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`) } - }) + } } +//let handle_move = () => { +// console.log('move') +//} + +function handle_move(from_id, 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) + } + + 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 + } +} |