From 6a44c47fcef5f645879780b0b5f287cfb60be088 Mon Sep 17 00:00:00 2001 From: IgrisBRC Date: Mon, 15 Jul 2024 17:18:34 +0530 Subject: added check detection, for now only console loggin when check --- public/move.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'public/move.js') diff --git a/public/move.js b/public/move.js index f7d8d4c..89bd3de 100644 --- a/public/move.js +++ b/public/move.js @@ -10,10 +10,75 @@ let board = [ ] let to_move = true //white to move +let check = false //console.log(board[7][1]) //console.log(move(board, 4, 4, to_move)) +function check_check(board, to_move, check) { + let king_position = [] + + if (!to_move) { + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[i].length; j++) { + if (board[i][j] == 1) { + king_position = [i, j] + break + } + } + } + } else { + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[i].length; j++) { + if (board[i][j] == -1) { + king_position = [i, j] + break + } + } + } + } + + let controlled = controlled_squares(board, !to_move) + + for (let i = 0; i < controlled.length; i++) { + if (controlled[i][0] == king_position[0] && controlled[i][1] == king_position[1]) { + return true + } + } + + return false +} + +function controlled_squares(board, to_move) { + let squares = [] + + if (to_move) { + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[0].length; j++) { + if (board[i][j] < 0) { + let moves = move(board, i, j, !to_move) + for (let i = 0; i < moves.length; i++) { + squares.push(moves[i]) + } + } + } + } + } else { + for (let i = 0; i < board.length; i++) { + for (let j = 0; j < board[i].length; j++) { + if (board[i][j] > 0) { + let moves = move(board, i, j, !to_move) + for (let i = 0; i < moves.length; i++) { + squares.push(moves[i]) + } + } + } + } + } + + return squares +} + function move(board, y, x, to_move) { if (to_move ^ board[y][x] > 0) { return [] -- cgit v1.2.3