-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.vue
148 lines (136 loc) · 2.93 KB
/
index.vue
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
<script setup lang="ts">
const WIDTH = 10;
const HEIGHT = 10;
interface BlockState {
x?: number;
y?: number;
revealed: boolean;
mine?: boolean;
flagged?: boolean;
adjacentMines: number;
}
const state = reactive(
Array.from({ length: HEIGHT }, (_, y) =>
Array.from(
{ length: WIDTH },
(_, x): BlockState => ({
x,
y,
adjacentMines: 0,
revealed: false,
})
)
)
);
const directions = [
[1, 1],
[1, 0],
[1, -1],
[0, -1],
[0, 1],
[-1, 0],
[-1, 1],
[-1, -1],
];
const numberColors = [
"text-transparent",
"text-blue-500",
"text-green-500",
"text-yellow-500",
"text-orange-500",
"text-red-500",
"text-purple-500",
"text-pink-500",
"text-teal-500",
];
let mineGenerated = false;
const dev = true;
function generateMines(initial: BlockState) {
for (const row of state) {
for (const block of row) {
if (Math.abs(initial.x - block.x) < 1) continue;
if (Math.abs(initial.y - block.y) < 1) continue;
block.mine = Math.random() < 0.2;
}
}
}
function getSiblings(block: BlockState) {
return directions
.map(([dx, dy]) => {
const x1 = block.x + dx;
const y1 = block.y + dy;
if (x1 >= WIDTH || x1 < 0 || y1 >= HEIGHT || y1 < 0) return undefined;
return state[x1][y1];
})
.filter(Boolean) as BlockState[];
}
function updateNumbers() {
state.forEach((row, y) => {
row.forEach((block, x) => {
if (block.mine) return;
getSiblings(block).forEach((sibling) => {
if (sibling.mine) block.adjacentMines += 1;
});
});
});
}
function getBlockClass(block: BlockState) {
if (!block.revealed) return "bg-gray-500/40";
return block.mine ? "bg-red-500/50" : numberColors[block.adjacentMines];
}
function expandZero(block: BlockState) {
if (block.adjacentMines) return;
getSiblings(block).forEach((s) => {
if (!s.revealed) {
s.revealed = true;
expandZero(s);
}
});
}
function onClick(block: BlockState) {
if (!mineGenerated) {
generateMines(block);
updateNumbers();
mineGenerated = true;
}
if (block.mine) alert("BOOOM!");
block.revealed = true;
expandZero(block);
}
</script>
<template>
<div>
Minesweeper
<div p-5>
<div
v-for="(row, y) in state"
:key="y"
flex="~"
items-center
justify-center
>
<button
v-for="(block, x) in row"
flex="~"
items-center
justify-center
:key="x"
w-8
h-8
hover="bg-gray/10"
border="1 gray-400/30"
m="0.5"
:class="getBlockClass(block)"
@click="onClick(block)"
>
<template v-if="block.revealed || dev">
<div v-if="block.mine" i-mdi-mine></div>
<div v-else>
{{ block.adjacentMines }}
</div>
</template>
</button>
</div>
</div>
</div>
</template>