From 4b4473e2b2c4749a5401b33b0812731636ff1e1b Mon Sep 17 00:00:00 2001 From: UnEpicier Date: Sun, 18 Feb 2024 21:37:49 +0100 Subject: [PATCH] :bug: Fix win validation --- src/components/Grid/hooks/useActions.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/components/Grid/hooks/useActions.ts b/src/components/Grid/hooks/useActions.ts index 758c4c2..7255803 100644 --- a/src/components/Grid/hooks/useActions.ts +++ b/src/components/Grid/hooks/useActions.ts @@ -44,8 +44,8 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch ({ - ...prev, + setGameState((prevGameState) => ({ + ...prevGameState, endType: 'loose', })); return revealAllGrid(grid, true); @@ -57,12 +57,12 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch ({ - ...prev, + if (hasWin(prev, bombsCount || gameState.bombs, gameState.placedFlags)) { + setGameState((prevGameState) => ({ + ...prevGameState, endType: 'win', })); - return revealAllGrid(grid, true); + return revealAllGrid(prev, true); } return prev; @@ -115,7 +115,7 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch ({ ...prev, endType: 'win', @@ -134,7 +134,7 @@ export const useActions = (grid: GridType, setGrid: React.Dispatch { +const hasWin = (grid: GridType, bombs: number, placedFlags: number) => { let bombsFlagged = 0; grid.map((row) => { @@ -145,7 +145,7 @@ const hasWin = (grid: GridType, bombs: number) => { }); }); - if (bombsFlagged === bombs) { + if (bombsFlagged === bombs && placedFlags === bombs) { return true; }