-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (109 loc) · 3.24 KB
/
index.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
/*
Generate button => field of n lis where n is max elements fit in window x y
how many fit?
x = (innerX - padding) / li width .floor
y = (inner)y - nav+padding) li height .floor
n = x*y
test:
innerHeight = 667
18 px on top
nav = 47 + 20 pad + 20 margin
105 top
innerWidth = 1027
body margin+padding = 18 ( x 2 for each side)
ul width - 991 body 1011 ?
li w x h = 110 x 110
w 48px font, 7 top btm padding, 10 margin
x = ( 1027 - 18(*2 ?) ) / 110 = 9
y = (667 - 105) / 110 = 5.1
x = Math.floor(( window.innerWidth - 36 )/110)
y = Math.floor(( window.innerHeight - 105 )/110)
gameplay:
poopDestroy()
touch poop destroys the poop and one bomb,
bombDestroy()
touch bomb destroys all poop and itself
fieldCondition() ?
if poop and bombs - playing
if no poop but bombs - lose
if no bombs but poop - lose
if nothing - win
*/
//GENERATE
let generateButton = document.getElementById("newGame")
let fieldUL = document.querySelector("#field")
generateButton.addEventListener("click", makeField)
makeField();
function makeField(){
// delete current:
while (fieldUL.firstChild) {
fieldUL.removeChild(fieldUL.firstChild);
};
//solve for n
x = Math.floor(( window.innerWidth - 36 )/110)
y = Math.floor(( window.innerHeight - 105 )/110)
let times = x*y;
//make new
for(let i=0; i < times; i++){
let newLi = document.createElement("li")
let test = Math.random()
if (test > 0.2) {
newLi.innerText = "💩";
newLi.classList.add("poop");
} else {
newLi.innerText = "💣";
newLi.classList.add("bomb");
}
fieldUL.append(newLi);
};
};
//POOP + BOMB DESTROY
fieldUL.addEventListener("click", function(evt){
if (evt.target.innerText === "💣") {
evt.target.remove();
poops = fieldUL.querySelectorAll(".poop");
poops.forEach(e => {
e.remove();
});
fieldCondition()
} else if (evt.target.innerText === "💩") {
evt.target.remove();
bombs = fieldUL.getElementsByClassName("bomb");
place = Math.floor(Math.random()*bombs.length);
bombs[place].remove();
fieldCondition()
} else {};
})
//FIELD CONDITION
function fieldCondition() {
bombs = fieldUL.getElementsByClassName("bomb");
poops = fieldUL.getElementsByClassName("poop");
if (bombs.length > 0 && poops.length > 0) {
// keep playing
} else if (poops.length == 0 && bombs.length > 0) {
Lose()
} else if (bombs.length == 0 && poops.length > 0) {
Lose()
} else {
Win()
};
}
//DISPLAY WIN / LOSE
function Lose() {
over = document.getElementById("overlay");
text = document.getElementById("text");
text.innerText = "You really mucked this one up.";
over.style.display = 'block';
// alert("You failed.")
}
function Win() {
over = document.getElementById("overlay");
text = document.getElementById("text");
text.innerText = "MUCK YEAH!";
over.style.display = 'block';
// alert("MUCK YEAH!")
}
function off() {
makeField()
document.getElementById("overlay").style.display = "none";
}