diff --git a/src/helpers/cells/cells-manipulator.ts b/src/helpers/cells/cells-manipulator.ts index f4e5e85f..c6d046cd 100644 --- a/src/helpers/cells/cells-manipulator.ts +++ b/src/helpers/cells/cells-manipulator.ts @@ -67,13 +67,9 @@ export const openCell = (coords: Coordinates, playerField: Field, gameField: Fie const scopedGameCell = gameField[y][x]; const scopedPlayerCell = playerField[y][x]; - if (scopedGameCell === empty && scopedPlayerCell === hidden) { + if (scopedPlayerCell === hidden && scopedGameCell !== bomb) { playerField = openCell([y, x], playerField, gameField); } - - if (scopedGameCell < bomb) { - playerField[y][x] = scopedGameCell; - } } } } diff --git a/src/modules/GameWithHooks/GameWithHooks.tsx b/src/modules/GameWithHooks/GameWithHooks.tsx index 6f37404c..42ae900c 100644 --- a/src/modules/GameWithHooks/GameWithHooks.tsx +++ b/src/modules/GameWithHooks/GameWithHooks.tsx @@ -6,7 +6,7 @@ import { Top } from "@components/top-section"; import { openCell } from "@helpers/cells/cells-manipulator"; import { CellState, Coordinates, Field, fieldGenerator, generateFieldWithDefaultState } from "@helpers/field"; import { GameLevels, GameSettings, LevelNames } from "@modules/GameSettings"; -import React, { FunctionComponent, useMemo, useState } from "react"; +import React, { FunctionComponent, useState } from "react"; export const GameWithHooks: FunctionComponent = () => { const [level, setLevel] = useState("beginner"); @@ -14,7 +14,7 @@ export const GameWithHooks: FunctionComponent = () => { const [playerField, setPlayerField] = useState(generateFieldWithDefaultState(size, CellState.hidden)); - const gameField = useMemo(() => fieldGenerator(size, bombs / (size * size)), [size, bombs]); + const [gameField, setGameField] = useState(fieldGenerator(size, bombs / (size * size))); const onClickHandler = (coords: Coordinates) => { const newPlayerField = openCell(coords, playerField, gameField); @@ -22,14 +22,22 @@ export const GameWithHooks: FunctionComponent = () => { setPlayerField([...newPlayerField]); }; + const resetHandler = ([size, bombs]: [number, number]) => { + const newGameField = fieldGenerator(size, bombs / (size * size)); + const newPlayerField = generateFieldWithDefaultState(size, CellState.hidden); + + setGameField([...newGameField]); + setPlayerField([...newPlayerField]); + }; + const onChangeLevelHandler = ({ target: { value: level } }: React.ChangeEvent) => { setLevel(level as LevelNames); - const [newSize] = GameSettings[level as LevelNames]; - - const resetNewPlayerField = generateFieldWithDefaultState(newSize, CellState.hidden); - setPlayerField([...resetNewPlayerField]); + const newSettings = GameSettings[level as LevelNames]; + resetHandler(newSettings); }; + const onResetHandler = () => resetHandler([size, bombs]); + return ( @@ -40,7 +48,7 @@ export const GameWithHooks: FunctionComponent = () => { time="000" mines="000" levels={GameLevels as unknown as string[]} - onReset={() => null} + onReset={onResetHandler} onChangeLevel={onChangeLevelHandler} /> null} isWin={true} />