-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathminesweeper.js
131 lines (120 loc) · 3.48 KB
/
minesweeper.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
// Minefield generation and rendering
let width = 4;
let height = 4;
let num_bombs = 4; // May make this a random number
let display_chance = 0.9; // Probability that a square is uncovered
let minefield;
let selected;
// Make minefield
function generate_minefield(){
// Create matrix (there are more compact ways but this is more readable)
let minefield = [];
for (let i=0; i<height; i++){
let row = [];
for (let j=0; j<width; j++){
row.push(0);
}
minefield.push(row);
}
// Plant bombs randomly
for (let i=0; i<num_bombs; i++){
while (true){
let bomb_x = Math.floor(Math.random()*width);
let bomb_y = Math.floor(Math.random()*height);
if (minefield[bomb_y][bomb_x] == 0){
minefield[bomb_y][bomb_x] = 9;
break;
}
}
}
// Generate the remaining numbers
for (let i=0; i<height; i++){
for (let j=0; j<width; j++){
if (minefield[i][j] != 9){
let num_surrounding_bombs = 0;
for (let y=i-1; y<=i+1; y++){
if (y >= 0 && y < height){
for (let x=j-1; x<=j+1; x++){
if (x >= 0 && x < width){
if (minefield[y][x] == 9){
num_surrounding_bombs ++;
}
}
}
}
}
minefield[i][j] = num_surrounding_bombs;
}
}
}
// console.log(minefield);
return minefield;
}
// Render the minefield
function render_minefield(minefield){
let tile_grid = document.getElementById("tile-grid");
tile_grid.innerHTML = "";
for (let i=0; i<height; i++){
for (let j=0; j<width; j++){
let num = minefield[i][j];
if (num == 9){
num = 0;
}
if (Math.random() > display_chance){
num = 0;
}
let image_num = Math.floor(Math.random()*2);
tile_grid.innerHTML += `<div class=img onmousedown="select(${j}, ${i}, this)"><img src=img/${num}/${image_num}.jpg></div>`;
}
}
}
function make_minefield(){
minefield = generate_minefield();
render_minefield(minefield);
selected = [];
for (let i=0; i<height; i++){
let row = [];
for (let j=0; j<width; j++){
row.push(0);
}
selected.push(row);
}
}
make_minefield();
// Mine selection and verification
function select(x,y,elem){
elem.classList.toggle("selected");
if (selected[y][x] == 1){
selected[y][x] = 0;
}
else{
selected[y][x] = 1;
}
}
function verify(){
// Verify that all bombs are selected, and no other squares are selected
// console.log(selected);
for (let i=0; i<height; i++){
for (let j=0; j<width; j++){
if (selected[i][j] == 0 && minefield[i][j] == 9){
// console.log(i,j);
return false;
}
if (selected[i][j] == 1 && minefield[i][j] != 9){
// console.log(i,j);
return false;
}
}
}
return true;
}
function verification_handler(){
let solved = verify();
if (solved){
location.href="correct.html";
}
else{
// alert("incorrect");
make_minefield();
}
}