diff --git a/server.js b/server.js index a7eeec61e284aab1ff6422e8f23aa9e90e24b83b..91ad4e1828f8d7edb3893a414ffe5460f34d1aba 100644 --- a/server.js +++ b/server.js @@ -121,102 +121,58 @@ io.on('connection', (socket) => { 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) { - const player = board[row][col]; - if (player === null) return null; - + const player = board[row][col]; + if (player === null) return null; - const ROWS = board.length; - const COLS = board[0].length; - const WINNING_LENGTH = 4; - + 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; + const directions = [ + [0, 1], // horizontal + [1, 0], // vertical + [1, 1], // diagonale descendante + [1, -1] // diagonale montante + ]; + + for (const [dx, dy] of directions) { + let count = 1; + + // Dans une direction + 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; + + // Dans l’autre direction + 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; - } + if (count >= WINNING_LENGTH) { + return player; + } + return null; } \ No newline at end of file