2022-04-30 07:27:49 +02:00
|
|
|
import { Close, Flag, Report } from "@mui/icons-material";
|
|
|
|
import { Box, Paper, Typography } from "@mui/material";
|
|
|
|
import { uniqueId } from "lodash";
|
2022-04-25 01:51:30 +02:00
|
|
|
import React, { useEffect, useState } from "react";
|
2023-06-12 06:34:20 +02:00
|
|
|
import { AugmentationName } from "@enums";
|
2022-10-10 00:42:14 +02:00
|
|
|
import { Player } from "@player";
|
2022-04-30 07:27:49 +02:00
|
|
|
import { Settings } from "../../Settings/Settings";
|
2022-04-25 01:51:30 +02:00
|
|
|
import { KEY } from "../../utils/helpers/keyCodes";
|
|
|
|
import { downArrowSymbol, getArrow, leftArrowSymbol, rightArrowSymbol, upArrowSymbol } from "../utils";
|
|
|
|
import { interpolate } from "./Difficulty";
|
|
|
|
import { GameTimer } from "./GameTimer";
|
|
|
|
import { IMinigameProps } from "./IMinigameProps";
|
|
|
|
import { KeyHandler } from "./KeyHandler";
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
interface Difficulty {
|
2021-09-05 01:09:30 +02:00
|
|
|
[key: string]: number;
|
|
|
|
timer: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
mines: number;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const difficulties: {
|
2021-09-05 01:09:30 +02:00
|
|
|
Trivial: Difficulty;
|
|
|
|
Normal: Difficulty;
|
|
|
|
Hard: Difficulty;
|
|
|
|
Impossible: Difficulty;
|
2021-06-13 17:05:40 +02:00
|
|
|
} = {
|
2021-09-05 01:09:30 +02:00
|
|
|
Trivial: { timer: 15000, width: 3, height: 3, mines: 4 },
|
|
|
|
Normal: { timer: 15000, width: 4, height: 4, mines: 7 },
|
|
|
|
Hard: { timer: 15000, width: 5, height: 5, mines: 11 },
|
|
|
|
Impossible: { timer: 15000, width: 6, height: 6, mines: 15 },
|
|
|
|
};
|
2021-06-13 17:05:40 +02:00
|
|
|
|
|
|
|
export function MinesweeperGame(props: IMinigameProps): React.ReactElement {
|
2021-09-05 01:09:30 +02:00
|
|
|
const difficulty: Difficulty = { timer: 0, width: 0, height: 0, mines: 0 };
|
|
|
|
interpolate(difficulties, props.difficulty, difficulty);
|
|
|
|
const timer = difficulty.timer;
|
|
|
|
const [minefield] = useState(generateMinefield(difficulty));
|
|
|
|
const [answer, setAnswer] = useState(generateEmptyField(difficulty));
|
|
|
|
const [pos, setPos] = useState([0, 0]);
|
|
|
|
const [memoryPhase, setMemoryPhase] = useState(true);
|
2023-06-12 06:34:20 +02:00
|
|
|
const hasAugment = Player.hasAugmentation(AugmentationName.HuntOfArtemis, true);
|
2021-09-25 04:15:19 +02:00
|
|
|
function press(this: Document, event: KeyboardEvent): void {
|
2021-09-05 01:09:30 +02:00
|
|
|
event.preventDefault();
|
|
|
|
if (memoryPhase) return;
|
|
|
|
const move = [0, 0];
|
|
|
|
const arrow = getArrow(event);
|
|
|
|
switch (arrow) {
|
2022-03-23 15:31:56 +01:00
|
|
|
case upArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[1]--;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case leftArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[0]--;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case downArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[1]++;
|
|
|
|
break;
|
2022-03-23 15:31:56 +01:00
|
|
|
case rightArrowSymbol:
|
2021-09-05 01:09:30 +02:00
|
|
|
move[0]++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
const next = [pos[0] + move[0], pos[1] + move[1]];
|
|
|
|
next[0] = (next[0] + minefield[0].length) % minefield[0].length;
|
|
|
|
next[1] = (next[1] + minefield.length) % minefield.length;
|
|
|
|
setPos(next);
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2022-03-23 15:31:56 +01:00
|
|
|
if (event.key == KEY.SPACE) {
|
2021-09-05 01:09:30 +02:00
|
|
|
if (!minefield[pos[1]][pos[0]]) {
|
|
|
|
props.onFailure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setAnswer((old) => {
|
|
|
|
old[pos[1]][pos[0]] = true;
|
|
|
|
if (fieldEquals(minefield, old)) props.onSuccess();
|
|
|
|
return old;
|
|
|
|
});
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
}
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
useEffect(() => {
|
|
|
|
const id = setTimeout(() => setMemoryPhase(false), 2000);
|
|
|
|
return () => clearInterval(id);
|
|
|
|
}, []);
|
2021-06-13 17:05:40 +02:00
|
|
|
|
2022-04-30 07:27:49 +02:00
|
|
|
const flatGrid: { flagged?: boolean; current?: boolean; marked?: boolean }[] = [];
|
|
|
|
|
|
|
|
minefield.map((line, y) =>
|
|
|
|
line.map((cell, x) => {
|
|
|
|
if (memoryPhase) {
|
|
|
|
flatGrid.push({ flagged: Boolean(minefield[y][x]) });
|
|
|
|
return;
|
|
|
|
} else if (x === pos[0] && y === pos[1]) {
|
|
|
|
flatGrid.push({ current: true });
|
|
|
|
} else if (answer[y][x]) {
|
|
|
|
flatGrid.push({ marked: true });
|
|
|
|
} else if (hasAugment && minefield[y][x]) {
|
|
|
|
flatGrid.push({ flagged: true });
|
|
|
|
} else {
|
|
|
|
flatGrid.push({});
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
2022-04-24 21:58:59 +02:00
|
|
|
<>
|
2021-09-05 01:09:30 +02:00
|
|
|
<GameTimer millis={timer} onExpire={props.onFailure} />
|
2022-04-30 07:27:49 +02:00
|
|
|
<Paper sx={{ display: "grid", justifyItems: "center", pb: 1 }}>
|
2021-10-01 19:08:37 +02:00
|
|
|
<Typography variant="h4">{memoryPhase ? "Remember all the mines!" : "Mark all the mines!"}</Typography>
|
2022-04-30 07:27:49 +02:00
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
display: "grid",
|
|
|
|
gridTemplateColumns: `repeat(${Math.round(difficulty.width)}, 1fr)`,
|
|
|
|
gridTemplateRows: `repeat(${Math.round(difficulty.height)}, 1fr)`,
|
|
|
|
gap: 1,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{flatGrid.map((item) => {
|
2022-05-02 18:38:20 +02:00
|
|
|
let color: string;
|
|
|
|
let icon: React.ReactElement;
|
|
|
|
|
|
|
|
if (item.marked) {
|
|
|
|
color = Settings.theme.warning;
|
|
|
|
icon = <Flag />;
|
|
|
|
} else if (item.current) {
|
|
|
|
color = Settings.theme.infolight;
|
|
|
|
icon = <Close />;
|
|
|
|
} else if (item.flagged) {
|
|
|
|
color = Settings.theme.error;
|
|
|
|
icon = <Report />;
|
|
|
|
} else {
|
|
|
|
color = Settings.theme.primary;
|
|
|
|
icon = <></>;
|
|
|
|
}
|
|
|
|
|
2022-04-30 07:27:49 +02:00
|
|
|
return (
|
|
|
|
<Typography
|
|
|
|
key={`${item}${uniqueId()}`}
|
|
|
|
sx={{
|
|
|
|
color: color,
|
|
|
|
border: `2px solid ${item.current ? Settings.theme.infolight : Settings.theme.primary}`,
|
|
|
|
height: "32px",
|
|
|
|
width: "32px",
|
|
|
|
display: "flex",
|
|
|
|
alignItems: "center",
|
|
|
|
justifyContent: "center",
|
|
|
|
}}
|
|
|
|
>
|
2022-05-02 18:38:20 +02:00
|
|
|
{icon}
|
2022-04-30 07:27:49 +02:00
|
|
|
</Typography>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</Box>
|
2021-09-05 01:09:30 +02:00
|
|
|
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
|
2022-04-24 21:58:59 +02:00
|
|
|
</Paper>
|
|
|
|
</>
|
2021-09-05 01:09:30 +02:00
|
|
|
);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function fieldEquals(a: boolean[][], b: boolean[][]): boolean {
|
2021-09-05 01:09:30 +02:00
|
|
|
function count(field: boolean[][]): number {
|
|
|
|
return field.flat().reduce((a, b) => a + (b ? 1 : 0), 0);
|
|
|
|
}
|
|
|
|
return count(a) === count(b);
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateEmptyField(difficulty: Difficulty): boolean[][] {
|
2021-09-05 01:09:30 +02:00
|
|
|
const field = [];
|
|
|
|
for (let i = 0; i < difficulty.height; i++) {
|
|
|
|
field.push(new Array(Math.round(difficulty.width)).fill(false));
|
|
|
|
}
|
|
|
|
return field;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function generateMinefield(difficulty: Difficulty): boolean[][] {
|
2021-09-05 01:09:30 +02:00
|
|
|
const field = generateEmptyField(difficulty);
|
|
|
|
for (let i = 0; i < difficulty.mines; i++) {
|
|
|
|
const x = Math.floor(Math.random() * field.length);
|
|
|
|
const y = Math.floor(Math.random() * field[0].length);
|
|
|
|
if (field[x][y]) {
|
|
|
|
i--;
|
|
|
|
continue;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
field[x][y] = true;
|
|
|
|
}
|
|
|
|
return field;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|