2021-09-05 01:09:30 +02:00
|
|
|
import React, { useState, useEffect } from "react";
|
2021-09-17 01:23:03 +02:00
|
|
|
import Grid from "@mui/material/Grid";
|
2021-06-13 17:05:40 +02:00
|
|
|
import { IMinigameProps } from "./IMinigameProps";
|
|
|
|
import { KeyHandler } from "./KeyHandler";
|
|
|
|
import { GameTimer } from "./GameTimer";
|
|
|
|
import { interpolate } from "./Difficulty";
|
|
|
|
import { getArrow } from "../utils";
|
2021-10-01 19:08:37 +02:00
|
|
|
import Typography from "@mui/material/Typography";
|
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);
|
2021-06-13 17:05:40 +02:00
|
|
|
|
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) {
|
|
|
|
case "↑":
|
|
|
|
move[1]--;
|
|
|
|
break;
|
|
|
|
case "←":
|
|
|
|
move[0]--;
|
|
|
|
break;
|
|
|
|
case "↓":
|
|
|
|
move[1]++;
|
|
|
|
break;
|
|
|
|
case "→":
|
|
|
|
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
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
if (event.keyCode == 32) {
|
|
|
|
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
|
|
|
|
2021-09-05 01:09:30 +02:00
|
|
|
return (
|
|
|
|
<Grid container spacing={3}>
|
|
|
|
<GameTimer millis={timer} onExpire={props.onFailure} />
|
|
|
|
<Grid item xs={12}>
|
2021-10-01 19:08:37 +02:00
|
|
|
<Typography variant="h4">{memoryPhase ? "Remember all the mines!" : "Mark all the mines!"}</Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
{minefield.map((line, y) => (
|
|
|
|
<div key={y}>
|
2021-10-02 04:27:32 +02:00
|
|
|
<Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
{line.map((cell, x) => {
|
|
|
|
if (memoryPhase) {
|
|
|
|
if (minefield[y][x]) return <span key={x}>[?] </span>;
|
|
|
|
return <span key={x}>[ ] </span>;
|
2021-06-13 17:05:40 +02:00
|
|
|
} else {
|
2021-09-09 05:47:34 +02:00
|
|
|
if (x == pos[0] && y == pos[1]) return <span key={x}>[X] </span>;
|
2021-09-05 01:09:30 +02:00
|
|
|
if (answer[y][x]) return <span key={x}>[.] </span>;
|
|
|
|
return <span key={x}>[ ] </span>;
|
2021-06-13 17:05:40 +02:00
|
|
|
}
|
2021-09-05 01:09:30 +02:00
|
|
|
})}
|
2021-10-02 04:27:32 +02:00
|
|
|
</Typography>
|
2021-09-05 01:09:30 +02:00
|
|
|
<br />
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
<KeyHandler onKeyDown={press} onFailure={props.onFailure} />
|
|
|
|
</Grid>
|
|
|
|
</Grid>
|
|
|
|
);
|
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
|
|
|
}
|