-
Notifications
You must be signed in to change notification settings - Fork 0
/
while.js
195 lines (173 loc) · 4.11 KB
/
while.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
let boxArr = [];
let gameSize = 10;
let bombs = 8;
let firstClick;
let win = 0;
let notReveal = true;
const cXY = [[1, -1], [1, 0], [1, 1], [0, 1],
[0, -1], [-1, -1], [-1, 0], [-1, 1]];
//[gamesize, bombs]
const levels = {
noob: [10, 8],
beginner: [12, 14],
impossibru: [25, 500]
}
const clickedZero = (x, y, i=0, dx, dy) => {
while (i < 8) {
dx = x + cXY[i][0];
dy = y + cXY[i][1];
if (boxArr[dx] && boxArr[dx][dy]) //if its 0 and unselected
if ((boxArr[dx][dy].num !== "bomb") && !boxArr[dx][dy].selected())
boxArr[dx][dy].click();
i++;
}
}
const rand = () => Math.floor(Math.random() * gameSize)
const bombNum = (x, y, i=0, n=0, dx, dy) => {
while (i < 8) {
dx = x + cXY[i][0]
dy = y + cXY[i][1];
if (boxArr[dx] && boxArr[dx][dy] && (boxArr[dx][dy].num === "bomb")) n++;
i++;
}
return n;
}
const assignNums = (x=0, y=0) => {
while (x < boxArr.length) {
y = 0;
while (y < boxArr.length) {
if (!boxArr[x][y].num) boxArr[x][y].num = bombNum(x, y);
y++;
}
x++;
}
}
const assignBombs = (x1, y1, size=bombs, x, y) => {
while (size) {
x = rand();
y = rand();
if ((x !== x1) && (y !== y1)) {
if (!boxArr[x][y].num) {
boxArr[x][y].num = "bomb";
size--;
}
}
}
}
const build = (x=0, y=0) => {
while (x < gameSize) {
boxArr.push([]);
y = 0;
while (y < gameSize) {
boxArr[x].push(new Box(x, y));
y++;
}
x++;
}
}
const start = () => {
container.style.width = `${gameSize * 50}` + `px`;
build();
toggleClicks(true);
firstClick = true;
notReveal = true;
win = 0;
}
const toggleClicks = (b, x=0, y) => {
while (x < boxArr.length) {
y = 0;
while (y < boxArr.length) {
boxArr[x][y].clickSwitch(b);
y++;
}
x++;
}
}
const revealAll = (x=0, y) => {
while (x < boxArr.length) {
y = 0;
while (y < boxArr.length) {
boxArr[x][y].click();
y++;
}
x++;
}
}
function Box (x, y) {
const box = document.createElement('div');
box.className = 'box';
container.appendChild(box);
this.num = 0;
this.clickSwitch = (b) => box.style.pointerEvents = b ? 'auto' : 'none';
this.selected = () => box.className.includes('selected');
this.flag = () => box.className.includes('flagged');
box.oncontextmenu = (e) => {
if (!this.flag()) box.classList.add('flagged');
else box.classList.remove('flagged');
e.preventDefault();
}
this.click = () => {
if (firstClick) {
assignBombs(x, y);
assignNums();
firstClick = false;
}
if (this.num === "bomb" && (notReveal)) {
win--; //incase there is only 1 number and 1 bomb left
alert("BAHAHAHHAH U HAVE LOST. U CAN TRY AGAIN OR RAGEQUIT");
insults.innerText = "HELLO U ARE A LOoO0oo0oSER HAHAHAHAHA";
toggleClicks(false);
}
if (notReveal) win++;
box.classList.add('selected');
box.classList.remove('flagged');
box.innerHTML = this.num ? this.num : '';
if (!this.num && (this.num !== "bomb")) clickedZero(x, y);
if (win === (gameSize**2 - bombs)) {
alert("YOU WIN!");
insults.innerText = "U ARE STILL A LOSER"
notReveal = false;
win = 0;
revealAll();
}
}
box.onclick = () => this.click();
}
const invalidInputs = () => {
const box = Number(boxChange.value);
const bomb = Number(bombChange.value);
if (isNaN(box) || isNaN(bomb))
return true;
if (bomb && box && (bomb >= (box ** 2))) return true;
return false;
}
const restart = (bool, level) => {
container.innerHTML = '';
boxArr = [];
if (bool) {
gameSize = level[0];
bombs = level[1];
}
else {
gameSize = Number(boxChange.value) || gameSize;
bombs = Number(bombChange.value) || bombs;
}
boxChange.value = '';
bombChange.value = '';
start();
}
reveal.addEventListener('click', () => {
if (win !== (gameSize**2 - bombs))
insults.innerText = "LOL CHEATER HAHAHAHAH";
notReveal = false;
revealAll();
});
replay.addEventListener('click', () => {
if (invalidInputs()) return alert("INVALID INPUT DETECTED, FIX PLZ >:(");
insults.innerText = "mhmmm"
restart(false);
});
noob.addEventListener('click', () => restart(true, levels.noob));
beginner.addEventListener('click', () => restart(true, levels.beginner));
impossibru.addEventListener('click', () => restart(true, levels.impossibru));
start();