diff --git a/server.js b/server.js index 20ded88c77df735eb1085b3d3ebfd961d8abd388..8fab326462928f7b1acc0dc4d1c1023f70d2e35c 100644 --- a/server.js +++ b/server.js @@ -170,53 +170,49 @@ function checkWin(board, row, col) { const WINNING_LENGTH = 4; const directions = [ - [0, 1], // horizontal - [1, 0], // vertical - [1, 1], // diagonale descendante - [1, -1], // diagonale montante + [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; + 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; + } } - } - // 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; + // Direction opposée + 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; + if (count >= WINNING_LENGTH) { + return player; + } } return null; - } -} \ No newline at end of file +}