Skip to content
Snippets Groups Projects
Commit c498ecaa authored by oumaima laklouch's avatar oumaima laklouch
Browse files

Update server.js

parent 022a9a2d
No related branches found
No related tags found
No related merge requests found
...@@ -50,7 +50,6 @@ io.on('connection', (socket) => { ...@@ -50,7 +50,6 @@ io.on('connection', (socket) => {
let stopGame = (room, roomId) => { let stopGame = (room, roomId) => {
console.log("Not enough players in room " + roomId + ", game stops."); console.log("Not enough players in room " + roomId + ", game stops.");
io.to(roomId).emit('stopGame');
}; };
let startGame = (room, roomId) => { let startGame = (room, roomId) => {
...@@ -71,7 +70,6 @@ io.on('connection', (socket) => { ...@@ -71,7 +70,6 @@ io.on('connection', (socket) => {
stopGame(currentRoom, currentRoom.id); stopGame(currentRoom, currentRoom.id);
} }
console.log("Player " + playerName + " leaved room " + currentRoom.id + "."); console.log("Player " + playerName + " leaved room " + currentRoom.id + ".");
socket.leave(currentRoom.id);
currentRoom = null; currentRoom = null;
io.emit('updateRooms', rooms); io.emit('updateRooms', rooms);
} }
...@@ -97,8 +95,6 @@ io.on('connection', (socket) => { ...@@ -97,8 +95,6 @@ io.on('connection', (socket) => {
}); });
io.emit('updateRooms', rooms); io.emit('updateRooms', rooms);
socket.join(roomId);
io.to(roomId).emit('updatePlayers', room.players); io.to(roomId).emit('updatePlayers', room.players);
if (room.players.length === 2) { if (room.players.length === 2) {
startGame(room, roomId); startGame(room, roomId);
...@@ -143,7 +139,6 @@ io.on('connection', (socket) => { ...@@ -143,7 +139,6 @@ io.on('connection', (socket) => {
if (!currentRoom) { if (!currentRoom) {
return; return;
} }
console.log("Message "+message+" in room "+currentRoom.id+" from "+playerName);
io.to(currentRoom.id).emit('message', { io.to(currentRoom.id).emit('message', {
name: playerName, name: playerName,
message message
...@@ -151,7 +146,6 @@ io.on('connection', (socket) => { ...@@ -151,7 +146,6 @@ io.on('connection', (socket) => {
}); });
socket.on('disconnect', () => { socket.on('disconnect', () => {
console.log("Player "+playerName+" disconnected");
leaveRooms(); leaveRooms();
}); });
}); });
...@@ -165,7 +159,64 @@ function findAvailableRow(board, col) { ...@@ -165,7 +159,64 @@ function findAvailableRow(board, col) {
function checkWin(board, row, col) { function checkWin(board, row, col) {
const player = board[row][col]; const player = board[row][col];
// Check horizontal, vertical, and diagonal wins function checkWin(board, row, col) {
// Implementation left for brevity 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; return null;
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment