🐛 Fix win validation

This commit is contained in:
2024-02-18 21:37:49 +01:00
parent c9123a2da1
commit 4b4473e2b2
+9 -9
View File
@@ -44,8 +44,8 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStat
// If the clicked cell hide a bomb, loose immediatly // If the clicked cell hide a bomb, loose immediatly
if (prev[rowIndex][colIndex].value === 'bomb') { if (prev[rowIndex][colIndex].value === 'bomb') {
setGameState((prev) => ({ setGameState((prevGameState) => ({
...prev, ...prevGameState,
endType: 'loose', endType: 'loose',
})); }));
return revealAllGrid(grid, true); return revealAllGrid(grid, true);
@@ -57,12 +57,12 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStat
prev[rowIndex][colIndex].hidden = false; prev[rowIndex][colIndex].hidden = false;
} }
if (hasWin(prev, bombsCount || gameState.bombs)) { if (hasWin(prev, bombsCount || gameState.bombs, gameState.placedFlags)) {
setGameState((prev) => ({ setGameState((prevGameState) => ({
...prev, ...prevGameState,
endType: 'win', endType: 'win',
})); }));
return revealAllGrid(grid, true); return revealAllGrid(prev, true);
} }
return prev; return prev;
@@ -115,7 +115,7 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStat
return row; return row;
}); });
if (hasWin(prev, bombsCount || gameState.bombs)) { if (hasWin(prev, bombsCount || gameState.bombs, gameState.placedFlags)) {
setGameState((prev) => ({ setGameState((prev) => ({
...prev, ...prev,
endType: 'win', endType: 'win',
@@ -134,7 +134,7 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch<React.SetStat
}; };
}; };
const hasWin = (grid: GridType, bombs: number) => { const hasWin = (grid: GridType, bombs: number, placedFlags: number) => {
let bombsFlagged = 0; let bombsFlagged = 0;
grid.map((row) => { grid.map((row) => {
@@ -145,7 +145,7 @@ const hasWin = (grid: GridType, bombs: number) => {
}); });
}); });
if (bombsFlagged === bombs) { if (bombsFlagged === bombs && placedFlags === bombs) {
return true; return true;
} }