summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIgrisBRC <[email protected]>2024-07-20 11:24:15 +0530
committerIgrisBRC <[email protected]>2024-07-20 11:24:15 +0530
commitfce0d5b41707facbc2c50b074fe63002b2e3611c (patch)
tree5fa38b41baa3d6548f00e033f788ff7d4ba1916e
parentbec75ae5ebcc8c5df9b0be3115370ed7013127a1 (diff)
made en passant, there might be bugs but I haven't found them yet
-rw-r--r--public/board.js21
-rw-r--r--public/make_move.js45
-rw-r--r--public/move.js26
3 files changed, 80 insertions, 12 deletions
diff --git a/public/board.js b/public/board.js
index bd39363..785efff 100644
--- a/public/board.js
+++ b/public/board.js
@@ -56,8 +56,7 @@ function handle_move(from_id, to_id) {
prev_y_x.push([from_y, from_x])
new_y_x.push([to_y, to_x])
- if(Math.abs(from) == 1 && to == 0){
- console.log("valid")
+ if (Math.abs(from) == 1 && to == 0) {
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])
@@ -101,6 +100,24 @@ function handle_move(from_id, to_id) {
make_move(board, prev_y_x, new_y_x)
+ if (en_passant_move >= 0) {
+ if (from_color) {
+ let passanted_pawn = document.getElementById(`3${en_passant_move}`)
+
+ while (passanted_pawn.firstChild) {
+ passanted_pawn.removeChild(passanted_pawn.firstChild)
+ }
+ } else {
+ let passanted_pawn = document.getElementById(`4${en_passant_move}`)
+
+ while (passanted_pawn.firstChild) {
+ passanted_pawn.removeChild(passanted_pawn.firstChild)
+ }
+ }
+
+ en_passant_move = -1
+ }
+
let from_element = document.getElementById(from_id)
let to_element = document.getElementById(to_id)
diff --git a/public/make_move.js b/public/make_move.js
index 6233ed0..07bda02 100644
--- a/public/make_move.js
+++ b/public/make_move.js
@@ -12,27 +12,58 @@
function make_move(board, prev_y_x, new_y_x) {
+ if ((board[prev_y_x[0][0]][prev_y_x[0][1]] == 6 || board[prev_y_x[0][0]][prev_y_x[0][1]] == -6) && board[new_y_x[0][0]][new_y_x[0][1]] == 0 && new_y_x[0][1] != prev_y_x[0][1]) {
+ console.log("passant")
+ console.log(en_passant)
+
+ if (board[prev_y_x[0][0]][prev_y_x[0][1]] == 6) {
+ board[3][en_passant] = 0
+ } else {
+ board[4][en_passant] = 0
+ }
+
+ en_passant_move = en_passant
+ }
+
+ en_passant = -1
+
for (let i = 0; i < new_y_x.length; i++) {
+ if (board[prev_y_x[i][0]][prev_y_x[i][1]] == 6 && prev_y_x[i][0] - new_y_x[i][0] == 2) {
+ en_passant = prev_y_x[i][1]
+ }
+
+ if (board[prev_y_x[i][0]][prev_y_x[i][1]] == -6 && prev_y_x[i][0] - new_y_x[i][0] == -2) {
+ en_passant = prev_y_x[i][1]
+ }
+
+
board[new_y_x[i][0]][new_y_x[i][1]] = board[prev_y_x[i][0]][prev_y_x[i][1]]
}
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
+ 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 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 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
}
+
+ check = check_check(board, to_move, check)
+ if (check) {
+ console.log("check")
+ }
+
+ console.log(board)
}
diff --git a/public/move.js b/public/move.js
index 87de976..294ace5 100644
--- a/public/move.js
+++ b/public/move.js
@@ -15,12 +15,14 @@ let check = false
//console.log(board[7][1])
//console.log(move(board, 4, 4, to_move))
-
let castle_white_short = true
let castle_white_long = true
let castle_black_short = true
let castle_black_long = true
+let en_passant = -1
+let en_passant_move = -1
+
function check_check(board, to_move, check) {
let king_position = []
@@ -160,6 +162,24 @@ function pawn_move(board, y, x) {
}
}
+ if (en_passant > 0 && color && y == 3) {
+ if (x - 1 == en_passant) {
+ moves.push([2, x - 1])
+ }
+ if (x + 1 == en_passant) {
+ moves.push([2, x + 1])
+ }
+ }
+
+ if (en_passant > 0 && !color && y == 4) {
+ if (x - 1 == en_passant) {
+ moves.push([5, x - 1, 1])
+ }
+ if (x + 1 == en_passant) {
+ moves.push([5, x + 1, 1])
+ }
+ }
+
return moves
}
@@ -168,7 +188,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 +199,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)) {