File size: 24,351 Bytes
7bc796e 8dc0e70 7bc796e 294f52c 2cbb5b0 8dc0e70 294f52c 2cbb5b0 294f52c 39851f5 294f52c 8dc0e70 7bc796e 2cbb5b0 294f52c 2cbb5b0 8a91eec 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 39851f5 2cbb5b0 294f52c 39851f5 294f52c 2cbb5b0 294f52c 2cbb5b0 39851f5 294f52c 39851f5 7bc796e 9f671a2 7bc796e 8dc0e70 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 39851f5 294f52c 39851f5 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 39851f5 2cbb5b0 294f52c 2cbb5b0 39851f5 8a91eec 294f52c 44bedb5 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 39851f5 294f52c 34d1909 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 39851f5 2cbb5b0 34d1909 294f52c 2cbb5b0 8a91eec 39851f5 2cbb5b0 294f52c 39851f5 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 294f52c 2cbb5b0 39851f5 2cbb5b0 294f52c 8dc0e70 7bc796e 8dc0e70 |
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 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 |
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Jeu d'Échecs Tactile</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
.chess-board {
display: grid;
grid-template-columns: repeat(8, 1fr);
grid-template-rows: repeat(8, 1fr);
aspect-ratio: 1;
max-width: 600px;
margin: 0 auto;
border: 3px solid #374151;
border-radius: 8px;
overflow: hidden;
user-select: none;
}
.chess-square {
position: relative;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease;
}
.chess-square.light {
background-color: #f0d9b5;
}
.chess-square.dark {
background-color: #b58863;
}
.chess-square.selected {
box-shadow: inset 0 0 0 4px #fbbf24;
z-index: 2;
}
.chess-square.possible-move {
box-shadow: inset 0 0 0 3px #10b981;
}
.chess-square.possible-move:after {
content: '';
position: absolute;
width: 30%;
height: 30%;
background-color: #10b981;
border-radius: 50%;
opacity: 0.7;
}
.chess-square.last-move {
box-shadow: inset 0 0 0 3px #eab308;
}
.chess-piece {
font-size: clamp(2rem, 6vw, 3.5rem);
font-family: 'Segoe UI Symbol', 'Apple Symbols', sans-serif;
cursor: grab;
transition: transform 0.1s ease;
z-index: 1;
}
.chess-piece:hover {
transform: scale(1.1);
}
.chess-piece.dragging {
cursor: grabbing;
transform: scale(1.2);
z-index: 10;
pointer-events: none;
}
.coordinates {
color: #6b7280;
font-size: 0.75rem;
font-weight: bold;
}
.coord-file {
position: absolute;
bottom: 2px;
right: 4px;
}
.coord-rank {
position: absolute;
top: 2px;
left: 4px;
}
@media (max-width: 640px) {
.chess-piece {
font-size: 2rem;
}
.coordinates {
font-size: 0.6rem;
}
}
</style>
</head>
<body class="bg-gray-900 text-gray-100 min-h-screen flex flex-col items-center p-4">
<div class="container mx-auto max-w-6xl">
<h1 class="text-4xl font-bold text-center my-6 text-teal-400">Jeu d'Échecs Tactile</h1>
<div class="bg-gray-800 p-6 rounded-lg shadow-xl mb-6">
<h2 class="text-2xl font-semibold mb-3 text-sky-400">Configuration</h2>
<div class="flex flex-wrap gap-4 items-center">
<button id="setPvP" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Humain vs Humain
</button>
<div>
<button id="setPvAIWhite" class="bg-green-600 hover:bg-green-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Jouer vs IA (Blancs)
</button>
<button id="setPvAIBlack" class="bg-purple-600 hover:bg-purple-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline mt-2 sm:mt-0 sm:ml-2">
Jouer vs IA (Noirs)
</button>
</div>
<button id="resetGame" class="bg-red-600 hover:bg-red-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline">
Nouvelle Partie
</button>
</div>
<p class="mt-3 text-sm text-gray-400">Mode: <span id="currentMode" class="font-semibold">PVP</span></p>
<p id="playerColorInfo" class="mt-1 text-sm text-gray-400 hidden">
Vous jouez: <span id="currentPlayerColor" class="font-semibold">Blancs</span>
</p>
</div>
<div class="grid lg:grid-cols-4 gap-6">
<div class="lg:col-span-3 bg-gray-800 p-4 rounded-lg shadow-xl">
<div id="chessBoard" class="chess-board"></div>
<div class="mt-4 text-center text-sm text-gray-400">
<p>Cliquez sur une pièce puis sur la destination, ou glissez-déposez</p>
</div>
</div>
<div class="bg-gray-800 p-6 rounded-lg shadow-xl">
<h3 class="text-xl font-semibold mb-3 text-sky-400">Informations</h3>
<p id="turnDisplay" class="mb-2">Tour: <span class="font-bold text-white">Blancs</span></p>
<p id="status" class="text-yellow-400 font-semibold mb-4 min-h-6"></p>
<div id="outcomeDisplay" class="mb-4 text-lg font-bold text-center"></div>
<div class="space-y-3">
<div>
<p class="text-sm text-gray-400">Dernier coup:</p>
<p id="lastMove" class="text-gray-200 font-mono text-sm">-</p>
</div>
<div>
<p class="text-sm text-gray-400">Dernier coup IA:</p>
<p id="lastAIMove" class="text-gray-200 font-mono text-sm">-</p>
</div>
</div>
<div class="mt-6">
<h4 class="text-lg font-semibold mb-2 text-sky-400">Notation manuelle</h4>
<form id="moveForm" class="space-y-3">
<input type="text" id="moveInput"
class="w-full bg-gray-700 border border-gray-600 rounded-md py-2 px-3 focus:outline-none focus:ring-sky-500 focus:border-sky-500 text-white text-sm"
placeholder="e.g., e2e4, Nf3">
<button type="submit"
class="w-full bg-teal-600 hover:bg-teal-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline transition duration-150 ease-in-out">
Jouer
</button>
</form>
</div>
</div>
</div>
</div>
<script>
// Configuration du jeu
let gameMode = 'pvp';
let playerColor = 'white';
let isPlayerTurn = true;
let gameBoard = {};
let selectedSquare = null;
let possibleMoves = [];
let lastMoveSquares = [];
// Éléments DOM
const chessBoardEl = document.getElementById('chessBoard');
const moveForm = document.getElementById('moveForm');
const moveInput = document.getElementById('moveInput');
const statusDisplay = document.getElementById('status');
const turnDisplay = document.getElementById('turnDisplay').querySelector('span');
const outcomeDisplay = document.getElementById('outcomeDisplay');
const lastMoveDisplay = document.getElementById('lastMove');
const lastAIMoveDisplay = document.getElementById('lastAIMove');
const currentModeDisplay = document.getElementById('currentMode');
const playerColorInfoDisplay = document.getElementById('playerColorInfo');
const currentPlayerColorDisplay = document.getElementById('currentPlayerColor');
// Symboles des pièces d'échecs
const pieceSymbols = {
'K': '♔', 'Q': '♕', 'R': '♖', 'B': '♗', 'N': '♘', 'P': '♙',
'k': '♚', 'q': '♛', 'r': '♜', 'b': '♝', 'n': '♞', 'p': '♟'
};
// Position initiale FEN
let currentFEN = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
function createChessBoard() {
chessBoardEl.innerHTML = '';
for (let rank = 8; rank >= 1; rank--) {
for (let file = 0; file < 8; file++) {
const fileChar = String.fromCharCode(97 + file); // a-h
const square = fileChar + rank;
const squareEl = document.createElement('div');
squareEl.className = `chess-square ${(rank + file) % 2 === 0 ? 'light' : 'dark'}`;
squareEl.dataset.square = square;
// Ajouter les coordonnées
if (file === 0) {
const rankCoord = document.createElement('div');
rankCoord.className = 'coordinates coord-rank';
rankCoord.textContent = rank;
squareEl.appendChild(rankCoord);
}
if (rank === 1) {
const fileCoord = document.createElement('div');
fileCoord.className = 'coordinates coord-file';
fileCoord.textContent = fileChar;
squareEl.appendChild(fileCoord);
}
// Event listeners pour les interactions
squareEl.addEventListener('click', handleSquareClick);
squareEl.addEventListener('dragover', handleDragOver);
squareEl.addEventListener('drop', handleDrop);
chessBoardEl.appendChild(squareEl);
}
}
}
function updateBoardFromFEN(fen) {
const [boardPart] = fen.split(' ');
const ranks = boardPart.split('/');
gameBoard = {};
// Effacer toutes les pièces actuelles
document.querySelectorAll('.chess-piece').forEach(piece => piece.remove());
for (let rankIdx = 0; rankIdx < 8; rankIdx++) {
const rank = 8 - rankIdx;
const rankStr = ranks[rankIdx];
let fileIdx = 0;
for (let char of rankStr) {
if (isNaN(char)) {
// C'est une pièce
const file = String.fromCharCode(97 + fileIdx);
const square = file + rank;
gameBoard[square] = char;
const squareEl = document.querySelector(`[data-square="${square}"]`);
if (squareEl) {
const pieceEl = document.createElement('div');
pieceEl.className = 'chess-piece';
pieceEl.textContent = pieceSymbols[char];
pieceEl.draggable = true;
pieceEl.dataset.piece = char;
pieceEl.dataset.square = square;
pieceEl.addEventListener('dragstart', handleDragStart);
pieceEl.addEventListener('dragend', handleDragEnd);
squareEl.appendChild(pieceEl);
}
fileIdx++;
} else {
// C'est un nombre (cases vides)
fileIdx += parseInt(char);
}
}
}
currentFEN = fen;
updateTurnDisplay();
}
function updateTurnDisplay() {
const turn = currentFEN.split(' ')[1];
turnDisplay.textContent = turn === 'w' ? 'Blancs' : 'Noirs';
if (gameMode === 'ai') {
const aiIsWhite = (playerColor === 'black');
const aiIsBlack = (playerColor === 'white');
if ((aiIsWhite && turn === 'w') || (aiIsBlack && turn === 'b')) {
isPlayerTurn = false;
statusDisplay.textContent = "L'IA réfléchit...";
// En mode IA, on pourrait désactiver les interactions ici
} else {
isPlayerTurn = true;
statusDisplay.textContent = "À vous de jouer";
}
} else {
isPlayerTurn = true;
statusDisplay.textContent = "";
}
}
function handleSquareClick(e) {
if (!isPlayerTurn) return;
const square = e.currentTarget.dataset.square;
const piece = gameBoard[square];
if (selectedSquare) {
if (selectedSquare === square) {
// Déselection
clearSelection();
} else if (possibleMoves.includes(square)) {
// Coup valide
makeMove(selectedSquare + square);
} else if (piece && isPlayerPiece(piece)) {
// Sélection d'une autre pièce
selectSquare(square);
} else {
clearSelection();
}
} else if (piece && isPlayerPiece(piece)) {
selectSquare(square);
}
}
function selectSquare(square) {
clearSelection();
selectedSquare = square;
const squareEl = document.querySelector(`[data-square="${square}"]`);
squareEl.classList.add('selected');
// Simuler les coups possibles (ici on devrait faire appel au serveur)
showPossibleMoves(square);
}
function clearSelection() {
selectedSquare = null;
possibleMoves = [];
document.querySelectorAll('.chess-square').forEach(sq => {
sq.classList.remove('selected', 'possible-move');
});
}
function showPossibleMoves(fromSquare) {
// Pour l'instant, on simule quelques coups possibles
// Dans une vraie implémentation, on ferait appel au serveur
const piece = gameBoard[fromSquare];
possibleMoves = getPossibleMovesForPiece(fromSquare, piece);
possibleMoves.forEach(square => {
const squareEl = document.querySelector(`[data-square="${square}"]`);
if (squareEl) {
squareEl.classList.add('possible-move');
}
});
}
function getPossibleMovesForPiece(square, piece) {
// Simulation basique - dans la vraie version, ceci viendrait du serveur
const moves = [];
const file = square.charCodeAt(0) - 97;
const rank = parseInt(square[1]);
// Exemple pour un pion
if (piece.toLowerCase() === 'p') {
const direction = piece === 'P' ? 1 : -1;
const newRank = rank + direction;
if (newRank >= 1 && newRank <= 8) {
const newSquare = String.fromCharCode(97 + file) + newRank;
if (!gameBoard[newSquare]) {
moves.push(newSquare);
// Double move from starting position
if ((piece === 'P' && rank === 2) || (piece === 'p' && rank === 7)) {
const doubleSquare = String.fromCharCode(97 + file) + (rank + 2 * direction);
if (!gameBoard[doubleSquare]) {
moves.push(doubleSquare);
}
}
}
}
}
return moves;
}
function isPlayerPiece(piece) {
if (gameMode === 'pvp') return true;
const isWhitePiece = piece === piece.toUpperCase();
return (playerColor === 'white' && isWhitePiece) ||
(playerColor === 'black' && !isWhitePiece);
}
// Gestion du drag & drop
function handleDragStart(e) {
if (!isPlayerTurn) {
e.preventDefault();
return;
}
const piece = e.target.dataset.piece;
if (!isPlayerPiece(piece)) {
e.preventDefault();
return;
}
e.target.classList.add('dragging');
e.dataTransfer.setData('text/plain', e.target.dataset.square);
// Sélectionner la case pour montrer les coups possibles
selectSquare(e.target.dataset.square);
}
function handleDragEnd(e) {
e.target.classList.remove('dragging');
}
function handleDragOver(e) {
e.preventDefault();
}
function handleDrop(e) {
e.preventDefault();
const fromSquare = e.dataTransfer.getData('text/plain');
const toSquare = e.currentTarget.dataset.square;
if (fromSquare && toSquare && fromSquare !== toSquare) {
if (possibleMoves.includes(toSquare)) {
makeMove(fromSquare + toSquare);
}
}
clearSelection();
}
function highlightLastMove(move) {
// Effacer les anciens highlights
document.querySelectorAll('.chess-square').forEach(sq => {
sq.classList.remove('last-move');
});
if (move && move.length >= 4) {
const fromSquare = move.substring(0, 2);
const toSquare = move.substring(2, 4);
const fromEl = document.querySelector(`[data-square="${fromSquare}"]`);
const toEl = document.querySelector(`[data-square="${toSquare}"]`);
if (fromEl) fromEl.classList.add('last-move');
if (toEl) toEl.classList.add('last-move');
lastMoveSquares = [fromSquare, toSquare];
}
}
// Communication avec le serveur (adapté de votre code original)
async function makeMove(moveStr) {
statusDisplay.textContent = 'Traitement...';
clearSelection();
try {
const response = await fetch('/make_move', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ move: moveStr })
});
const data = await response.json();
if (data.error) {
statusDisplay.textContent = `Erreur: ${data.error}`;
} else {
updateBoardFromFEN(data.fen);
lastMoveDisplay.textContent = moveStr;
highlightLastMove(moveStr);
if (data.ai_move_uci) {
lastAIMoveDisplay.textContent = data.ai_move_uci;
highlightLastMove(data.ai_move_uci);
}
if (data.game_over) {
statusDisplay.textContent = "Partie terminée!";
outcomeDisplay.innerHTML = `<p class="text-green-400">${data.outcome}</p>`;
isPlayerTurn = false;
}
}
} catch (error) {
console.error("Erreur lors de la communication:", error);
statusDisplay.textContent = "Erreur de communication avec le serveur.";
}
}
// Form de saisie manuelle
moveForm.addEventListener('submit', async (e) => {
e.preventDefault();
const move = moveInput.value.trim();
if (!move) return;
await makeMove(move);
moveInput.value = '';
});
// Boutons de configuration
document.getElementById('resetGame').addEventListener('click', async () => {
statusDisplay.textContent = 'Réinitialisation...';
try {
const response = await fetch('/reset_game', { method: 'POST' });
const data = await response.json();
updateBoardFromFEN(data.fen);
lastMoveDisplay.textContent = "-";
lastAIMoveDisplay.textContent = "-";
outcomeDisplay.innerHTML = '';
clearSelection();
currentModeDisplay.textContent = data.game_mode ? data.game_mode.toUpperCase() : 'PVP';
if (data.game_mode === 'ai' && data.player_color) {
currentPlayerColorDisplay.textContent = data.player_color.charAt(0).toUpperCase() + data.player_color.slice(1);
playerColorInfoDisplay.classList.remove('hidden');
} else {
playerColorInfoDisplay.classList.add('hidden');
}
gameMode = data.game_mode || 'pvp';
playerColor = data.player_color || 'white';
updateTurnDisplay();
} catch (error) {
console.error("Erreur lors de la réinitialisation:", error);
statusDisplay.textContent = "Erreur lors de la réinitialisation.";
}
});
async function setGameMode(mode, pColor = 'white') {
statusDisplay.textContent = `Changement de mode vers ${mode.toUpperCase()}...`;
try {
const response = await fetch('/set_mode', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ game_mode: mode, player_color: pColor })
});
const data = await response.json();
if (data.error) {
statusDisplay.textContent = `Erreur: ${data.error}`;
} else {
gameMode = data.game_mode;
playerColor = data.player_color;
updateBoardFromFEN(data.fen);
clearSelection();
currentModeDisplay.textContent = gameMode.toUpperCase();
if (gameMode === 'ai') {
currentPlayerColorDisplay.textContent = playerColor.charAt(0).toUpperCase() + playerColor.slice(1);
playerColorInfoDisplay.classList.remove('hidden');
} else {
playerColorInfoDisplay.classList.add('hidden');
}
statusDisplay.textContent = data.message || '';
if (data.initial_ai_move_uci) {
lastAIMoveDisplay.textContent = data.initial_ai_move_uci;
highlightLastMove(data.initial_ai_move_uci);
}
}
} catch (error) {
console.error("Erreur de changement de mode:", error);
statusDisplay.textContent = "Erreur de changement de mode.";
}
}
document.getElementById('setPvP').addEventListener('click', () => setGameMode('pvp'));
document.getElementById('setPvAIWhite').addEventListener('click', () => setGameMode('ai', 'white'));
document.getElementById('setPvAIBlack').addEventListener('click', () => setGameMode('ai', 'black'));
// Initialisation
createChessBoard();
updateBoardFromFEN(currentFEN);
</script>
</body>
</html> |