bitburner-src/src/Infiltration/ui/MinesweeperGame.tsx

140 lines
4.5 KiB
TypeScript
Raw Normal View History

2022-04-25 01:51:30 +02:00
import { Paper, Typography } from "@mui/material";
import React, { useEffect, useState } from "react";
import { AugmentationNames } from "../../Augmentation/data/AugmentationNames";
import { Player } from "../../Player";
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";
interface Difficulty {
2021-09-05 01:09:30 +02:00
[key: string]: number;
timer: number;
width: number;
height: number;
mines: number;
}
const difficulties: {
2021-09-05 01:09:30 +02:00
Trivial: Difficulty;
Normal: Difficulty;
Hard: Difficulty;
Impossible: Difficulty;
} = {
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 },
};
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);
2022-04-22 21:30:49 +02:00
const hasAugment = Player.hasAugmentation(AugmentationNames.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) {
case upArrowSymbol:
2021-09-05 01:09:30 +02:00
move[1]--;
break;
case leftArrowSymbol:
2021-09-05 01:09:30 +02:00
move[0]--;
break;
case downArrowSymbol:
2021-09-05 01:09:30 +02:00
move[1]++;
break;
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);
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-09-05 01:09:30 +02:00
}
2021-09-05 01:09:30 +02:00
useEffect(() => {
const id = setTimeout(() => setMemoryPhase(false), 2000);
return () => clearInterval(id);
}, []);
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-24 21:58:59 +02:00
<Paper sx={{ display: "grid", justifyItems: "center" }}>
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}>[?]&nbsp;</span>;
return <span key={x}>[&nbsp;]&nbsp;</span>;
} else {
2021-09-09 05:47:34 +02:00
if (x == pos[0] && y == pos[1]) return <span key={x}>[X]&nbsp;</span>;
2021-09-05 01:09:30 +02:00
if (answer[y][x]) return <span key={x}>[.]&nbsp;</span>;
if (hasAugment && minefield[y][x]) return <span key={x}>[?]&nbsp;</span>;
2021-09-05 01:09:30 +02:00
return <span key={x}>[&nbsp;]&nbsp;</span>;
}
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} />
2022-04-24 21:58:59 +02:00
</Paper>
</>
2021-09-05 01:09:30 +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);
}
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;
}
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-09-05 01:09:30 +02:00
field[x][y] = true;
}
return field;
}