From c5de8054793b54567e778935986e16741bdacad0 Mon Sep 17 00:00:00 2001 From: IgrisBRC Date: Sun, 14 Jul 2024 11:24:07 +0530 Subject: refactored the project --- public/app.js | 366 --------------------------------------------------- public/board.js | 35 +++++ public/game.js | 106 --------------- public/index.html | 6 +- public/make_move.js | 5 + public/move.js | 366 +++++++++++++++++++++++++++++++++++++++++++++++++++ public/sync_board.js | 75 +++++++++++ 7 files changed, 485 insertions(+), 474 deletions(-) delete mode 100644 public/app.js create mode 100644 public/board.js delete mode 100644 public/game.js create mode 100644 public/make_move.js create mode 100644 public/move.js create mode 100644 public/sync_board.js (limited to 'public') diff --git a/public/app.js b/public/app.js deleted file mode 100644 index 8de18e4..0000000 --- a/public/app.js +++ /dev/null @@ -1,366 +0,0 @@ -let board = [ - [-3, -5, -4, -2, -1, -4, -5, -3], - [-6, -6, -6, -6, -6, -6, -6, -6], - [0, 0, -5, 0, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0], - [0, 0, 0, 2, 0, 0, 0, 0], - [0, 0, 0, 0, 0, 0, 0, 0], - [6, 6, 6, 6, 6, 6, 6, 6], - [3, 5, 4, 2, 1, 4, 5, 3], -] - -//console.log(move(board, 4, 3)) -//move(board, 3, 3) - -function move(board, y, x) { - let piece = Math.abs(board[y][x]) - switch (piece) { - case 0: - return [] - case 1: - return king_move(board, y, x) - case 2: - return amelia_move(board, y, x) - case 3: - return rook_move(board, y, x) - case 4: - return bishop_move(board, y, x) - case 5: - return knight_move(board, y, x) - case 6: - return pawn_move(board, y, x) - } -} - -function pawn_move(board, y, x) { - let moves = [] - let color = board[y][x] > 0 - - if (color) { - if (y == 6) { - if (board[y - 1][x] == 0) { - moves.push([y - 1, x]) - } - else { - return moves - } - if (board[y - 2][x] == 0) { - moves.push([y - 2, x]) - } - } else if (color && y != 0 && board[y - 1][x] == 0) { - moves.push([y - 1, x]) - } - - return moves - } else { - if (y == 1) { - if (board[y + 1][x] == 0) { - moves.push([y + 1, x]) - } - else { - return moves - } - if (board[y + 2][x] == 0) { - moves.push([y + 2, x]) - } - } else if (color && y != 7 && board[y + 1][x] == 0) { - moves.push([y + 1, x]) - } - return moves - } -} - -function king_move(board, y, x) { - let color = board[y][x] > 0 - let moves = [] - - 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) { - continue - } - if (board[i][j] == 0 || (color ^ board[i][j] > 0)) { - moves.push([i, j]) - } - } - } - - return moves -} - -function knight_move(board, y, x) { - let color = board[y][x] > 0 - let moves = [] - - let cand = [ - [y + 2, x + 1], - [y + 2, x - 1], - [y + 1, x - 2], - [y + 1, x + 2], - [y - 2, x + 1], - [y - 2, x - 1], - [y - 1, x + 2], - [y - 1, x - 2], - ] - - for (let k = 0; k < cand.length; k++) { - if (cand[k][0] < 0 || cand[k][1] < 0 || cand[k][0] >= board.length || cand[k][1] >= board[cand[k][0]].length) { - continue - } - - if (board[cand[k][0]][cand[k][1]] == 0 || (color ^ board[cand[k][0]][cand[k][1]] > 0)) { - moves.push([cand[k][0], cand[k][1]]) - } - } - - return moves -} - - -function rook_move(board, y, x) { - let color = board[y][x] > 0 - let moves = [] - - for (let i = x + 1; i < board[1].length; i++) { - if (board[y][i] != 0) { - if (color ^ board[y][i] > 0) { - console.log(color ^ board[y][i]) - moves.push([y, i]) - break - } - break - } - moves.push([y, i]) - } - - for (let i = x - 1; i >= 0; i--) { - if (board[y][i] != 0) { - if (color ^ board[y][i] > 0) { - moves.push([y, i]) - break - } - break - } - moves.push([y, i]) - } - - - for (let i = y + 1; i < board.length; i++) { - if (board[i][x] != 0) { - if (color ^ board[i][x] > 0) { - moves.push([i, x]) - break - } - break - } - moves.push([i, x]) - } - - for (let i = y - 1; i >= 0; i--) { - if (board[i][x] != 0) { - if (color ^ board[i][x] > 0) { - moves.push([i, x]) - break - } - break - } - moves.push([i, x]) - } - - return moves -} - -function bishop_move(board, y, x) { - let color = board[y][x] > 0 - let moves = [] - - let i = y + 1 - let j = x + 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i += 1 - j += 1 - } - - i = y - 1 - j = x - 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i -= 1 - j -= 1 - } - - i = y - 1 - j = x + 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i -= 1 - j += 1 - } - - i = y + 1 - j = x - 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i += 1 - j -= 1 - } - - return moves -} - -function amelia_move(board, y, x) { - let color = board[y][x] > 0 - let moves = [] - - for (let i = x + 1; i < board[1].length; i++) { - if (board[y][i] != 0) { - if (color ^ board[y][i] > 0) { - console.log(color ^ board[y][i]) - moves.push([y, i]) - break - } - break - } - moves.push([y, i]) - } - - for (let i = x - 1; i >= 0; i--) { - if (board[y][i] != 0) { - if (color ^ board[y][i] > 0) { - moves.push([y, i]) - break - } - break - } - moves.push([y, i]) - } - - - for (let i = y + 1; i < board.length; i++) { - if (board[i][x] != 0) { - if (color ^ board[i][x] > 0) { - moves.push([i, x]) - break - } - break - } - moves.push([i, x]) - } - - for (let i = y - 1; i >= 0; i--) { - if (board[i][x] != 0) { - if (color ^ board[i][x] > 0) { - moves.push([i, x]) - break - } - break - } - moves.push([i, x]) - } - let i = y + 1 - let j = x + 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i += 1 - j += 1 - } - - i = y - 1 - j = x - 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i -= 1 - j -= 1 - } - - i = y - 1 - j = x + 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i -= 1 - j += 1 - } - - i = y + 1 - j = x - 1 - - while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { - if (board[i][j] != 0) { - if (color ^ board[i][j] > 0) { - moves.push([i, j]) - break - } - break - } - - moves.push([i, j]) - i += 1 - j -= 1 - } - return moves -} - diff --git a/public/board.js b/public/board.js new file mode 100644 index 0000000..e0f0cbe --- /dev/null +++ b/public/board.js @@ -0,0 +1,35 @@ +const alphabet = 'abcdefgh' + +let squares = document.getElementsByClassName('square') + +sync_board() + +let previously_highlighted_squares = ['dummy'] + +for (let i = 0; i < squares.length; i++) { + squares[i].addEventListener('click', () => { + 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]) + + + 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++) { + + 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]}`) + } + }) +} + + diff --git a/public/game.js b/public/game.js deleted file mode 100644 index e5740de..0000000 --- a/public/game.js +++ /dev/null @@ -1,106 +0,0 @@ -const alphabet = 'abcdefgh' - - -for (let i = 0; i < 8; i++) { - for (let j = 0; j < 8; j++) { - let square = document.getElementById(`${alphabet.charAt(j)}${8 - i}`) - let piece = document.createElement('p') - - piece.classList.add('piece') - - switch (board[i][j]) { - case 1: - piece.classList.add('wk') - square.append(piece) - piece.append('K') - break - case -1: - piece.classList.add('bk') - square.append(piece) - piece.append('K') - break - case 2: - piece.classList.add('wq') - square.append(piece) - piece.append('Q') - break - case -2: - piece.classList.add('bq') - square.append(piece) - piece.append('Q') - break - case 3: - piece.classList.add('wr') - square.append(piece) - piece.append('R') - break - case -3: - piece.classList.add('br') - square.append(piece) - piece.append('R') - break - case 4: - piece.classList.add('wb') - square.append(piece) - piece.append('B') - break - case -4: - piece.classList.add('bb') - square.append(piece) - piece.append('B') - break - case 5: - piece.classList.add('wn') - square.append(piece) - piece.append('N') - break - case -5: - piece.classList.add('bn') - square.append(piece) - piece.append('N') - break - case 6: - piece.classList.add('wp') - square.append(piece) - piece.append('p') - break - case -6: - piece.classList.add('bp') - square.append(piece) - piece.append('p') - break - } - } -} - -let squares = document.getElementsByClassName('square') - -let previously_highlighted_squares = ['dummy'] - -for (let i = 0; i < squares.length; i++) { - - - squares[i].addEventListener('click', (event) => { - 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]) - - for (let i = 0; i < previously_highlighted_squares.length; i++) { - //document.getElementById(previously_highlighted_squares[i]).style.backgroundColor = 'transparent' - document.getElementById(previously_highlighted_squares[i]).classList.remove('highlight') - } - - for (let i = 0; i < moves.length; i++) { - - //document.getElementById(`${alphabet[moves[i][1]]}${8 - moves[i][0]}`).style.backgroundColor = 'yellow' - 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]}`) - } - - }) -} diff --git a/public/index.html b/public/index.html index 20bda5a..dcf7f5f 100644 --- a/public/index.html +++ b/public/index.html @@ -126,7 +126,9 @@ - - + + + + diff --git a/public/make_move.js b/public/make_move.js new file mode 100644 index 0000000..ec0956d --- /dev/null +++ b/public/make_move.js @@ -0,0 +1,5 @@ +function make_move(board, y ,x, new_y, new_x) { + let temp = board[y][x] + board[y][x] = 0 + board[new_y][new_x] = temp +} diff --git a/public/move.js b/public/move.js new file mode 100644 index 0000000..fe1767b --- /dev/null +++ b/public/move.js @@ -0,0 +1,366 @@ +let board = [ + [-3, -5, -4, -2, -1, -4, -5, -3], + [-6, -6, -6, -6, -6, -6, -6, -6], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 2, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0], + [6, 6, 6, 6, 6, 6, 6, 6], + [3, 5, 4, 2, 1, 4, 5, 3], +] + +//console.log(move(board, 4, 3)) +//move(board, 3, 3) + +function move(board, y, x) { + let piece = Math.abs(board[y][x]) + switch (piece) { + case 0: + return [] + case 1: + return king_move(board, y, x) + case 2: + return amelia_move(board, y, x) + case 3: + return rook_move(board, y, x) + case 4: + return bishop_move(board, y, x) + case 5: + return knight_move(board, y, x) + case 6: + return pawn_move(board, y, x) + } +} + +function pawn_move(board, y, x) { + let moves = [] + let color = board[y][x] > 0 + + if (color) { + if (y == 6) { + if (board[y - 1][x] == 0) { + moves.push([y - 1, x]) + } + else { + return moves + } + if (board[y - 2][x] == 0) { + moves.push([y - 2, x]) + } + } else if (color && y != 0 && board[y - 1][x] == 0) { + moves.push([y - 1, x]) + } + + return moves + } else { + if (y == 1) { + if (board[y + 1][x] == 0) { + moves.push([y + 1, x]) + } + else { + return moves + } + if (board[y + 2][x] == 0) { + moves.push([y + 2, x]) + } + } else if (color && y != 7 && board[y + 1][x] == 0) { + moves.push([y + 1, x]) + } + return moves + } +} + +function king_move(board, y, x) { + let color = board[y][x] > 0 + let moves = [] + + 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) { + continue + } + if (board[i][j] == 0 || (color ^ board[i][j] > 0)) { + moves.push([i, j]) + } + } + } + + return moves +} + +function knight_move(board, y, x) { + let color = board[y][x] > 0 + let moves = [] + + let cand = [ + [y + 2, x + 1], + [y + 2, x - 1], + [y + 1, x - 2], + [y + 1, x + 2], + [y - 2, x + 1], + [y - 2, x - 1], + [y - 1, x + 2], + [y - 1, x - 2], + ] + + for (let k = 0; k < cand.length; k++) { + if (cand[k][0] < 0 || cand[k][1] < 0 || cand[k][0] >= board.length || cand[k][1] >= board[cand[k][0]].length) { + continue + } + + if (board[cand[k][0]][cand[k][1]] == 0 || (color ^ board[cand[k][0]][cand[k][1]] > 0)) { + moves.push([cand[k][0], cand[k][1]]) + } + } + + return moves +} + + +function rook_move(board, y, x) { + let color = board[y][x] > 0 + let moves = [] + + for (let i = x + 1; i < board[1].length; i++) { + if (board[y][i] != 0) { + if (color ^ board[y][i] > 0) { + console.log(color ^ board[y][i]) + moves.push([y, i]) + break + } + break + } + moves.push([y, i]) + } + + for (let i = x - 1; i >= 0; i--) { + if (board[y][i] != 0) { + if (color ^ board[y][i] > 0) { + moves.push([y, i]) + break + } + break + } + moves.push([y, i]) + } + + + for (let i = y + 1; i < board.length; i++) { + if (board[i][x] != 0) { + if (color ^ board[i][x] > 0) { + moves.push([i, x]) + break + } + break + } + moves.push([i, x]) + } + + for (let i = y - 1; i >= 0; i--) { + if (board[i][x] != 0) { + if (color ^ board[i][x] > 0) { + moves.push([i, x]) + break + } + break + } + moves.push([i, x]) + } + + return moves +} + +function bishop_move(board, y, x) { + let color = board[y][x] > 0 + let moves = [] + + let i = y + 1 + let j = x + 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i += 1 + j += 1 + } + + i = y - 1 + j = x - 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i -= 1 + j -= 1 + } + + i = y - 1 + j = x + 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i -= 1 + j += 1 + } + + i = y + 1 + j = x - 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i += 1 + j -= 1 + } + + return moves +} + +function amelia_move(board, y, x) { + let color = board[y][x] > 0 + let moves = [] + + for (let i = x + 1; i < board[1].length; i++) { + if (board[y][i] != 0) { + if (color ^ board[y][i] > 0) { + console.log(color ^ board[y][i]) + moves.push([y, i]) + break + } + break + } + moves.push([y, i]) + } + + for (let i = x - 1; i >= 0; i--) { + if (board[y][i] != 0) { + if (color ^ board[y][i] > 0) { + moves.push([y, i]) + break + } + break + } + moves.push([y, i]) + } + + + for (let i = y + 1; i < board.length; i++) { + if (board[i][x] != 0) { + if (color ^ board[i][x] > 0) { + moves.push([i, x]) + break + } + break + } + moves.push([i, x]) + } + + for (let i = y - 1; i >= 0; i--) { + if (board[i][x] != 0) { + if (color ^ board[i][x] > 0) { + moves.push([i, x]) + break + } + break + } + moves.push([i, x]) + } + let i = y + 1 + let j = x + 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i += 1 + j += 1 + } + + i = y - 1 + j = x - 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i -= 1 + j -= 1 + } + + i = y - 1 + j = x + 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i -= 1 + j += 1 + } + + i = y + 1 + j = x - 1 + + while (!(i < 0 || j < 0 || i >= board.length || j >= board[i].length)) { + if (board[i][j] != 0) { + if (color ^ board[i][j] > 0) { + moves.push([i, j]) + break + } + break + } + + moves.push([i, j]) + i += 1 + j -= 1 + } + return moves +} + diff --git a/public/sync_board.js b/public/sync_board.js new file mode 100644 index 0000000..84fab04 --- /dev/null +++ b/public/sync_board.js @@ -0,0 +1,75 @@ + +function sync_board() { + for (let i = 0; i < 8; i++) { + for (let j = 0; j < 8; j++) { + let square = document.getElementById(`${alphabet.charAt(j)}${8 - i}`) + let piece = document.createElement('p') + + piece.classList.add('piece') + + switch (board[i][j]) { + case 1: + piece.classList.add('wk') + square.append(piece) + piece.append('K') + break + case -1: + piece.classList.add('bk') + square.append(piece) + piece.append('K') + break + case 2: + piece.classList.add('wq') + square.append(piece) + piece.append('Q') + break + case -2: + piece.classList.add('bq') + square.append(piece) + piece.append('Q') + break + case 3: + piece.classList.add('wr') + square.append(piece) + piece.append('R') + break + case -3: + piece.classList.add('br') + square.append(piece) + piece.append('R') + break + case 4: + piece.classList.add('wb') + square.append(piece) + piece.append('B') + break + case -4: + piece.classList.add('bb') + square.append(piece) + piece.append('B') + break + case 5: + piece.classList.add('wn') + square.append(piece) + piece.append('N') + break + case -5: + piece.classList.add('bn') + square.append(piece) + piece.append('N') + break + case 6: + piece.classList.add('wp') + square.append(piece) + piece.append('p') + break + case -6: + piece.classList.add('bp') + square.append(piece) + piece.append('p') + break + } + } + } + +} -- cgit v1.2.3