-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
320 lines (268 loc) · 7.78 KB
/
script.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
log("Script loaded");
var numbersTable = [];
var elementsTable = [];
var sizeX = 16*7;
var sizeY = 16*3;
var mineCount = 650;
var mouseDown = false;
/*
0-9 "non-mine," number represents bordering tiles
-1 mine
*/
const TILE_0 = 0;
const TILE_1 = 1;
const TILE_2 = 2;
const TILE_3 = 3;
const TILE_4 = 4;
const TILE_5 = 5;
const TILE_6 = 6;
const TILE_7 = 7;
const TILE_8 = 8;
const TILE_9 = 9;
const TILE_MINE = -1;
//Gets a random number, between OR INCLUDING the min and max values
function random(min, max)
{
return Math.floor(Math.random() * ((max + 1) - min) + min);
}
//Entry point
window.onload = function()
{
log("Page loaded");
//Functions to monitor whether the mouse button is down or up.
document.body.onmousedown = function() { mouseDown = true; }
document.body.onmouseup = function() { mouseDown = false; }
//Fills out the internal gameboard state
for(var y = 0; y < sizeY; y++)
{
numbersTable[y] = [];
for(var x = 0; x < sizeX; x++)
numbersTable[y][x] = {value: 0, covered: true, flagged: false};
}
//TODO: This is poorly optimized. High mine saturation on large board may cause problems.
//Place mines
let minesPlaced = 0;
while(minesPlaced < mineCount)
{
x = random(0, sizeX - 1);
y = random(0, sizeY - 1);
if(numbersTable[y][x].value != TILE_MINE)
{
numbersTable[y][x].value = TILE_MINE;
//numbersTable[y][x].covered = false;
minesPlaced++;
}
}
//Places tile mine neighbor counts
for(var x = 0; x < sizeX; x++)
for(var y = 0; y < sizeY; y++)
if(numbersTable[y][x].value != TILE_MINE)
{
var mines = getNeighborMineCount(x, y);
numbersTable[y][x].value = mines;
}
//Create HTML elements
createGameboard();
}
function getNeighborMineCount(tileX, tileY)
{
var mineCount = 0;
let neighbors = getNeighbors(tileX, tileY)
for(var i = 0; i < neighbors.length; i++)
if(numbersTable[neighbors[i].y][neighbors[i].x].value == TILE_MINE)
mineCount++;
return mineCount;
}
function getNeighborsElems(tileX, tileY)
{
let neighbors = [];
for(let x = tileX - 1; x <= tileX + 1; x++)
for(let y = tileY - 1; y <= tileY + 1; y++)
if(!(x == tileX && y == tileY) && (x >= 0) && (y >= 0) && (x < sizeX) && (y < sizeY))
neighbors.push(elementsTable[y][x]);
return neighbors;
}
function getNeighbors(tileX, tileY)
{
let neighbors = [];
for(let x = tileX-1; x <= tileX+1; x++)
for(let y = tileY-1; y <= tileY+1; y++)
if(!(x == tileX && y == tileY) && (x >= 0) && (y >= 0) && (x < sizeX) && (y < sizeY))
neighbors.push({x: x, y: y});
return neighbors;
}
//Visual function
function downify(elem)
{
if(numbersTable[elem.y][elem.x].covered && !numbersTable[elem.y][elem.x].flagged)
elem.style.backgroundPosition = "0px 0px"
}
//Visual function
function upify(elem)
{
if(numbersTable[elem.y][elem.x].covered && !numbersTable[elem.y][elem.x].flagged)
elem.style.backgroundPosition = "0px -16px"
}
//Visual function
function cellMouseDown(args)
{
downify(args.target);
}
//Visual function
function cellMouseOver(args)
{
if(mouseDown)
downify(args.target);
}
//Visual function
function cellMouseOut(args)
{
upify(args.target);
}
function cellContextMenu(args)
{
return false;
}
function lose()
{
for(var x = 0; x < sizeX; x++)
for(var y = 0; y < sizeY; y++)
numbersTable[y][x].covered = false;
}
//TODO: Functionality of "clicking" a tile
function cellMouseUp(args)
{
var elem = args.target;
var x = elem.x;
var y = elem.y;
upify(elem);
if(args.which == 1)
{
if(!numbersTable[y][x].flagged)
{
numbersTable[y][x].covered = false;
if(numbersTable[y][x].value == TILE_MINE)
lose();
function expose(x, y)
{
var neighbors = getNeighbors(x, y, true);
for(var i = 0; i < neighbors.length; i++)
{
if(numbersTable[neighbors[i].y][neighbors[i].x].value >= 0 &&
numbersTable[neighbors[i].y][neighbors[i].x].covered == true &&
!numbersTable[neighbors[i].y][neighbors[i].x].flagged)
{
numbersTable[neighbors[i].y][neighbors[i].x].covered = false;
if(numbersTable[neighbors[i].y][neighbors[i].x].value == 0)
expose(neighbors[i].x, neighbors[i].y);
}
}
}
expose(x, y);
}
}
else
{
if(numbersTable[y][x].covered)
numbersTable[y][x].flagged = !numbersTable[y][x].flagged;
}
updateElems();
}
//Creates the table/elements for the gameboard
function createGameboard()
{
for(var y = 0; y < sizeY; y++)
elementsTable[y] = [];
var gameTable = createElement("table", "game", document.body);
for(let y = 0; y < sizeY; y++)
{
var row = createElement("tr", null, gameTable);
for(let x = 0; x < sizeX; x++)
{
let cell = createElement("td", "cell", row);
cell.onmousedown = cellMouseDown;
cell.onmouseup = cellMouseUp;
cell.onmouseover = cellMouseOver;
cell.onmouseout = cellMouseOut;
cell.oncontextmenu = cellContextMenu;
elementsTable[y][x] = cell;
//This may not be "legal," but it works- and improves performance
cell.x = x;
cell.y = y;
}
}
updateElems();
//TODO: Re-enable
/*
var buttonSolve = createElement("button", null, document.body)
buttonSolve.innerHTML = "Solve"
buttonSolve.onclick = solve;
var buttonSlowSolve = createElement("button", null, document.body)
buttonSlowSolve.innerHTML = "Slow Solve"
buttonSlowSolve.onclick = slowSolve;
*/
var buttonReset = createElement("button", null, document.body)
buttonReset.innerHTML = "Reset"
buttonReset.onclick = reset;
};
//Updates the background off all elements based on their gameboard value
function updateElems()
{
for(var x = 0; x < sizeX; x++)
for(var y = 0; y < sizeY; y++)
{
elementsTable[y][x].style.backgroundPosition = (numbersTable[y][x].value * -16).toString() + "px 0px";
if(numbersTable[y][x].value == TILE_MINE)
elementsTable[y][x].style.backgroundPosition = "-64px -16px";
if(numbersTable[y][x].covered == true)
if(numbersTable[y][x].flagged)
elementsTable[y][x].style.backgroundPosition = "-16px -16px";
else
elementsTable[y][x].style.backgroundPosition = "0px -16px";
}
}
//TODO: Implement
function reset()
{
}
//TODO: Implement
function makeAMove()
{
return true;
}
//TODO: Implement
function solve()
{
while(!checkComplete() && makeAMove()){}
return checkComplete();
}
//TODO: Implement
function slowSolve()
{
if(!checkComplete() && makeAMove())
setTimeout(slowSolve, 0);
else
return checkComplete();
}
//TODO: Implement
//Returns true if the game is solved. Returns false otherwise.
function checkComplete()
{
return false;
return true;
}
//Creates an element of type elemType, sets className to className, and appends it as a child to parent
function createElement(elemType, className, parent)
{
var elem = document.createElement(elemType);
if(className)
elem.className = className;
if(parent)
parent.appendChild(elem);
return elem;
}
//Alias for console.log
function log(msg)
{
console.log(msg);
}