From 9855d527edf2c2b6b9990c9a85ee1bef62e8acf1 Mon Sep 17 00:00:00 2001
From: Thomas de Brettes <thomas.de-brettes@etu.ec-lyon.fr>
Date: Tue, 1 Apr 2025 20:25:15 +0200
Subject: [PATCH] roque

---
 app.js          | 10 ++---
 htdocs/index.js | 97 ++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 95 insertions(+), 12 deletions(-)

diff --git a/app.js b/app.js
index 38cabdd..f4f42ba 100644
--- a/app.js
+++ b/app.js
@@ -68,16 +68,14 @@ io.on('connection', (socket) => {
     });
     socket.on('roque', (data) => {
         console.log(`[Server] Play reçu : ${JSON.stringify(data)}`);  // Debugging
-
-<<<<<<< Updated upstream
+        socket.broadcast.emit('roque', data);  // On diffuse à tous les clients
+    });
+    
     socket.on('echec', () => {
         socket.broadcast.emit('echec');
     });
-=======
-        socket.broadcast.emit('roque', data);  // On diffuse à tous les clients
-    });
+        
 
->>>>>>> Stashed changes
     socket.on('play again', (roomId) => {
         const room = rooms.find(r => r.id === roomId);
 
diff --git a/htdocs/index.js b/htdocs/index.js
index 732b0a7..c3a939b 100644
--- a/htdocs/index.js
+++ b/htdocs/index.js
@@ -128,14 +128,15 @@ socket.on('play', (moveData) => {
     newRow=moveData.move.to.x;
     newCol=moveData.move.to.y;
     selectedPosition = { row,col };
-
     board[newRow][newCol] = moveData.move.piece;
     board[selectedPosition.row][selectedPosition.col] = "";
 
+
     // Réinitialiser la pièce sélectionnée
 
     selectedPiece = null;
     selectedPosition = null;
+
     draw()
     // Ne remplace PAS l'échiquier complet, mais applique le coup reçu
     //movePiece(moveData.move.piece, newRow, newCol);  // Applique le coup reçu sur l'échiquier local et redessine l'échiquier
@@ -170,6 +171,13 @@ socket.on('play', (moveData) => {
     // }
 });
 
+socket.on('roque', (moveData) =>{
+    board[moveData.move.from.x][moveData.move.from.y]="";
+    board[moveData.move.to.x][moveData.move.to.y]=moveData.move.piece;
+    console.log(board)
+    draw()
+
+})
 socket.on('play again', (players) => {
     restartGame(players);
 })
@@ -480,8 +488,29 @@ function highlightMoves(tour,piece, row, col) {
                 }
             }
         }
-        if (row==7) {
-            if (board[row][col]){}
+        if (row==7 && col == 4) {//position roi blanc d'origine
+            if (board[7][3] === ""  && board[7][2] ==="" && board[7][1] ==="" && board[7][0] ==="t"){//rock à gauche 
+                let newRow = row;
+                let newCol = 2;
+                moves.push({ row: newRow, col: newCol });
+            }
+            if (board[7][5] === ""  && board[7][6] ==="" && board[7][7] ==="t"){//rock à droite
+                let newRow = row;
+                let newCol = 6;
+                moves.push({ row: newRow, col: newCol });
+            }            
+        }
+        if (row==0 && col == 4) {//position roi noir d'origine
+            if (board[0][3] === ""  && board[0][2] ==="" && board[0][1] ==="" && board[0][0] ==="t"){//rock à gauche 
+                let newRow = row;
+                let newCol = 2;
+                moves.push({ row: newRow, col: newCol });
+            }
+            if (board[0][5] === ""  && board[0][6] ==="" && board[0][7] ==="t"){//rock à droite
+                let newRow = row;
+                let newCol = 6;
+                moves.push({ row: newRow, col: newCol });
+            }     
         }
     }
     if (piece.toLowerCase() === "c") {
@@ -669,6 +698,61 @@ function movePiece(piece, newRow, newCol) {
             socket.emit('echec');
         }
 
+        //Verifie si c'était un rock
+        if (piece.toLowerCase() == "k" && Math.abs(newCol-selectedPosition.col)==2){
+            if (newCol-selectedPosition.col==2){//rock à droite
+                if (!player.isBlackPlayer){
+                board[7][5]="t"
+                board[7][7]=""
+                socket.emit('roque', {
+                    move: {
+                        from:{ x:7, y:7
+                        },
+                        to: { x: 7, y: 5 },        // Position d'arrivée
+                        piece: "t"}
+                    })
+                } else {
+                    board[0][5]="T"
+                    board[0][7]=""
+                    socket.emit('roque', {
+                    move: {
+                        from:{ x:0, y:7
+                        },
+                        to: { x: 0, y: 5 },        // Position d'arrivée
+                        piece: "T"           // Nom de la pièce (ex: "pion", "tour", etc.)
+                    }
+                    })
+                }
+            }
+
+            if (newCol-selectedPosition.col == -2){//rock à gauche
+                if (!player.isBlackPlayer){
+                board[7][3]="t"
+                board[7][0]=""
+                socket.emit('roque', {
+                    move: {
+                        from:{ x:7, y:0
+                        },
+                        to: { x: 7, y: 3 },        // Position d'arrivée
+                        piece: "t"}
+                    })
+                } else {
+                    board[0][3]="T"
+                    board[0][0]=""
+                    socket.emit('roque', {
+                    move: {
+                        from:{ x:0, y:0
+                        },
+                        to: { x: 0, y: 3 },        // Position d'arrivée
+                        piece: "T"           // Nom de la pièce (ex: "pion", "tour", etc.)
+                    }
+                    })
+                }
+            }
+            
+
+
+        }
         setTurnMessage('alert-success', 'alert-info', `C'est au tour de <b>${ennemyUsername}</b> de jouer`)
         player.turn = false;
 
@@ -677,10 +761,11 @@ function movePiece(piece, newRow, newCol) {
         [tour.currentPlayer, tour.ennemyPlayer] = [tour.ennemyPlayer, tour.currentPlayer];
 
         // Redessiner le plateau
-        draw();
-    } else {
+        draw()}
+
+    else {
         // Si le mouvement est invalide, tu pourrais afficher un message
         console.log("Mouvement invalide !");
         selectedPiece=null;
     }
-}
+    }
-- 
GitLab