From c498ecaae01740abda90c9c9344f1fa862257c40 Mon Sep 17 00:00:00 2001
From: oumaima laklouch <oumaima0.7laklouch@gmail.com>
Date: Wed, 19 Mar 2025 12:03:45 +0100
Subject: [PATCH] Update server.js

---
 server.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 59 insertions(+), 8 deletions(-)

diff --git a/server.js b/server.js
index a307c3f..a7eeec6 100644
--- a/server.js
+++ b/server.js
@@ -50,7 +50,6 @@ io.on('connection', (socket) => {
 
     let stopGame = (room, roomId) => {
         console.log("Not enough players in room " + roomId + ", game stops.");
-        io.to(roomId).emit('stopGame');
     };
 
     let startGame = (room, roomId) => {
@@ -71,7 +70,6 @@ io.on('connection', (socket) => {
                 stopGame(currentRoom, currentRoom.id);
             }
             console.log("Player " + playerName + " leaved room " + currentRoom.id + ".");
-            socket.leave(currentRoom.id);
             currentRoom = null;
             io.emit('updateRooms', rooms);
         }
@@ -97,8 +95,6 @@ io.on('connection', (socket) => {
         });
         io.emit('updateRooms', rooms);
 
-        socket.join(roomId);
-
         io.to(roomId).emit('updatePlayers', room.players);
         if (room.players.length === 2) {
             startGame(room, roomId);
@@ -143,7 +139,6 @@ io.on('connection', (socket) => {
         if (!currentRoom) {
             return;
         }
-        console.log("Message "+message+" in room "+currentRoom.id+" from "+playerName);
         io.to(currentRoom.id).emit('message', {
             name: playerName,
             message
@@ -151,7 +146,6 @@ io.on('connection', (socket) => {
     });
 
     socket.on('disconnect', () => {
-        console.log("Player "+playerName+" disconnected");
         leaveRooms();
     });
 });
@@ -165,7 +159,64 @@ function findAvailableRow(board, col) {
 
 function checkWin(board, row, col) {
     const player = board[row][col];
-    // Check horizontal, vertical, and diagonal wins
-    // Implementation left for brevity
+    function checkWin(board, row, col) {
+        const player = board[row][col];
+        if (player === null) return null;
+    
+        
+        const ROWS = board.length;
+        const COLS = board[0].length;
+        const WINNING_LENGTH = 4;
+    
+        
+        const directions = [
+            [0, 1],   
+            [1, 0],   
+            [1, 1],   
+            [1, -1]   
+        ];
+    
+        for (const [dx, dy] of directions) {
+            let count = 1;
+    
+            
+            for (let step = 1; step < WINNING_LENGTH; step++) {
+                const newRow = row + step * dx;
+                const newCol = col + step * dy;
+                if (
+                    newRow >= 0 && newRow < ROWS &&
+                    newCol >= 0 && newCol < COLS &&
+                    board[newRow][newCol] === player
+                ) {
+                    count++;
+                } else {
+                    break;
+                }
+            }
+    
+            
+            for (let step = 1; step < WINNING_LENGTH; step++) {
+                const newRow = row - step * dx;
+                const newCol = col - step * dy;
+                if (
+                    newRow >= 0 && newRow < ROWS &&
+                    newCol >= 0 && newCol < COLS &&
+                    board[newRow][newCol] === player
+                ) {
+                    count++;
+                } else {
+                    break;
+                }
+            }
+    
+        
+            if (count >= WINNING_LENGTH) {
+                return player;
+            }
+        }
+    
+        return null;
+    }
+    
     return null;
 }
\ No newline at end of file
-- 
GitLab