Skip to content
Snippets Groups Projects
Commit 07cf28a4 authored by Elarouti Adam's avatar Elarouti Adam
Browse files

dame roi fou cavalier

parent e706f75a
No related branches found
No related tags found
No related merge requests found
...@@ -222,6 +222,139 @@ function highlightMoves(piece, row, col) { ...@@ -222,6 +222,139 @@ function highlightMoves(piece, row, col) {
} }
} }
} }
if (piece.toLowerCase() === "k") {
let directions = [
{ dr: -1, dc: 0 }, // Haut
{ dr: 1, dc: 0 }, // Bas
{ dr: 0, dc: -1 }, // Gauche
{ dr: 0, dc: 1 }, // Droite
{ dr: -1, dc: -1 }, // Diagonale haut-gauche
{ dr: -1, dc: 1 }, // Diagonale haut-droite
{ dr: 1, dc: -1 }, // Diagonale bas-gauche
{ dr: 1, dc: 1 } // Diagonale bas-droite
];
for (let { dr, dc } of directions) {
let newRow = row + dr;
let newCol = col + dc;
// Vérifie que la nouvelle position est sur l'échiquier
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
if (board[newRow][newCol] === "" || noirs.includes(board[newRow][newCol])) {
moves.push({ row: newRow, col: newCol });
}
}
}
}
if (piece.toLowerCase() === "c") {
let movesL = [
{ dr: -2, dc: -1 }, { dr: -2, dc: 1 }, // Haut gauche, Haut droite
{ dr: 2, dc: -1 }, { dr: 2, dc: 1 }, // Bas gauche, Bas droite
{ dr: -1, dc: -2 }, { dr: -1, dc: 2 }, // Gauche haut, Droite haut
{ dr: 1, dc: -2 }, { dr: 1, dc: 2 } // Gauche bas, Droite bas
];
for (let { dr, dc } of movesL) {
let newRow = row + dr;
let newCol = col + dc;
// Vérifie que la nouvelle position est sur l'échiquier
if (newRow >= 0 && newRow < 8 && newCol >= 0 && newCol < 8) {
if (board[newRow][newCol] === "" || noirs.includes(board[newRow][newCol])) {
moves.push({ row: newRow, col: newCol });
}
}
}
}
if (piece.toLowerCase() === "q") {
// Déplacements verticaux (haut et bas)
for (let r = row - 1; r >= 0; r--) { // Haut
if (board[r][col] === "") {
moves.push({ row: r, col });
} else {
if (noirs.includes(board[r][col])) {
moves.push({ row: r, col }); // Capture possible
}
break; // Bloqué par une pièce
}
}
for (let r = row + 1; r < 8; r++) { // Bas
if (board[r][col] === "") {
moves.push({ row: r, col });
} else {
if (noirs.includes(board[r][col])) {
moves.push({ row: r, col });
}
break;
}
}
// Déplacements horizontaux (gauche et droite)
for (let c = col - 1; c >= 0; c--) { // Gauche
if (board[row][c] === "") {
moves.push({ row, col: c });
} else {
if (noirs.includes(board[row][c])) {
moves.push({ row, col: c });
}
break;
}
}
for (let c = col + 1; c < 8; c++) { // Droite
if (board[row][c] === "") {
moves.push({ row, col: c });
} else {
if (noirs.includes(board[row][c])) {
moves.push({ row, col: c });
}
break;
}
}
// Déplacements diagonaux (haut-gauche, haut-droite, bas-gauche, bas-droite)
for (let r = row - 1, c = col - 1; r >= 0 && c >= 0; r--, c--) { // Haut-Gauche
if (board[r][c] === "") {
moves.push({ row: r, col: c });
} else {
if (noirs.includes(board[r][c])) {
moves.push({ row: r, col: c });
}
break;
}
}
for (let r = row - 1, c = col + 1; r >= 0 && c < 8; r--, c++) { // Haut-Droite
if (board[r][c] === "") {
moves.push({ row: r, col: c });
} else {
if (noirs.includes(board[r][c])) {
moves.push({ row: r, col: c });
}
break;
}
}
for (let r = row + 1, c = col - 1; r < 8 && c >= 0; r++, c--) { // Bas-Gauche
if (board[r][c] === "") {
moves.push({ row: r, col: c });
} else {
if (noirs.includes(board[r][c])) {
moves.push({ row: r, col: c });
}
break;
}
}
for (let r = row + 1, c = col + 1; r < 8 && c < 8; r++, c++) { // Bas-Droite
if (board[r][c] === "") {
moves.push({ row: r, col: c });
} else {
if (noirs.includes(board[r][c])) {
moves.push({ row: r, col: c });
}
break;
}
}
}
return moves; return moves;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment