diff --git a/htdocs/chess.js b/htdocs/chess.js deleted file mode 100644 index 22164937b88e66b718dc785801a972aee864d33e..0000000000000000000000000000000000000000 --- a/htdocs/chess.js +++ /dev/null @@ -1,423 +0,0 @@ -const canvas = document.getElementById("chessboard"); -const ctx = canvas.getContext("2d"); -const size = 60; // Taille des cases -currentPlayer = 0; -// Disposition initiale de l'échiquier -const board = [ - ["T", "C", "F", "Q", "K", "F", "C", "T"], - ["P", "P", "P", "P", "P", "P", "P", "P"], - ["", "", "", "", "", "", "", ""], - ["", "", "", "", "", "", "", ""], - ["", "", "", "", "", "", "", ""], - ["", "", "", "", "", "", "", ""], - ["p", "p", "p", "p", "p", "p", "p", "p"], - ["t", "c", "f", "q", "k", "f", "c", "t"] -]; - -const player = { - host: false, - playedCell: "", - roomId: null, - username: "", - socketId: "", - symbol: "X", - turn: false, - win: false -}; - - - -// Charger les images des pièces - -const pieceImages = { - "p": "pion_blanc.png", - "P": "pion_noir.png", - "t": "tour_blanc.png", - "T": "tour_noir.png", - "c": "cavalier_blanc.png", - "C": "cavalier_noir.png", - "f": "fou_blanc.png", - "F": "fou_noir.png", - "q": "reine_blanc.png", - "Q": "reine_noir.png", - "k": "roi_blanc.png", - "K": "roi_noir.png" -}; - -const pieces_joueurs = {0: ["p","t","c","f","q","k"],1:["P","T","C","F","Q","K"]}; - - - - -// Dessiner l'échiquier avec les couleurs des cases -function drawBoard() { - for (let row = 0; row < 8; row++) { - for (let col = 0; col < 8; col++) { - ctx.fillStyle = (row + col) % 2 === 0 ? "#F5DEB3" : "#8B4513"; - ctx.fillRect(col * size, row * size, size, size); - } - } -} - -// Dessiner les pièces sur l'échiquier -function drawPieces() { - for (let row = 0; row < 8; row++) { - for (let col = 0; col < 8; col++) { - const piece = board[row][col]; - if (piece !== "") { - let img = new Image(); - img.src = "chess_pieces/" + pieceImages[piece]; - img.onload = function () { - ctx.drawImage(img, col * size, row * size, size, size); - }; - img.onerror = function (e) { - console.log("Erreur de chargement de l'image : ", img.src); // En cas d'erreur de chargement - }; - } - } - } -} - -// Fonction principale pour dessiner l'échiquier -function draw() { - drawBoard(); - drawPieces(); -} - -// Appeler la fonction pour afficher l'échiquier -draw(); - - -//Mouvement des pièces sur l'échiquier - -let selectedPiece = null; -let selectedPosition = null; - -canvas.addEventListener("click", function(event) { - const col = Math.floor(event.offsetX / size); - const row = Math.floor(event.offsetY / size); - - if (selectedPiece) { - movePiece(row, col); - } else { - selectPiece(row, col); - } -}); - -function selectPiece(row, col) { - const piece = board[row][col]; - - if (piece !== "" && pieces_joueurs[currentPlayer].includes(piece)) { // Vérifier que ce n'est pas une case vide - selectedPiece = piece; - selectedPosition = { row, col }; - highlightMoves(currentPlayer,piece, row, col); - } -} - -function highlightMoves(player,piece, row, col) { - if (player===0){ - direction=-1; - opponent=1; - } - else{direction=1; - opponent=0; - } - let moves = []; - if (piece.toLowerCase() === "p") { - if(row===6 && player===0){ - if (board[row + 2*direction][col] === "") { - moves.push({row: row + 2*direction,col}); - } - } - if(row===1 && player===1){ - if (board[row +2*direction][col] === "") { - moves.push({row: row + 2*direction,col}); - } - } - if (board[row + direction] && board[row + direction][col] === "") { - moves.push({ row: row + direction, col }); - } - if (pieces_joueurs[opponent].includes(board[row + direction][col + direction])){ - moves.push({row: row +direction, col: col + direction }) - } - if (pieces_joueurs[opponent].includes(board[row + direction][col - direction])){ - moves.push({row: row +direction, col: col - direction }) - } - } - if (piece.toLowerCase() === "t") { - // Déplacements vers le haut - for (let r = row - 1; r >= 0; r--) { - if (board[r][col] === "") { - moves.push({ row: r, col }); - } - else { - if (pieces_joueurs[opponent].includes(board[r][col])) { - moves.push({ row: r, col }); // Capture possible - } - break // Bloqué par une pièce - } - } - - // Déplacements vers le bas - for (let r = row + 1; r < 8; r++) { - if (board[r][col] === "") { - moves.push({ row: r, col }); - } else { - if (pieces_joueurs[opponent].includes(board[r][col])) { - moves.push({ row: r, col }); - } - break; - } - } - - // Déplacements vers la gauche - for (let c = col - 1; c >= 0; c--) { - if (board[row][c] === "") { - moves.push({ row, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[row][c])) { - moves.push({ row, col: c }); - } - break; - } - } - - // Déplacements vers la droite - for (let c = col + 1; c < 8; c++) { - if (board[row][c] === "") { - moves.push({ row, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[row][c])) { - moves.push({ row, col: c }); - } - break; - } - } - } - if (piece.toLowerCase() === "f") { - // Déplacements en haut à gauche - for (let r = row - 1, c = col - 1; r >= 0 && c >= 0; r--, c--) { - if (board[r][c] === "") { - moves.push({ row: r, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[r][c])) { - moves.push({ row: r, col: c }); - } - break; // Bloqué par une pièce - } - } - - // Déplacements en haut à droite - for (let r = row - 1, c = col + 1; r >= 0 && c < 8; r--, c++) { - if (board[r][c] === "") { - moves.push({ row: r, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[r][c])) { - moves.push({ row: r, col: c }); - } - break; - } - } - - // Déplacements en bas à gauche - for (let r = row + 1, c = col - 1; r < 8 && c >= 0; r++, c--) { - if (board[r][c] === "") { - moves.push({ row: r, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[r][c])) { - moves.push({ row: r, col: c }); - } - break; - } - } - - // Déplacements en bas à droite - for (let r = row + 1, c = col + 1; r < 8 && c < 8; r++, c++) { - if (board[r][c] === "") { - moves.push({ row: r, col: c }); - } else { - if (pieces_joueurs[opponent].includes(board[r][c])) { - moves.push({ row: r, col: c }); - } - break; - } - } - } - 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] === "" || pieces_joueurs[opponent].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] === "" || pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].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 (pieces_joueurs[opponent].includes(board[r][c])) { - moves.push({ row: r, col: c }); - } - break; - } - } - } - - return moves; - - } - - - - - -function drawHighlightedMoves(moves) { - ctx.fillStyle = "rgba(0, 255, 0, 0.5)"; // Vert transparent - - for (let move of moves) { - ctx.beginPath(); - ctx.arc(move.col * size + size / 2, move.row * size + size / 2, size / 4, 0, 2 * Math.PI); - ctx.fill(); - } -} -function movePiece(newRow, newCol) { - if (!selectedPiece) return; - - // Vérifier si le mouvement est valide - const validMoves = highlightMoves(currentPlayer,selectedPiece, selectedPosition.row, selectedPosition.col); - - - // Vérifier que validMoves est un tableau et qu'il contient des objets avec { row, col } - const isValidMove = Array.isArray(validMoves) && validMoves.some(m => m.row === newRow && m.col === newCol); - - if (isValidMove) { - // Déplacer la pièce - board[newRow][newCol] = selectedPiece; - board[selectedPosition.row][selectedPosition.col] = ""; - - // Réinitialiser la pièce sélectionnée - selectedPiece = null; - selectedPosition = null; - currentPlayer=(currentPlayer+1)%2; - // Redessiner le plateau - drawBoard(); - drawPieces(); - } else { - // Si le mouvement est invalide, tu pourrais afficher un message - console.log("Mouvement invalide !"); - selectedPiece=null; - } -} \ No newline at end of file diff --git a/htdocs/index.js b/htdocs/index.js index 91667c3df5c60e6907892ff419804c11cfd15d7a..e3a7b8e3a36711f3bed3941e5bf11d3ff4826e87 100644 --- a/htdocs/index.js +++ b/htdocs/index.js @@ -19,7 +19,8 @@ const player = { win: false, isBlackPlayer: true, pieces: ["T", "C", "F", "Q", "K", "P"], - echec: false + echec: false, + score : 0 }; @@ -61,6 +62,7 @@ socket.on('list rooms', (rooms) => { <p class="p-0 m-0 flex-grow-1 fw-bold">Salon de ${room.players[0].username} - ${room.id}</p> <button class="btn btn-sm btn-success join-room" data-room="${room.id}">Rejoindre</button> </li>`; + player.username=room.players[0].username } }); } @@ -907,4 +909,12 @@ socket.on('update-timer', (gameState) => { document.getElementById('player1-time').textContent = gameState.player1.time; document.getElementById('player2-time').textContent = gameState.player2.time; }); +document.getElementById('player1-usurname').textContent = player.username; + +console.log(player.username); + +document.getElementById('player1-usurname').textContent = ennemyUsername; + + + diff --git a/htdocs/page_acceuil.html b/htdocs/page_acceuil.html deleted file mode 100644 index efa186c7c84b483b686287d229afb486fe8870c4..0000000000000000000000000000000000000000 --- a/htdocs/page_acceuil.html +++ /dev/null @@ -1,88 +0,0 @@ -<!DOCTYPE html> -<html lang="fr"> - <head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Page d'Accueil - Jeu d'Échecs</title> - <link href="https://fonts.googleapis.com/css2?family=Libre+Baskerville&display=swap" rel="stylesheet"> <!-- Ajout de la police Libre Baskerville --> - <link rel="stylesheet" href="style_page_acceuil.css"> - </head> - - -<body> - <header> - <h1>Bienvenue sur le Jeu d'Échecs</h1> - </header> - - <section id="menu"> - <h2>Choisissez une option :</h2> - <div class="options-container"> - <div class="option"> - <a href="page_jeu.html">Jouer</a> - </div> - <div class="option"> - <a href="#rankings">Classement</a> - </div> - <div class="option"> - <a href="#contact">Contact</a> - </div> - </div> - </section> - <div class="container mt-5"> - <div class="row"> - <div class="col-sm-12 col-md-6 offset-md-3"> - - <div class="alert d-none" role="alert" id="turn-message"></div> - - <div class="card mb-3" id="user-card"> - <div class="card-body"> - - <form method="POST" id="form"> - <div class="mb-3"> - - <label for="username" class="form-label">Nom d'utilisateur</label> - <input type="text" class="form-control" id="username" minlength="2" maxlength="20" - placeholder="Saisir votre nom d'utilisateur" required> - </div> - <button class="btn btn-primary" id="start" type="submit">Créer un salon privée</button> - </form> - - </div> - </div> - - <div class="card mb-3 d-none" id="rooms-card"> - <div class="card-header">Salons disponibles</div> - <ul class="list-group list-group-flush" id="rooms-list"></ul> - </div> - - <div class="d-none" id="waiting-area"> - <div class="card mb-3"> - <div class="card-header">En attente d'un autre joueur</div> - <div class="card-body mx-auto"> - <div class="spinner-border" role="status"> - <span class="visually-hidden">Loading...</span> - </div> - </div> - </div> - - <div class="card"> - <div class="card-body text-center"> - Partage ce lien pour inviter quelqu'un à jouer avec toi - <span id="link-to-share"></span> - </div> - </div> - </div> - <div class="text-center d-none mt-2" id="restart-area"> - <input class="btn btn-primary" id="restart" type="button" value="Rejouer"> - </div> - </div> - </div> - </div> - - <footer> - <p>© 2025 Jeu d'Échecs | Tous droits réservés.</p> - </footer> - - -</body> -</html> diff --git a/htdocs/page_acceuil2.html b/htdocs/page_acceuil2.html index 3040298f1feebed9878cd0474138cf223813f9de..1516e17aa691a07f8a1fa679586145aca6baa36d 100644 --- a/htdocs/page_acceuil2.html +++ b/htdocs/page_acceuil2.html @@ -95,7 +95,9 @@ </div> <div id="timer" class="text-white"> + <h2><span id="player1-username"></span></h2> <h2>Joueur 1 : <span id="player1-time">300</span> secondes</h2> + <h2><span id="player2-username"></span></h2> <h2>Joueur 2 : <span id="player2-time">300</span> secondes</h2> </div> diff --git a/htdocs/page_jeu.html b/htdocs/page_jeu.html deleted file mode 100644 index 6d0ffe05ca6516d2ba1fb532073e213ffc9e037c..0000000000000000000000000000000000000000 --- a/htdocs/page_jeu.html +++ /dev/null @@ -1,36 +0,0 @@ -<!DOCTYPE html> -<html lang="fr"> -<head> - <meta charset="UTF-8"> - <meta name="viewport" content="width=device-width, initial-scale=1.0"> - <title>Thomas le Braîtte</title> - <link rel="stylesheet" href="style_page_jeu.css"> - <script src="/socket.io/socket.io.js"></script> -</head> -<body> - - - <script> - // Attendre que le DOM soit complètement chargé avant d'exécuter le script - document.addEventListener("DOMContentLoaded", function() { - // Se connecter au serveur WebSocket (en utilisant le même port 8080) - var socket = io.connect('http://localhost:8060'); - - // Quand la connexion est établie, recevoir un message du serveur - socket.on('hello', function(data) { - console.log("Message du serveur : ", data); - }); - - // Envoi d'un message vers le serveur - socket.emit('message', { 'msg': 'Hello, serveur !' }); - socket.on('connect_error', function(err) { - console.error("Erreur de connexion : ", err); - }); - }); - </script> - <canvas id="chessboard" width="480" height="480"></canvas> - - <script src="chess.js"></script> - -</body> -</html> diff --git a/htdocs/style_page_acceuil.css b/htdocs/style_page_acceuil.css deleted file mode 100644 index ce986d89f0f731058043569c2773a60e56a49418..0000000000000000000000000000000000000000 --- a/htdocs/style_page_acceuil.css +++ /dev/null @@ -1,100 +0,0 @@ -/* Général */ -body { - font-family: 'Libre Baskerville', serif; /* Utilisation de la police de style rétro */ - margin: 0; - padding: 0; - background-color: #f4f4f4; - color: #333; -} - -h1, h2 { - color: #444; -} - -/* Header */ -header { - background-color: #333; - color: white; - padding: 20px; - text-align: center; -} - -header h1 { - margin: 0; -} - -/* Menu */ -#menu { - text-align: center; - margin-top: 20px; -} - -#menu h2 { - font-size: 24px; - margin-bottom: 20px; -} - -.options-container { - display: flex; - justify-content: center; - gap: 40px; /* espace entre les cases */ -} - -.option { - width: 250px; - height: 150px; - border: 2px solid #333; - border-radius: 10px; - background-color: #b60c0c; - display: flex; - align-items: center; - justify-content: center; -} - -.option a { - color: white; - font-size: 18px; - text-decoration: none; - font-weight: bold; - text-align: center; -} - -.option a:hover { - text-decoration: underline; - background-color: #F5DEB3; -} - -/* Footer */ -footer { - background-color: #333; - color: white; - text-align: center; - padding: 10px; - position: fixed; - width: 100%; - bottom: 0; -} -.board { - background-color: gainsboro; -} - -.cell { - border: 1px solid; - font-size: 5rem; - height: 150px; - text-align: center; - width: 150px; -} - -.cell:hover { - cursor: pointer; - background-color: rgb(192, 191, 191); -} - -.cell:active { - background-color: rgb(155, 154, 154); -} - -.win-cell { - background-color: greenyellow !important; -} diff --git a/htdocs/style_page_jeu.css b/htdocs/style_page_jeu.css deleted file mode 100644 index 3014b215966b76b13a68910605b039657dbb64e6..0000000000000000000000000000000000000000 --- a/htdocs/style_page_jeu.css +++ /dev/null @@ -1,15 +0,0 @@ -/* Centrer l'échiquier dans la page avec un fond vert clair */ -body { - display: flex; - justify-content: center; - align-items: center; - height: 100vh; /* Prend toute la hauteur de la page */ - background-color: #a8d5ba; /* Fond vert clair */ - margin: 0; -} - -/* Ajouter un contour noir autour du canvas */ -#chessboard { - border: 5px solid black; /* Contour noir autour de l'échiquier */ - box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3); -} diff --git a/server.js b/server.js deleted file mode 100644 index 6e056d32a4afcc28e9059f958f2082c04b350f53..0000000000000000000000000000000000000000 --- a/server.js +++ /dev/null @@ -1,82 +0,0 @@ -var http = require('http'); -var fs = require('fs'); // Pour lire les fichiers -var path = require('path'); // Pour gérer les chemins de fichiers -var socketIo = require('socket.io'); - -// Création du serveur HTTP -var server = http.createServer((req, res) => { - // Si l'URL est la racine ('/'), renvoyer la page_jeu.html depuis le dossier htdocs - if (req.url === '/') { - // Construction du chemin complet du fichier 'page_jeu.html' - var filePath = path.join(__dirname, 'htdocs', 'page_jeu.html'); - - fs.readFile(filePath, 'utf8', (err, data) => { - if (err) { - res.writeHead(500, { "Content-Type": "text/plain" }); - res.end("Erreur du serveur."); - return; - } - res.writeHead(200, { "Content-Type": "text/html" }); - res.end(data); - }); - } else if (req.url.endsWith('.css')) { - // Gérer les fichiers CSS - var filePath = path.join(__dirname, 'htdocs', req.url); - fs.readFile(filePath, (err, data) => { - if (err) { - res.writeHead(404, { "Content-Type": "text/plain" }); - res.end("CSS non trouvé."); - return; - } - res.writeHead(200, { "Content-Type": "text/css" }); - res.end(data); - }); - } else if (req.url.endsWith('.js')) { - // Gérer les fichiers JS - var filePath = path.join(__dirname, 'htdocs', req.url); - fs.readFile(filePath, (err, data) => { - if (err) { - res.writeHead(404, { "Content-Type": "text/plain" }); - res.end("JS non trouvé."); - return; - } - res.writeHead(200, { "Content-Type": "application/javascript" }); - res.end(data); - }); - } else if (req.url.startsWith('/chess_pieces/')) { - // Gérer les fichiers image dans le dossier 'chess_pieces' - var filePath = path.join(__dirname, 'htdocs', req.url); - fs.readFile(filePath, (err, data) => { - if (err) { - res.writeHead(404, { "Content-Type": "text/plain" }); - res.end("Image non trouvée."); - return; - } - res.writeHead(200, { "Content-Type": "image/png" }); - res.end(data); - }); - } else { - // Autres requêtes peuvent être traitées ici - res.writeHead(404, { "Content-Type": "text/plain" }); - res.end("Page non trouvée."); - } -}); - -// Lier Socket.io au serveur HTTP -var io = socketIo(server); - -io.on('connection', function (socket) { - console.log("Un client est connecté : " + socket.id); - - // Envoyer un message "hello" au client - socket.emit('hello', { 'this': 'is my data' }); - // Écouter un message du client (optionnel) - socket.on('message', function (data) { - console.log("Message reçu du client :", data); - socket.on('disconnect', () => { - console.log('Le client est déconnecté'); - }); - }); - -}); -