diff options
-rw-r--r-- | public/board.js | 132 | ||||
-rw-r--r-- | public/make_move.js | 15 | ||||
-rw-r--r-- | public/move.js | 4 | ||||
-rw-r--r-- | public/style.css | 3 |
4 files changed, 80 insertions, 74 deletions
diff --git a/public/board.js b/public/board.js index 7fc8bec..4afaeb7 100644 --- a/public/board.js +++ b/public/board.js @@ -32,56 +32,71 @@ function handle_highlight(id) { } function handle_move(from_id, to_id) { - return function() { + function fun() { let table = { 1: 'K', 2: 'Q', 3: 'R', 4: 'B', 5: 'N', 6: 'p' } + let from_y = from_id.charCodeAt(0) - 48 + let from_x = from_id.charCodeAt(1) - 48 + + let to_y = to_id.charCodeAt(0) - 48 + let to_x = to_id.charCodeAt(1) - 48 + + let from = board[from_y][from_x] + let to = board[to_y][to_x] + + let from_color = board[from_y][from_x] > 0 + let to_color = board[to_y][to_x] > 0 + 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]) + prev_y_x.push([from_y, from_x]) + new_y_x.push([to_y, to_x]) - 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]) + if(Math.abs(from) == 1 && to == 0){ + console.log("valid") + if (castle_white_short && from_color && to_y == 7 && to_x == 6) { //white short + 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_rook_move.push([7, 7]) + castle_rook_move.push([7, 5]) - castle = true - } + 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]) + else if (castle_white_long && from_color && to_y == 7 && to_x == 2) { //white long + 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_rook_move.push([7, 0]) + castle_rook_move.push([7, 3]) - castle = true - } + 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]) + else if (castle_black_short && !from_color && to_y == 0 && to_x == 6) { //black short + 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_rook_move.push([0, 7]) + castle_rook_move.push([0, 5]) - castle = true - } + 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]) + else if (castle_black_long && !from_color && to_y == 0 && to_x == 2) { //black long + 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_rook_move.push([0, 0]) + castle_rook_move.push([0, 3]) - castle = true + castle = true + } } make_move(board, prev_y_x, new_y_x) @@ -98,54 +113,27 @@ function handle_move(from_id, to_id) { } 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]}`) - 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) - } + while (from_element.firstChild) { + from_element.removeChild(from_element.firstChild) + } - let piece = document.createElement('p') + while (to_element.firstChild) { + to_element.removeChild(to_element.firstChild) + } - 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[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])]) + piece.classList.add(board[to_y][to_x] > 0 ? 'w' : 'b', 'piece') + piece.append(table[Math.abs(board[to_y][to_x])]) to_element.append(piece) for (let i = 0; i < highlighted_squares.length; i++) { @@ -155,4 +143,6 @@ function handle_move(from_id, to_id) { to_move = !to_move } + + return fun; } diff --git a/public/make_move.js b/public/make_move.js index e00b999..6233ed0 100644 --- a/public/make_move.js +++ b/public/make_move.js @@ -17,6 +17,21 @@ function make_move(board, prev_y_x, new_y_x) { } for (let i = 0; i < prev_y_x.length; i++) { + let color = board[prev_y_x[i][0]][prev_y_x[i][1]] > 0 + + if(Math.abs(board[prev_y_x[i][0]][prev_y_x[i][1]]) == 1){ + if(color) castle_white_short = castle_white_long = false + else castle_black_short = castle_black_long = false + } else if(Math.abs(board[prev_y_x[i][0]][prev_y_x[i][1]]) == 3){ + if(prev_y_x[i][1] == 0){ + if(color) castle_white_long = false + else castle_black_long = false + } else if (prev_y_x[i][1] == 7){ + if(color) castle_white_short = false + else castle_black_short = false + } + } + board[prev_y_x[i][0]][prev_y_x[i][1]] = 0 } } diff --git a/public/move.js b/public/move.js index 59badf4..87de976 100644 --- a/public/move.js +++ b/public/move.js @@ -168,7 +168,7 @@ function king_move(board, y, x) { let moves = [] let controlled = controlled_squares(board, to_move) - + for (let i = y - 1; i <= y + 1; i += 1) { for (let j = x - 1; j <= x + 1; j += 1) { if ((i == y && j == x) || i < 0 || j < 0 || i >= board.length || j >= board[i].length) { @@ -179,7 +179,7 @@ function king_move(board, y, x) { } } } - + if (color && castle_white_short && board[7][6] == 0 && board[7][5] == 0) { for (let i = 0; i < controlled.length; i++) { if ((controlled[i][0] == 7 && controlled[i][1] == 6) || (controlled[i][0] == 7 && controlled[i][1] == 5)) { diff --git a/public/style.css b/public/style.css index 929af94..86510ea 100644 --- a/public/style.css +++ b/public/style.css @@ -6,7 +6,8 @@ .box{ height: 76px; width: 76px; - position: relative; + position: relative; + user-select: none; } .top-corner{ padding: 1px; |