summaryrefslogtreecommitdiff
path: root/public/board.js
diff options
context:
space:
mode:
authorIgrisBRC <[email protected]>2024-07-16 22:24:49 +0530
committerIgrisBRC <[email protected]>2024-07-16 22:24:49 +0530
commita7e11ff8f729a669d8b2445f066606302ecb1b46 (patch)
tree1f82d5fd3ca3ef6010068f2f0a1985f3f72efbac /public/board.js
parent6894b9d9337be6c5c8032e1d1890dc5a7ebdce9b (diff)
added castling ;)
Diffstat (limited to 'public/board.js')
-rw-r--r--public/board.js99
1 files changed, 97 insertions, 2 deletions
diff --git a/public/board.js b/public/board.js
index 625be4a..7fc8bec 100644
--- a/public/board.js
+++ b/public/board.js
@@ -35,7 +35,56 @@ 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 castle = false
+ let castle_rook_move = []
+
+ let prev_y_x = []
+ let new_y_x = []
+
+ prev_y_x.push([from_id.charCodeAt(0) - 48, from_id.charCodeAt(1) - 48])
+ new_y_x.push([to_id.charCodeAt(0) - 48, to_id.charCodeAt(1) - 48])
+
+ if (board[from_id.charCodeAt(0) - 48][from_id.charCodeAt(1) - 48] == 1 && to_id.charCodeAt(0) - 48 == 7 && to_id.charCodeAt(1) - 48 == 6) {
+ prev_y_x.push([7, 7])
+ new_y_x.push([7, 5])
+
+ castle_rook_move.push([7, 7])
+ castle_rook_move.push([7, 5])
+
+ castle = true
+ }
+
+ else if (board[from_id.charCodeAt(0) - 48][from_id.charCodeAt(1) - 48] == 1 && to_id.charCodeAt(0) - 48 == 7 && to_id.charCodeAt(1) - 48 == 2) {
+ prev_y_x.push([7, 0])
+ new_y_x.push([7, 3])
+
+ castle_rook_move.push([7, 0])
+ castle_rook_move.push([7, 3])
+
+ castle = true
+ }
+
+ else if (board[from_id.charCodeAt(0) - 48][from_id.charCodeAt(1) - 48] == -1 && to_id.charCodeAt(0) - 48 == 0 && to_id.charCodeAt(1) - 48 == 6) {
+ prev_y_x.push([0, 7])
+ new_y_x.push([0, 5])
+
+ castle_rook_move.push([0, 7])
+ castle_rook_move.push([0, 5])
+
+ castle = true
+ }
+
+ else if (board[from_id.charCodeAt(0) - 48][from_id.charCodeAt(1) - 48] == -1 && to_id.charCodeAt(0) - 48 == 0 && to_id.charCodeAt(1) - 48 == 2) {
+ prev_y_x.push([0, 0])
+ new_y_x.push([0, 3])
+
+ castle_rook_move.push([0, 0])
+ castle_rook_move.push([0, 3])
+
+ castle = true
+ }
+
+ make_move(board, prev_y_x, new_y_x)
let from_element = document.getElementById(from_id)
let to_element = document.getElementById(to_id)
@@ -48,6 +97,52 @@ function handle_move(from_id, to_id) {
to_element.removeChild(to_element.firstChild)
}
+ if (castle) {
+ let color = castle && to_move
+ castle = false
+
+ if (color) {
+
+ let from_element = document.getElementById(`${castle_rook_move[0][0]}${castle_rook_move[0][1]}`)
+ let to_element = document.getElementById(`${castle_rook_move[1][0]}${castle_rook_move[1][1]}`)
+
+ 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[castle_rook_move[1][0]][castle_rook_move[1][1]] > 0 ? 'w' : 'b', 'piece')
+ piece.append(table[Math.abs(board[castle_rook_move[1][0]][castle_rook_move[1][1]])])
+ to_element.append(piece)
+
+ } else {
+
+
+ let from_element = document.getElementById(`${castle_rook_move[0][0]}${castle_rook_move[0][1]}`)
+ let to_element = document.getElementById(`${castle_rook_move[1][0]}${castle_rook_move[1][1]}`)
+
+ 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[castle_rook_move[1][0]][castle_rook_move[1][1]] > 0 ? 'w' : 'b', 'piece')
+ piece.append(table[Math.abs(board[castle_rook_move[1][0]][castle_rook_move[1][1]])])
+ to_element.append(piece)
+
+ }
+ }
+
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])])
@@ -60,4 +155,4 @@ function handle_move(from_id, to_id) {
to_move = !to_move
}
-} \ No newline at end of file
+}