Skip to content

Commit

Permalink
Finish building the game-with-hooks module
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Aug 9, 2022
1 parent 9520fdf commit 6bc6e6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/helpers/cells/cells-manipulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/modules/GameWithHooks/GameWithHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,38 @@ 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<LevelNames>("beginner");
const [size, bombs] = GameSettings[level];

const [playerField, setPlayerField] = useState<Field>(generateFieldWithDefaultState(size, CellState.hidden));

const gameField = useMemo(() => fieldGenerator(size, bombs / (size * size)), [size, bombs]);
const [gameField, setGameField] = useState<Field>(fieldGenerator(size, bombs / (size * size)));

const onClickHandler = (coords: Coordinates) => {
const newPlayerField = openCell(coords, playerField, gameField);

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<HTMLSelectElement>) => {
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 (
<WrapperContainer>
<Top feature="Flag" firstAction="right click">
Expand All @@ -40,7 +48,7 @@ export const GameWithHooks: FunctionComponent = () => {
time="000"
mines="000"
levels={GameLevels as unknown as string[]}
onReset={() => null}
onReset={onResetHandler}
onChangeLevel={onChangeLevelHandler}
/>
<GameOver onClick={() => null} isWin={true} />
Expand Down

0 comments on commit 6bc6e6f

Please sign in to comment.