forked from chess-mazes/chess-mazes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moveValidator.js
85 lines (76 loc) · 3.66 KB
/
moveValidator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function isValidMove(board, startCol, startRow, endCol, endRow, verbose = false, blackToMove = false) {
let piece = board[startRow * 8 + startCol];
if (piece === "") return false;
if (blackToMove) {
if (piece.toLowerCase() !== piece) {
if (verbose) console.log("invalid - white piece");
return false;
}
} else if (piece.toLowerCase() == piece) {
if (verbose) console.log("invalid - black piece");
return false;
}
piece = piece.toLowerCase();
if (piece == "p") {
const direction = blackToMove ? 1 : -1;
// Check if the pawn is moving to next row
if (endRow !== startRow + direction) {
if (verbose) console.log("invalid - not moving to next row");
return false;
}
// Check if pawn is moving diagonally to either side
if (endCol !== startCol - 1 && endCol !== startCol + 1) {
if (verbose) console.log("invalid - not moving diagonally");
return false;
}
// Check if the pawn is moving to a square with a friendly piece
if (board[endRow * 8 + endCol].toLowerCase() === board[endRow * 8 + endCol]) {
if (verbose) console.log("invalid - moving to friendly square");
return false;
}
} else if (piece === "b") {
// Check if the bishop is moving diagonally
if (Math.abs(endRow - startRow) !== Math.abs(endCol - startCol)) {
if (verbose) console.log("invalid - not moving diagonally")
return false;
}
} else if (piece === "n") {
// Check if the knight is moving two squares vertically and one square horizontally, in any direction
if (!((Math.abs(endRow - startRow) === 2 && Math.abs(endCol - startCol) === 1) || (Math.abs(endRow - startRow) === 1 && Math.abs(endCol - startCol) === 2))) {
if (verbose) console.log("invalid - not moving two squares vertically and one square horizontally");
return false;
}
} else if (piece === "k") {
// Check if the king is moving more than one square
if (Math.abs(endRow - startRow) > 1 || Math.abs(endCol - startCol) > 1) {
if (verbose) console.log("invalid - moving more than one square");
return false;
}
} else if (piece === "r") {
// Check if the rook is moving horizontally or vertically
if (endRow !== startRow && endCol !== startCol) {
if (verbose) console.log("invalid - not moving horizontally or vertically");
return false;
}
} else if (piece === "q") {
// Check if the queen is moving horizontally, vertically, or diagonally
if (endRow !== startRow && endCol !== startCol && Math.abs(endRow - startRow) !== Math.abs(endCol - startCol)) {
if (verbose) console.log("invalid - not moving horizontally, vertically, or diagonally");
return false;
}
}
// Check if blocked by another piece
if (piece === "b" || piece === "r" || piece === "q") {
for (let i = 1; i < Math.max(Math.abs(endRow - startRow), Math.abs(endCol - startCol)); i++) {
let row = startRow + Math.sign(endRow - startRow) * i;
let col = startCol + Math.sign(endCol - startCol) * i;
if (board[row * 8 + col] !== "") {
if (verbose) console.log("invalid - blocked by another piece");
return false;
}
}
}
if (verbose) console.log(`valid - from (${startCol}, ${startRow}) to (${endCol}, ${endRow})`)
return true;
}
export default isValidMove;