Update app.py
Browse files
app.py
CHANGED
|
@@ -1,213 +1,417 @@
|
|
| 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 |
-
for (
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import React, { useState, useEffect } from 'react';
|
| 2 |
+
import { Bomb } from 'lucide-react';
|
| 3 |
+
|
| 4 |
+
const ChessMinesweeper = () => {
|
| 5 |
+
const [board, setBoard] = useState([]);
|
| 6 |
+
const [selectedPiece, setSelectedPiece] = useState(null);
|
| 7 |
+
const [currentPlayer, setCurrentPlayer] = useState('white');
|
| 8 |
+
const [gameOver, setGameOver] = useState(false);
|
| 9 |
+
const [message, setMessage] = useState('');
|
| 10 |
+
const [capturedPieces, setCapturedPieces] = useState({ white: [], black: [] });
|
| 11 |
+
|
| 12 |
+
const pieceSymbols = {
|
| 13 |
+
white: { king: '♔', queen: '♕', rook: '♖', bishop: '♗', knight: '♘', pawn: '♙' },
|
| 14 |
+
black: { king: '♚', queen: '♛', rook: '♜', bishop: '♝', knight: '♞', pawn: '♟' }
|
| 15 |
+
};
|
| 16 |
+
|
| 17 |
+
useEffect(() => {
|
| 18 |
+
initBoard();
|
| 19 |
+
}, []);
|
| 20 |
+
|
| 21 |
+
const initBoard = () => {
|
| 22 |
+
const newBoard = Array(8).fill(null).map(() => Array(8).fill(null));
|
| 23 |
+
|
| 24 |
+
// Placer les pièces blanches (rangée 7 et 6)
|
| 25 |
+
newBoard[7][0] = { type: 'rook', color: 'white' };
|
| 26 |
+
newBoard[7][1] = { type: 'knight', color: 'white' };
|
| 27 |
+
newBoard[7][2] = { type: 'bishop', color: 'white' };
|
| 28 |
+
newBoard[7][3] = { type: 'queen', color: 'white' };
|
| 29 |
+
newBoard[7][4] = { type: 'king', color: 'white' };
|
| 30 |
+
newBoard[7][5] = { type: 'bishop', color: 'white' };
|
| 31 |
+
newBoard[7][6] = { type: 'knight', color: 'white' };
|
| 32 |
+
newBoard[7][7] = { type: 'rook', color: 'white' };
|
| 33 |
+
for (let i = 0; i < 8; i++) {
|
| 34 |
+
newBoard[6][i] = { type: 'pawn', color: 'white', hasMoved: false };
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
// Placer les pièces noires (rangée 0 et 1)
|
| 38 |
+
newBoard[0][0] = { type: 'rook', color: 'black' };
|
| 39 |
+
newBoard[0][1] = { type: 'knight', color: 'black' };
|
| 40 |
+
newBoard[0][2] = { type: 'bishop', color: 'black' };
|
| 41 |
+
newBoard[0][3] = { type: 'queen', color: 'black' };
|
| 42 |
+
newBoard[0][4] = { type: 'king', color: 'black' };
|
| 43 |
+
newBoard[0][5] = { type: 'bishop', color: 'black' };
|
| 44 |
+
newBoard[0][6] = { type: 'knight', color: 'black' };
|
| 45 |
+
newBoard[0][7] = { type: 'rook', color: 'black' };
|
| 46 |
+
for (let i = 0; i < 8; i++) {
|
| 47 |
+
newBoard[1][i] = { type: 'pawn', color: 'black', hasMoved: false };
|
| 48 |
+
}
|
| 49 |
+
|
| 50 |
+
// Générer les mines dans les rangées du milieu (2-5)
|
| 51 |
+
const mines = new Set();
|
| 52 |
+
const mineCount = 12;
|
| 53 |
+
|
| 54 |
+
while (mines.size < mineCount) {
|
| 55 |
+
const row = 2 + Math.floor(Math.random() * 4);
|
| 56 |
+
const col = Math.floor(Math.random() * 8);
|
| 57 |
+
mines.add(`${row},${col}`);
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Ajouter les mines et calculer les nombres adjacents
|
| 61 |
+
for (let row = 2; row <= 5; row++) {
|
| 62 |
+
for (let col = 0; col < 8; col++) {
|
| 63 |
+
const key = `${row},${col}`;
|
| 64 |
+
if (mines.has(key)) {
|
| 65 |
+
newBoard[row][col] = { type: 'mine', revealed: false };
|
| 66 |
+
} else {
|
| 67 |
+
const adjacentMines = countAdjacentMines(row, col, mines);
|
| 68 |
+
newBoard[row][col] = { type: 'empty', revealed: false, count: adjacentMines };
|
| 69 |
+
}
|
| 70 |
+
}
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
// Ajouter les indices de mines pour les rangées 1 et 6 (où sont les pions)
|
| 74 |
+
for (let col = 0; col < 8; col++) {
|
| 75 |
+
// Rangée 1 (pions noirs)
|
| 76 |
+
const count1 = countAdjacentMines(1, col, mines);
|
| 77 |
+
if (count1 > 0) {
|
| 78 |
+
newBoard[1][col] = { ...newBoard[1][col], mineCount: count1 };
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// Rangée 6 (pions blancs)
|
| 82 |
+
const count6 = countAdjacentMines(6, col, mines);
|
| 83 |
+
if (count6 > 0) {
|
| 84 |
+
newBoard[6][col] = { ...newBoard[6][col], mineCount: count6 };
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
setBoard(newBoard);
|
| 89 |
+
setCurrentPlayer('white');
|
| 90 |
+
setGameOver(false);
|
| 91 |
+
setMessage('');
|
| 92 |
+
setCapturedPieces({ white: [], black: [] });
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
const countAdjacentMines = (row, col, mines) => {
|
| 96 |
+
let count = 0;
|
| 97 |
+
for (let dr = -1; dr <= 1; dr++) {
|
| 98 |
+
for (let dc = -1; dc <= 1; dc++) {
|
| 99 |
+
if (dr === 0 && dc === 0) continue;
|
| 100 |
+
const nr = row + dr;
|
| 101 |
+
const nc = col + dc;
|
| 102 |
+
// Vérifier toutes les cases adjacentes, pas seulement la zone démineur
|
| 103 |
+
if (nr >= 0 && nr < 8 && nc >= 0 && nc < 8) {
|
| 104 |
+
if (mines.has(`${nr},${nc}`)) count++;
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
return count;
|
| 109 |
+
};
|
| 110 |
+
|
| 111 |
+
const handleSquareClick = (row, col) => {
|
| 112 |
+
if (gameOver) return;
|
| 113 |
+
|
| 114 |
+
const cell = board[row][col];
|
| 115 |
+
const actualPiece = cell?.piece || cell;
|
| 116 |
+
|
| 117 |
+
// Si on clique sur une pièce
|
| 118 |
+
if (actualPiece && actualPiece.color && ['king', 'queen', 'rook', 'bishop', 'knight', 'pawn'].includes(actualPiece.type)) {
|
| 119 |
+
if (actualPiece.color === currentPlayer) {
|
| 120 |
+
setSelectedPiece({ row, col });
|
| 121 |
+
return;
|
| 122 |
+
}
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Si on a une pièce sélectionnée, essayer de la déplacer
|
| 126 |
+
if (selectedPiece) {
|
| 127 |
+
movePiece(selectedPiece.row, selectedPiece.col, row, col);
|
| 128 |
+
}
|
| 129 |
+
};
|
| 130 |
+
|
| 131 |
+
const movePiece = (fromRow, fromCol, toRow, toCol) => {
|
| 132 |
+
const fromCell = board[fromRow][fromCol];
|
| 133 |
+
const piece = fromCell?.piece || fromCell;
|
| 134 |
+
const toCell = board[toRow][toCol];
|
| 135 |
+
const targetPiece = toCell?.piece || toCell;
|
| 136 |
+
|
| 137 |
+
if (!piece || !['king', 'queen', 'rook', 'bishop', 'knight', 'pawn'].includes(piece.type)) {
|
| 138 |
+
setSelectedPiece(null);
|
| 139 |
+
return;
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
// Vérifier que le mouvement est valide
|
| 143 |
+
if (!isValidMove(fromRow, fromCol, toRow, toCol, piece)) {
|
| 144 |
+
setSelectedPiece(null);
|
| 145 |
+
return;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
const newBoard = board.map(row => [...row]);
|
| 149 |
+
let newMessage = '';
|
| 150 |
+
let pieceCaptured = false;
|
| 151 |
+
|
| 152 |
+
// Si on capture une pièce ennemie
|
| 153 |
+
if (targetPiece && targetPiece.color && targetPiece.color !== piece.color) {
|
| 154 |
+
const newCaptured = { ...capturedPieces };
|
| 155 |
+
newCaptured[targetPiece.color].push(targetPiece.type);
|
| 156 |
+
setCapturedPieces(newCaptured);
|
| 157 |
+
pieceCaptured = true;
|
| 158 |
+
newMessage = `${piece.type} ${piece.color} capture ${targetPiece.type} ${targetPiece.color}!`;
|
| 159 |
+
}
|
| 160 |
+
|
| 161 |
+
// Si on se déplace sur une case du démineur
|
| 162 |
+
const isMinesweeperZone = toRow >= 2 && toRow <= 5;
|
| 163 |
+
if (isMinesweeperZone && toCell && (toCell.type === 'mine' || toCell.type === 'empty')) {
|
| 164 |
+
// Si c'est une mine (révélée ou non)
|
| 165 |
+
if (toCell.type === 'mine') {
|
| 166 |
+
// BOOM! La pièce est éliminée
|
| 167 |
+
newBoard[fromRow][fromCol] = fromCell.revealed ? { ...fromCell, piece: null } : null;
|
| 168 |
+
newBoard[toRow][toCol] = { type: 'mine', revealed: true };
|
| 169 |
+
newMessage = `💥 BOOM! ${piece.type} ${piece.color} a explosé sur une mine!`;
|
| 170 |
+
|
| 171 |
+
const newCaptured = { ...capturedPieces };
|
| 172 |
+
newCaptured[piece.color].push(piece.type);
|
| 173 |
+
setCapturedPieces(newCaptured);
|
| 174 |
+
|
| 175 |
+
if (piece.type === 'king') {
|
| 176 |
+
setGameOver(true);
|
| 177 |
+
newMessage += ` ${piece.color === 'white' ? 'Noirs' : 'Blancs'} gagnent!`;
|
| 178 |
+
}
|
| 179 |
+
} else if (!toCell.revealed) {
|
| 180 |
+
// Case sûre non révélée, révéler le nombre
|
| 181 |
+
const updatedPiece = piece.type === 'pawn' ? { ...piece, hasMoved: true } : piece;
|
| 182 |
+
newBoard[toRow][toCol] = { ...toCell, revealed: true, piece: updatedPiece };
|
| 183 |
+
newBoard[fromRow][fromCol] = fromCell.revealed ? { ...fromCell, piece: null } : null;
|
| 184 |
+
if (!pieceCaptured) {
|
| 185 |
+
newMessage = `Case révélée: ${toCell.count} mine(s) adjacente(s)`;
|
| 186 |
+
}
|
| 187 |
+
} else {
|
| 188 |
+
// Case sûre déjà révélée
|
| 189 |
+
const updatedPiece = piece.type === 'pawn' ? { ...piece, hasMoved: true } : piece;
|
| 190 |
+
newBoard[toRow][toCol] = { ...toCell, piece: updatedPiece };
|
| 191 |
+
newBoard[fromRow][fromCol] = fromCell.revealed ? { ...fromCell, piece: null } : null;
|
| 192 |
+
}
|
| 193 |
+
} else if (toCell && toCell.revealed && toCell.type === 'empty') {
|
| 194 |
+
// Se déplacer sur une case déjà révélée
|
| 195 |
+
const updatedPiece = piece.type === 'pawn' ? { ...piece, hasMoved: true } : piece;
|
| 196 |
+
newBoard[toRow][toCol] = { ...toCell, piece: updatedPiece };
|
| 197 |
+
newBoard[fromRow][fromCol] = fromCell.revealed ? { ...fromCell, piece: null } : null;
|
| 198 |
+
} else {
|
| 199 |
+
// Se déplacer sur une case normale (hors zone démineur ou pièce ennemie)
|
| 200 |
+
const updatedPiece = piece.type === 'pawn' ? { ...piece, hasMoved: true } : piece;
|
| 201 |
+
newBoard[toRow][toCol] = updatedPiece;
|
| 202 |
+
newBoard[fromRow][fromCol] = fromCell.revealed ? { ...fromCell, piece: null } : null;
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
setBoard(newBoard);
|
| 206 |
+
setSelectedPiece(null);
|
| 207 |
+
setMessage(newMessage);
|
| 208 |
+
|
| 209 |
+
if (!gameOver) {
|
| 210 |
+
setCurrentPlayer(currentPlayer === 'white' ? 'black' : 'white');
|
| 211 |
+
}
|
| 212 |
+
};
|
| 213 |
+
|
| 214 |
+
const isValidMove = (fromRow, fromCol, toRow, toCol, piece) => {
|
| 215 |
+
if (fromRow === toRow && fromCol === toCol) return false;
|
| 216 |
+
|
| 217 |
+
const toCell = board[toRow][toCol];
|
| 218 |
+
const targetPiece = toCell?.piece || toCell;
|
| 219 |
+
|
| 220 |
+
// Ne peut pas capturer sa propre pièce
|
| 221 |
+
if (targetPiece && targetPiece.color === piece.color) return false;
|
| 222 |
+
|
| 223 |
+
const rowDiff = toRow - fromRow;
|
| 224 |
+
const colDiff = toCol - fromCol;
|
| 225 |
+
|
| 226 |
+
switch (piece.type) {
|
| 227 |
+
case 'pawn':
|
| 228 |
+
return isValidPawnMove(fromRow, fromCol, toRow, toCol, piece, targetPiece);
|
| 229 |
+
case 'rook':
|
| 230 |
+
return isValidRookMove(fromRow, fromCol, toRow, toCol);
|
| 231 |
+
case 'knight':
|
| 232 |
+
return isValidKnightMove(rowDiff, colDiff);
|
| 233 |
+
case 'bishop':
|
| 234 |
+
return isValidBishopMove(fromRow, fromCol, toRow, toCol);
|
| 235 |
+
case 'queen':
|
| 236 |
+
return isValidQueenMove(fromRow, fromCol, toRow, toCol);
|
| 237 |
+
case 'king':
|
| 238 |
+
return isValidKingMove(rowDiff, colDiff);
|
| 239 |
+
default:
|
| 240 |
+
return false;
|
| 241 |
+
}
|
| 242 |
+
};
|
| 243 |
+
|
| 244 |
+
const isValidPawnMove = (fromRow, fromCol, toRow, toCol, piece, targetPiece) => {
|
| 245 |
+
const direction = piece.color === 'white' ? -1 : 1;
|
| 246 |
+
const rowDiff = toRow - fromRow;
|
| 247 |
+
const colDiff = Math.abs(toCol - fromCol);
|
| 248 |
+
|
| 249 |
+
// Capture en diagonale
|
| 250 |
+
if (rowDiff === direction && colDiff === 1) {
|
| 251 |
+
return targetPiece && targetPiece.color && targetPiece.color !== piece.color;
|
| 252 |
+
}
|
| 253 |
+
|
| 254 |
+
// Avancer d'une case
|
| 255 |
+
if (rowDiff === direction && colDiff === 0) {
|
| 256 |
+
return !targetPiece || !targetPiece.color;
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
// Premier mouvement: avancer de 2 cases
|
| 260 |
+
if (!piece.hasMoved && colDiff === 0 && rowDiff === direction * 2) {
|
| 261 |
+
const middleRow = fromRow + direction;
|
| 262 |
+
const middleCell = board[middleRow][fromCol];
|
| 263 |
+
const middlePiece = middleCell?.piece || middleCell;
|
| 264 |
+
return (!targetPiece || !targetPiece.color) && (!middlePiece || !middlePiece.color);
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
return false;
|
| 268 |
+
};
|
| 269 |
+
|
| 270 |
+
const isValidRookMove = (fromRow, fromCol, toRow, toCol) => {
|
| 271 |
+
if (fromRow !== toRow && fromCol !== toCol) return false;
|
| 272 |
+
return isPathClear(fromRow, fromCol, toRow, toCol);
|
| 273 |
+
};
|
| 274 |
+
|
| 275 |
+
const isValidKnightMove = (rowDiff, colDiff) => {
|
| 276 |
+
return (Math.abs(rowDiff) === 2 && Math.abs(colDiff) === 1) ||
|
| 277 |
+
(Math.abs(rowDiff) === 1 && Math.abs(colDiff) === 2);
|
| 278 |
+
};
|
| 279 |
+
|
| 280 |
+
const isValidBishopMove = (fromRow, fromCol, toRow, toCol) => {
|
| 281 |
+
if (Math.abs(toRow - fromRow) !== Math.abs(toCol - fromCol)) return false;
|
| 282 |
+
return isPathClear(fromRow, fromCol, toRow, toCol);
|
| 283 |
+
};
|
| 284 |
+
|
| 285 |
+
const isValidQueenMove = (fromRow, fromCol, toRow, toCol) => {
|
| 286 |
+
return isValidRookMove(fromRow, fromCol, toRow, toCol) ||
|
| 287 |
+
isValidBishopMove(fromRow, fromCol, toRow, toCol);
|
| 288 |
+
};
|
| 289 |
+
|
| 290 |
+
const isValidKingMove = (rowDiff, colDiff) => {
|
| 291 |
+
return Math.abs(rowDiff) <= 1 && Math.abs(colDiff) <= 1;
|
| 292 |
+
};
|
| 293 |
+
|
| 294 |
+
const isPathClear = (fromRow, fromCol, toRow, toCol) => {
|
| 295 |
+
const rowStep = toRow > fromRow ? 1 : toRow < fromRow ? -1 : 0;
|
| 296 |
+
const colStep = toCol > fromCol ? 1 : toCol < fromCol ? -1 : 0;
|
| 297 |
+
|
| 298 |
+
let currentRow = fromRow + rowStep;
|
| 299 |
+
let currentCol = fromCol + colStep;
|
| 300 |
+
|
| 301 |
+
while (currentRow !== toRow || currentCol !== toCol) {
|
| 302 |
+
const cell = board[currentRow][currentCol];
|
| 303 |
+
const piece = cell?.piece || cell;
|
| 304 |
+
if (piece && piece.color) return false;
|
| 305 |
+
currentRow += rowStep;
|
| 306 |
+
currentCol += colStep;
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
return true;
|
| 310 |
+
};
|
| 311 |
+
|
| 312 |
+
const renderSquare = (row, col) => {
|
| 313 |
+
const cell = board[row][col];
|
| 314 |
+
const piece = cell?.piece || cell;
|
| 315 |
+
const isSelected = selectedPiece && selectedPiece.row === row && selectedPiece.col === col;
|
| 316 |
+
const isDark = (row + col) % 2 === 1;
|
| 317 |
+
|
| 318 |
+
let bgColor = isDark ? 'bg-amber-700' : 'bg-amber-100';
|
| 319 |
+
if (isSelected) bgColor = 'bg-blue-400';
|
| 320 |
+
|
| 321 |
+
const isMinesweeperZone = row >= 2 && row <= 5;
|
| 322 |
+
if (isMinesweeperZone && cell && !cell.revealed) {
|
| 323 |
+
bgColor = isDark ? 'bg-gray-600' : 'bg-gray-400';
|
| 324 |
+
} else if (isMinesweeperZone && cell && cell.revealed) {
|
| 325 |
+
// Cases révélées en gris clair comme dans le vrai démineur
|
| 326 |
+
bgColor = 'bg-gray-300';
|
| 327 |
+
if (isSelected) bgColor = 'bg-blue-400';
|
| 328 |
+
}
|
| 329 |
+
|
| 330 |
+
return (
|
| 331 |
+
<div
|
| 332 |
+
key={`${row}-${col}`}
|
| 333 |
+
onClick={() => handleSquareClick(row, col)}
|
| 334 |
+
className={`w-16 h-16 flex items-center justify-center cursor-pointer border border-gray-800 ${bgColor} hover:opacity-80 transition-all relative`}
|
| 335 |
+
>
|
| 336 |
+
{piece && piece.color && pieceSymbols[piece.color][piece.type] && (
|
| 337 |
+
<>
|
| 338 |
+
<div className={`text-5xl ${piece.color === 'white' ? 'text-white' : 'text-gray-900'} drop-shadow-lg`}>
|
| 339 |
+
{pieceSymbols[piece.color][piece.type]}
|
| 340 |
+
</div>
|
| 341 |
+
{piece.mineCount > 0 && (row === 1 || row === 6) && (
|
| 342 |
+
<div className="absolute bottom-1 right-1 text-xs font-bold bg-yellow-400 rounded-full w-5 h-5 flex items-center justify-center">
|
| 343 |
+
{piece.mineCount}
|
| 344 |
+
</div>
|
| 345 |
+
)}
|
| 346 |
+
</>
|
| 347 |
+
)}
|
| 348 |
+
{cell && cell.revealed && cell.type === 'mine' && (
|
| 349 |
+
<Bomb className="text-red-600" size={32} />
|
| 350 |
+
)}
|
| 351 |
+
{cell && cell.revealed && cell.type === 'empty' && cell.count > 0 && !cell.piece && (
|
| 352 |
+
<div className="text-2xl font-bold text-blue-600">
|
| 353 |
+
{cell.count}
|
| 354 |
+
</div>
|
| 355 |
+
)}
|
| 356 |
+
{cell && cell.revealed && cell.type === 'empty' && cell.piece && (
|
| 357 |
+
<div className="absolute bottom-1 right-1 text-xs font-bold bg-yellow-400 rounded-full w-5 h-5 flex items-center justify-center">
|
| 358 |
+
{cell.count}
|
| 359 |
+
</div>
|
| 360 |
+
)}
|
| 361 |
+
</div>
|
| 362 |
+
);
|
| 363 |
+
};
|
| 364 |
+
|
| 365 |
+
return (
|
| 366 |
+
<div className="flex flex-col items-center justify-center min-h-screen bg-gradient-to-br from-slate-800 to-slate-900 p-8">
|
| 367 |
+
<h1 className="text-4xl font-bold text-white mb-4">♔ Échecs Démineur ♚</h1>
|
| 368 |
+
|
| 369 |
+
<div className="mb-4 text-center">
|
| 370 |
+
<div className="text-xl text-white mb-2">
|
| 371 |
+
Tour: <span className={currentPlayer === 'white' ? 'text-white font-bold' : 'text-gray-300 font-bold'}>
|
| 372 |
+
{currentPlayer === 'white' ? 'Blancs ♔' : 'Noirs ♚'}
|
| 373 |
+
</span>
|
| 374 |
+
</div>
|
| 375 |
+
{message && (
|
| 376 |
+
<div className="bg-yellow-500 text-black px-4 py-2 rounded-lg font-semibold max-w-md">
|
| 377 |
+
{message}
|
| 378 |
+
</div>
|
| 379 |
+
)}
|
| 380 |
+
</div>
|
| 381 |
+
|
| 382 |
+
<div className="flex gap-8 mb-4">
|
| 383 |
+
<div className="bg-gray-800 p-3 rounded-lg">
|
| 384 |
+
<div className="text-white font-bold mb-2">Pièces capturées (Blancs)</div>
|
| 385 |
+
<div className="text-3xl">{capturedPieces.white.map((p, i) => pieceSymbols.white[p]).join(' ')}</div>
|
| 386 |
+
</div>
|
| 387 |
+
<div className="bg-gray-800 p-3 rounded-lg">
|
| 388 |
+
<div className="text-white font-bold mb-2">Pièces capturées (Noirs)</div>
|
| 389 |
+
<div className="text-3xl">{capturedPieces.black.map((p, i) => pieceSymbols.black[p]).join(' ')}</div>
|
| 390 |
+
</div>
|
| 391 |
+
</div>
|
| 392 |
+
|
| 393 |
+
<div className="inline-block border-4 border-gray-900 shadow-2xl mb-4">
|
| 394 |
+
{board.map((row, rowIndex) => (
|
| 395 |
+
<div key={rowIndex} className="flex">
|
| 396 |
+
{row.map((_, colIndex) => renderSquare(rowIndex, colIndex))}
|
| 397 |
+
</div>
|
| 398 |
+
))}
|
| 399 |
+
</div>
|
| 400 |
+
|
| 401 |
+
<button
|
| 402 |
+
onClick={initBoard}
|
| 403 |
+
className="bg-blue-600 hover:bg-blue-700 text-white font-bold py-3 px-6 rounded-lg transition-colors"
|
| 404 |
+
>
|
| 405 |
+
Nouvelle Partie
|
| 406 |
+
</button>
|
| 407 |
+
|
| 408 |
+
<div className="mt-4 text-gray-300 text-sm max-w-md text-center">
|
| 409 |
+
<p>♟️ Règles complètes des échecs appliquées</p>
|
| 410 |
+
<p>💣 Attention: Les 4 rangées du milieu cachent des mines!</p>
|
| 411 |
+
<p>🎯 Capturez le roi ennemi ou faites-le exploser pour gagner</p>
|
| 412 |
+
</div>
|
| 413 |
+
</div>
|
| 414 |
+
);
|
| 415 |
+
};
|
| 416 |
+
|
| 417 |
+
export default ChessMinesweeper;
|