-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.test.ts
64 lines (54 loc) · 1.94 KB
/
game.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Minesweeper, State } from "./game.ts";
import {
assertEquals,
assertNotEquals,
} from "https://deno.land/std@0.121.0/testing/asserts.ts";
Deno.test("new Minesweeper()", () => {
const game = new Minesweeper(5, 123n);
assertEquals(game.state, State.Playing);
assertEquals(game.flagged, 0);
assertEquals(game.size, 5);
assertEquals(game.user, 123n);
assertEquals(game.flag, false);
const firstRevealed = [...game.map]
.map((e, i) => ({ e, i }))
.filter(({ i }) => game.isRevealed(i))[0].i;
assertEquals(game.isRevealed(firstRevealed), true);
assertNotEquals(game.map[firstRevealed], 9);
});
Deno.test("Minesweeper#flag", () => {
const game = new Minesweeper(5, 123n);
assertEquals(game.flag, false);
game.flag = true;
assertEquals(game.flag, true);
});
Deno.test("Minesweeper#click (non-mine)", () => {
const game = new Minesweeper(5, 123n);
const cell = game.map.findIndex((e, i) => e < 8 && !game.isRevealed(i));
if (cell === undefined) throw new Error("No cell with value 0 found");
game.click(cell);
assertEquals(game.isRevealed(cell), true);
assertEquals(game.state, State.Playing);
});
Deno.test("Minesweeper#click (mine)", () => {
const game = new Minesweeper(5, 123n);
const cell = game.map.findIndex((e, i) => e === 9 && !game.isRevealed(i));
if (cell === undefined) throw new Error("No cell with value 9 found");
game.click(cell);
assertEquals(game.isRevealed(cell), true);
assertEquals(game.state, State.Lose);
});
Deno.test("Minesweeper#click (flag)", () => {
const game = new Minesweeper(5, 123n);
const cell = game.map.findIndex((e, i) => !game.isRevealed(i) && e === 9);
game.flag = true;
game.click(cell);
assertEquals(game.isFlagged(cell), true);
assertEquals(game.state, State.Playing);
game.click(cell);
assertEquals(game.isFlagged(cell), false);
assertEquals(game.state, State.Playing);
game.flag = false;
game.click(cell);
assertEquals(game.state, State.Lose);
});