-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
156 lines (134 loc) · 3.59 KB
/
logic.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
export const TILE_STATUSES = {
HIDDEN: "hidden",
MINE: "mine",
NUMBER: "number",
MARKED: "marked",
};
export function createBoard(boardSize) {
const board = [];
for (let x = 0; x < boardSize; x++) {
const row = [];
for (let y = 0; y < boardSize; y++) {
const element = document.createElement("div");
element.dataset.status = TILE_STATUSES.HIDDEN;
const tile = {
element,
x,
y,
get status() {
return this.element.dataset.status;
},
set status(value) {
this.element.dataset.status = value;
},
};
row.push(tile);
}
board.push(row);
}
return board;
}
export function populateBoard(board, boardSize, numberOfMines, firstRevealed) {
do {
const minePositions = getMinePositions(boardSize, numberOfMines);
board.forEach((row) => {
row.forEach((tile) => {
tile.mine = minePositions.some((mine) => positionMatch(tile, mine));
});
});
} while (nearbyTiles(board, firstRevealed).filter((t) => t.mine) != 0);
}
export function markTile(tile) {
if (
tile.status !== TILE_STATUSES.HIDDEN &&
tile.status !== TILE_STATUSES.MARKED
) {
return;
}
if (tile.status === TILE_STATUSES.MARKED) {
tile.status = TILE_STATUSES.HIDDEN;
} else {
tile.status = TILE_STATUSES.MARKED;
}
}
export function revealTile(board, tile) {
if (tile.status !== TILE_STATUSES.HIDDEN) {
return;
}
if (tile.mine) {
tile.status = TILE_STATUSES.MINE;
return;
}
tile.status = TILE_STATUSES.NUMBER;
const adjacentTiles = nearbyTiles(board, tile);
const mines = adjacentTiles.filter((t) => t.mine);
if (mines.length === 0) {
adjacentTiles.forEach(revealTile.bind(null, board));
} else {
tile.element.textContent = mines.length;
tile.element.style.color = `var(--v${mines.length})`;
}
}
export function checkWin(board) {
return board.every((row) => {
return row.every((tile) => {
return (
tile.status === TILE_STATUSES.NUMBER ||
(tile.mine &&
(tile.status === TILE_STATUSES.HIDDEN ||
tile.status === TILE_STATUSES.MARKED))
);
});
});
}
export function checkLose(board) {
return board.some((row) => {
return row.some((tile) => {
return tile.status === TILE_STATUSES.MINE;
});
});
}
function getMinePositions(boardSize, numberOfMines) {
const positions = [];
while (positions.length < numberOfMines) {
const position = {
x: randomNumber(boardSize),
y: randomNumber(boardSize),
};
if (!positions.some(positionMatch.bind(null, position))) {
positions.push(position);
}
}
return positions;
}
function positionMatch(a, b) {
// console.log(
// `Positionmatch ${a.x} === ${b.x} && ${a.y} === a.x === b.x && a.y === b.y${
// b.y
// } \n${a.x === b.x && a.y === b.y ? "match" : ""}`
// );
return a.x === b.x && a.y === b.y;
}
function randomNumber(size) {
return Math.floor(Math.random() * size);
}
function nearbyTiles(board, { x, y }) {
const tiles = [];
for (let xOffset = -1; xOffset <= 1; xOffset++) {
for (let yOffset = -1; yOffset <= 1; yOffset++) {
const tile = board[x + xOffset]?.[y + yOffset];
if (tile) tiles.push(tile);
}
}
return tiles;
}
export function surroundCheck(grid, position, right, below) {
if (
(position < grid.width && !below) ||
(position - grid.width > grid.tiles && below) ||
(!(position / grid.width) && -right) ||
(!(position % grid.width === grid.width - 1) && right)
)
return null;
return position + below * grid.width + right;
}