-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
executable file
·156 lines (136 loc) · 3.48 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
const field = document.getElementById("field")
class Cell {
constructor() {
this.isBomb = false;
this.isOpen = false;
this.nearbyBombs = 0;
this.flagged = false;
this.component = document.createElement("div");
field.appendChild(this.component);
this.component.classList.add("cell");
this.nearbyCells = [];
this.component.onclick = () => expandCell(this);
this.component.oncontextmenu = () => {event.preventDefault();};
let self = this;
this.component.addEventListener('mouseup', (e) => flagCell(e, self));
}
}
// Adding cells to a field_____________________________________
let map = [];
let bombs = [];
let bombCount = 0;
for(let i = 0; i < 8; i++){
let row = [];
for(let j = 0; j < 8; j++){
let cc = new Cell();
row.push(cc);
}
map.push(row);
}
// Adding nearbyCells____________________________________________
for(let i = 0; i < 8; i++){
for(let j = 0; j < 8; j++){
let cc = map[i][j];
for(let i1 = i - 1; i1 <= i + 1; i1++){
for(let j1 = j - 1; j1 <= j + 1; j1++){
if(i1 != i || j1 != j){
if(typeof map[i1] != 'undefined'){
if(typeof map[i1][j1] != 'undefined'){
let cp = map[i1][j1];
cc.nearbyCells.push(cp);
}
}
}
}
}
}
}
// Bomb Generation____________________________________________
while(bombCount < 10){
let x = Math.round(Math.random() * 7);
let y = Math.round(Math.random() * 7);
let cell = map[x][y];
if(!cell.isBomb){
bombs.push(cell);
cell.isBomb = true;
cell.component.onclick = bombExplode;
bombCount++;
}
}
// Counting nearbyBombs_____________________________________________
for(let i of map){
for(let j of i){
if(!j.isBomb)
for(let k of j.nearbyCells) if(k.isBomb) j.nearbyBombs++;
}
}
// Needed Functions_____________________________________________
function bombExplode() {
for(let i of bombs){
i.component.innerHTML = "<h2>*</h2>";
console.log("Bomb");
}
gameover(false);
}
function expandCell(self) {
self.isOpen = true;
self.component.onclick = null;
self.component.classList.add("open");
if(self.nearbyBombs > 0) {
self.component.innerHTML = self.nearbyBombs;
self.component.onclick = () => expandOpenCell(self);
}
else {
for(let i of self.nearbyCells){
if(i.flagged) {
i.flagged = false;
i.component.classList.remove("flagged");
i.component.onclick = () => self.isBomb? bombExplode(): expandCell(self);
}
else if(!i.isOpen) {
expandCell(i);
}
}
}
console.log(self);
}
function expandOpenCell(self) {
let flags = 0;
for(let i of self.nearbyCells) if(i.flagged) flags++;
console.log(flags + " expanded");
if(flags == self.nearbyBombs)
for(let i of self.nearbyCells){
if(!i.flagged && !i.isOpen){
if(i.isBomb) bombExplode();
else expandCell(i);
}
}
}
function flagCell(e, self) {
console.log(e.button, self.flagged);
console.log(self)
if(e.button == 2) {
if(!self.flagged){
self.flagged = true;
self.component.classList.add("flagged");
self.component.onclick = null;
}
else {
self.flagged = false;
self.component.classList.remove("flagged");
self.component.onclick = () => self.isBomb? bombExplode(): expandCell(self);
}
if(checkWin()) gameover(true);
}
}
function checkWin(){
for(i of bombs) if(!i.flagged) return false;
return true;
}
function gameover(win){
let gamePrompt = document.getElementById("gameover");
gamePrompt.innerHTML = (win)? "<h1>You won</h1>": "<h1>You lost</h1>";
gamePrompt.style.zIndex = 1;
}
//______________________________________________________________
console.log(bombs);