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

Update server.js

parent 9a2734b4
Branches
No related tags found
No related merge requests found
...@@ -121,65 +121,25 @@ io.on('connection', (socket) => { ...@@ -121,65 +121,25 @@ io.on('connection', (socket) => {
col col
}); });
const winner = checkWin(currentRoom.gameState, row, col);
if (winner !== null) {
hallOfFame[playerName] = (hallOfFame[playerName] || 0) + 1;
fs.writeFileSync('halloffame.json', JSON.stringify(hallOfFame));
}
currentRoom.currentPlayer = 1 - currentRoom.currentPlayer;
io.to(currentRoom.id).emit('updateGame', {
board: currentRoom.gameState,
currentPlayer: currentRoom.currentPlayer,
winner
});
});
socket.on('chatMessage', (message) => {
if (!currentRoom) {
return;
}
io.to(currentRoom.id).emit('message', {
name: playerName,
message
});
});
socket.on('disconnect', () => {
leaveRooms();
});
});
function findAvailableRow(board, col) {
for (let row = 5; row >= 0; row--) {
if (!board[row][col]) return row;
}
return -1;
}
function checkWin(board, row, col) {
const player = board[row][col];
function checkWin(board, row, col) { function checkWin(board, row, col) {
const player = board[row][col]; const player = board[row][col];
if (player === null) return null; if (player === null) return null;
const ROWS = board.length; const ROWS = board.length;
const COLS = board[0].length; const COLS = board[0].length;
const WINNING_LENGTH = 4; const WINNING_LENGTH = 4;
const directions = [ const directions = [
[0, 1], [0, 1], // horizontal
[1, 0], [1, 0], // vertical
[1, 1], [1, 1], // diagonale descendante
[1, -1] [1, -1] // diagonale montante
]; ];
for (const [dx, dy] of directions) { for (const [dx, dy] of directions) {
let count = 1; let count = 1;
// Dans une direction
for (let step = 1; step < WINNING_LENGTH; step++) { for (let step = 1; step < WINNING_LENGTH; step++) {
const newRow = row + step * dx; const newRow = row + step * dx;
const newCol = col + step * dy; const newCol = col + step * dy;
...@@ -194,7 +154,7 @@ function checkWin(board, row, col) { ...@@ -194,7 +154,7 @@ function checkWin(board, row, col) {
} }
} }
// Dans l’autre direction
for (let step = 1; step < WINNING_LENGTH; step++) { for (let step = 1; step < WINNING_LENGTH; step++) {
const newRow = row - step * dx; const newRow = row - step * dx;
const newCol = col - step * dy; const newCol = col - step * dy;
...@@ -209,14 +169,10 @@ function checkWin(board, row, col) { ...@@ -209,14 +169,10 @@ function checkWin(board, row, col) {
} }
} }
if (count >= WINNING_LENGTH) { if (count >= WINNING_LENGTH) {
return player; 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