diff --git a/server.js b/server.js index a307c3f75874d5936d45673a0c95e102073ed3eb..a7eeec61e284aab1ff6422e8f23aa9e90e24b83b 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