Skip to content

Commit

Permalink
Add game-over logic situation
Browse files Browse the repository at this point in the history
  • Loading branch information
zourdyzou committed Aug 9, 2022
1 parent 6bc6e6f commit 5e3df07
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/components/game-area/game-over.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FunctionComponent, PropsWithChildren } from "react";
import React, { FunctionComponent } from "react";

import styles from "./game-section.module.scss";

Expand All @@ -8,5 +8,9 @@ interface GameOverProps {
}

export const GameOver: FunctionComponent<GameOverProps> = ({ onClick, isWin }) => {
return <div className={styles.gameOverComponent}>{isWin ? "😎" : "🙁"}</div>;
return (
<div onClick={onClick} className={styles.gameOverComponent}>
{isWin ? "😎" : "🙁"}
</div>
);
};
19 changes: 14 additions & 5 deletions src/modules/GameWithHooks/GameWithHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ export const GameWithHooks: FunctionComponent = () => {
const [level, setLevel] = useState<LevelNames>("beginner");
const [size, bombs] = GameSettings[level];

const [isGameOver, setIsGameOver] = useState(false);
const [isWin, setIsWin] = useState(false);

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

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

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

setPlayerField([...newPlayerField]);
try {
const newPlayerField = openCell(coords, playerField, gameField);
setPlayerField([...newPlayerField]);
} catch (e) {
setPlayerField([...gameField]);
setIsGameOver(true);
}
};

const resetHandler = ([size, bombs]: [number, number]) => {
Expand All @@ -28,6 +35,8 @@ export const GameWithHooks: FunctionComponent = () => {

setGameField([...newGameField]);
setPlayerField([...newPlayerField]);
setIsGameOver(false);
setIsWin(false);
};

const onChangeLevelHandler = ({ target: { value: level } }: React.ChangeEvent<HTMLSelectElement>) => {
Expand All @@ -46,12 +55,12 @@ export const GameWithHooks: FunctionComponent = () => {
<GameArea>
<ScoreBoard
time="000"
mines="000"
mines={String(bombs)}
levels={GameLevels as unknown as string[]}
onReset={onResetHandler}
onChangeLevel={onChangeLevelHandler}
/>
<GameOver onClick={() => null} isWin={true} />
{isGameOver && <GameOver onClick={onResetHandler} isWin={isWin} />}
<GridComponent children={playerField} onClick={onClickHandler} onContextMenu={() => null} />
</GameArea>
</WrapperContainer>
Expand Down

0 comments on commit 5e3df07

Please sign in to comment.