Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
187 lines (161 loc) · 6.65 KB
/
Copy pathindex.html
File metadata and controls
187 lines (161 loc) · 6.65 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<!DOCTYPE html>
<html>
<head>
<title>Ambiguous Minesweeper</title>
<style>
body { display: flex; flex-direction: column; align-items: center; background: #222; color: white; font-family: sans-serif; }
canvas { border: 4px solid #555; cursor: pointer; background: #eee;}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
const TILE_SIZE = 40;
const SIZEX = Math.floor(canvas.width / TILE_SIZE);
const SIZEY = Math.floor(canvas.height / TILE_SIZE); // 10x10 grid
const FIELD = SIZEX*SIZEY; //100
const POS_MINES = Math.floor((8/100)*(FIELD)); // 8x100 -> 4x50 -> 2x25s
const NEG_MINES = Math.floor((8/100)*(FIELD));
canvas.width = SIZEX * TILE_SIZE;
canvas.height = SIZEY * TILE_SIZE;
let grid = [];
let gameOver = false;
function initGame() {
grid = [];
// Create empty grid
for (let y = 0; y < SIZEY; y++) {
grid[y] = [];
for (let x = 0; x < SIZEX; x++) {
grid[y][x] = { x, y, mine: 0, revealed: false, flagged: false, count: 0 };
}
}
// Place Positive Mines (+1)
placeMines(POS_MINES, 1);
// Place Negative Mines (-1)
placeMines(NEG_MINES, -1);
// Calculate Sums
for (let y = 0; y < SIZEY; y++) {
for (let x = 0; x < SIZEX; x++) {
if (grid[y][x].mine === 0) {
grid[y][x].count = getSum(x, y);
}
}
}
draw();
}
function placeMines(count, type) {
let placed = 0;
while (placed < count) {
let x = Math.floor(Math.random() * SIZEX);
let y = Math.floor(Math.random() * SIZEY);
if (grid[y][x].mine === 0) {
grid[y][x].mine = type;
placed++;
}
}
}
function getSum(x, y) {
let sum = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
if (grid[y + i] && grid[y + i][x + j]) {
sum += grid[y + i][x + j].mine;
}
}
}
return sum;
}
function floodFill(x, y) {
if (x < 0 || x >= SIZEX || y < 0 || y >= SIZEY) return;
const tile = grid[y][x];
if (tile.revealed || tile.mine !== 0) return;
tile.revealed = true;
// Only continue filling if the sum is exactly 0
if (tile.count === 0) {
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
floodFill(x + j, y + i);
}
}
}
}
function valueToColor(v) {
// Clamp to the allowed range – just in case.
v = Math.max(-9, Math.min(9, Math.round(v)));
// ----- colour anchors (in 0‑255 RGB) -----
const gray = [128, 128, 128]; // #808080
const cyan = [0x00, 0x00, 0xff]; // #08f → (0,136,255)
const orange = [0xff, 0x00, 0x00]; // #f80 → (255,136,0)
// Choose the correct end‑point for the sign of v.
const end = v < 0 ? cyan : orange;
// Normalise the distance from the centre: 0 → 0, ±8 → 1.
const t = Math.abs(v) / 8;
// Linear interpolation for each channel.
const r = Math.round(gray[0] + (end[0] - gray[0]) * t);
const g = Math.round(gray[1] + (end[1] - gray[1]) * t);
const b = Math.round(gray[2] + (end[2] - gray[2]) * t);
// Convert back to a hex string.
const toHex = n => n.toString(16).padStart(2, '0');
return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (let y = 0; y < SIZEY; y++) {
for (let x = 0; x < SIZEX; x++) {
const tile = grid[y][x];
ctx.strokeStyle = "#999";
ctx.strokeRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
if (tile.revealed) {
ctx.fillStyle = "#fff";
ctx.fillRect(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2);
if (tile.mine !== 0) {
ctx.fillStyle = tile.mine > 0 ? "red" : "blue";
ctx.fillRect(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2);
} else {
ctx.fillStyle = valueToColor(tile.count);
ctx.fillRect(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2);
}
} else {
ctx.fillStyle = "#bbb";
ctx.fillRect(x * TILE_SIZE + 1, y * TILE_SIZE + 1, TILE_SIZE - 2, TILE_SIZE - 2);
if (tile.flagged) {
ctx.fillStyle = "orange";
ctx.fillRect(x * TILE_SIZE + 10, y * TILE_SIZE + 10, 20, 20);
}
}
}
}
}
canvas.addEventListener('mousedown', (e) => {
if (gameOver) return initGame() || (gameOver = false);
const rect = canvas.getBoundingClientRect();
const x = Math.floor((e.clientX - rect.left) / TILE_SIZE);
const y = Math.floor((e.clientY - rect.top) / TILE_SIZE);
const tile = grid[y][x];
if (e.button === 2) { // Right click
tile.flagged = !tile.flagged;
} else { // Left click
if (tile.flagged) return;
if (tile.mine !== 0) {
revealAll();
alert("BOOM! Game Over.");
gameOver = true;
} else {
floodFill(x, y);
}
}
draw();
});
function revealAll() {
grid.forEach(row => row.forEach(t => t.revealed = true));
}
// Prevent context menu on right click
canvas.oncontextmenu = (e) => e.preventDefault();
initGame();
</script>
</body>
</html>