-
Notifications
You must be signed in to change notification settings - Fork 0
/
扫雷.js
290 lines (271 loc) · 6.8 KB
/
扫雷.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
// //
// [1 1 1 9 1 0,
// 9 1 1 1 1 0,
// 2 9 1 0 0 0,
// 1 1 1 1 1 1,
// 0 0 0 1 9 1,
// 0 0 0 1 1 1,
// ]
//
//
// // 1.如果点到 0 显示周围9个
// // 2.如果0周围9个数字里有0
//
// 1.得到周围8个数字 2.给8个数字显示出来 3.如果其中有0 在执行whenzero
var clickaaa = function(array, x, y) {
var n = array.length
var a = ['id', 'cell']
var className = 'clicked'
var className2 = 'show'
if (x >= 0 && x < n && y >= 0 && y < n) {
a.push(x)
a.push(y)
var Id = '#' + a.join('-')
var element = e(Id)
element.classList.add(className)
element.firstChild.classList.add(className2)
// if (array[x][y] == 0) {
// whenzero(array, x, y)
// }
}
}
var leftClicked = function(x, y, element) {
var a = showmap(x, y)
element.classList.add('clicked')
element.firstChild.classList.add('show')
if (a == 0) {
whenzero(x, y)
}
}
var clickPos = function(x, y) {
if( x < 0 || x > 11 ) {
return
}
if( y < 0 || y > 11 ) {
return
}
var id = `#id-cell-${x}-${y}`
var a = e(id)
if( !a.classList.contains('clicked') ) {
leftClicked(x, y, a)
}
}
var whenzero = function (x, y) {
for(var i = x - 1; i <= x + 1; i++) {
for(var j = y - 1; j <= y + 1; j++) {
clickPos(i, j)
}
}
}
//制作显示页面的地图
var content = function (a, b) {
for (var i = 0; i < a; i++) {
var t1 = `
<div class="square-line-${i}"></div>
`
var ele = e('.container')
appendHtml(ele, t1)
for (var j = 0; j < b; j++) {
var t = `
<div class="square" id="id-cell-${i}-${j}"><span class="value"></span></div>
`
var selector = '.square-line-' + i
var element = e(selector)
appendHtml(element, t)
}
}
}
var container = function (grade) {
if (grade == '中级') {
var t = `
<div class="container" style='width: 300px; height: 300px;'></div>
`
var ele = e('.item')
appendHtml(ele, t)
content(12, 12)
} else if (grade == '高级') {
var t = `
<div class="container" style='width: 500px; height: 500px;'></div>
`
var ele = e('.item')
appendHtml(ele, t)
content(20, 20)
}
}
//制作显示页面的地图
// var map = area()
// 给所有地图中的元素绑定area数
var bindArea = function (map) {
var squares = es('.square')
for (var i = 0; i < squares.length; i++) {
//取id 给span的value添加innerhtml
var a = squares[i].id
var x = a.split('-')[2]
var y = a.split('-')[3]
var t = showmap(x, y)
if (t == 9) {
t = '雷'
} else if (t == 0) {
t = ''
}
sle = squares[i].firstChild
sle.innerHTML = t
}
}
// 对地图绑定‘click’事件
/* 1. 对所有suqare绑定事件
2. 可能点到提示数字 给他div加上clicked 给span加上 show
3.可能点到 雷 给所有 div加上clicked 给所有span加上 show
4.点到0 (暂时不考虑)
*/
var showmap = function (x, y) {
return map[x][y]
}
//得到map的对应的值
// getValue = function (id) {
// var x = id.split('-')[2]
// var y = id.split('-')[3]
// return map[x][y]
// }
var winGame = function (leishu) {
var a = es('.square')
var nub = 0
for (var i = 0; i < a.length; i++) {
if (a[i].classList.contains('clicked')) {
nub += 1
}
}
// 总数144
if (nub == winnub) {
log('texxxx', winnub)
alert('恭喜你赢了!')
stopTime()
}
}
const textbeginTime = function () {
var selector = '.clicked'
var selector2 = '.white'
var a = es(selector).length
var b = es(selector2).length
var time = e('#id-span-time')
if (a == 0 && b == 0) {
time.innerHTML = 0
beginTime(time)
}
}
const beginTime = function (a) {
var time = a
timeSetint = setInterval(function() {
var t = parseInt(time.innerHTML) + 1
time.innerHTML = t
}, 1000)
}
const stopTime = function () {
if (timeSetint == 0) {
return
} else {
clearInterval(timeSetint)
}
}
var bindAllSquare = function (map) {
var selector ='.square'
bindAll(selector, 'click', function (event) {
textbeginTime()
var a = event.target.id
var x = a.split('-')[2]
var y = a.split('-')[3]
var t = showmap(x, y)
var className = 'clicked'
var className2 = 'show'
var selector2 = '.value'
if (t == 9) {
addClassAll(selector, className)
addClassAll(selector2, className2)
alert('你输了!')
stopTime()
} else if (t == 0) {
event.target.classList.add(className)
//显示周围9个数
whenzero(parseInt(x), parseInt(y))
} else {
event.target.classList.add(className)
event.target.firstChild.classList.add(className2)
}
winGame()
})
//右击绑定事件
document.oncontextmenu = function(e){
// e.preventDefault();
var e = window.event
if(e.button == "2"){
var element = e.target
if (element.classList.contains('square')) {
e.preventDefault();
className = 'white'
toggleClass(element, className)
var whites = document.querySelectorAll('.white')
var nn = document.querySelector('#id-span-mines')
nn.innerHTML = leinubs - whites.length
}
}
}
// document.onmousedown = function(e){
// e.preventDefault();
// var e = e || window.event
// if(e.button == "2"){
// log(e.target)
// }
// }
}
var leinub = function (a) {
leinubs = a
var nn = e('#id-span-mines')
nn.innerHTML = a
}
// 对难度绑定click事件
var bindlever = function () {
var selector ='.start'
bindAll(selector, 'click', function (event) {
stopTime()
var time = e('#id-span-time')
time.innerHTML = 0
// textbeginTime()
var lever = event.target.value
// map = area(lever
log('text', lever)
var className = 'clicked'
var className1 = 'white'
var className2 = 'show'
removeClassAll(className)
removeClassAll(className1)
removeClassAll(className2)
// 6 12 20
if (lever == 6) {
window.map = area(15)
window.winnub = 129
bindArea()
leinub(15)
} else if (lever == 12) {
window.map = area(20)
window.winnub = 124
bindArea()
leinub(20)
} else if (lever == 20) {
window.map = area(25)
window.winnub = 119
bindArea()
leinub(25)
}
})
}
var __main = function () {
window.leinubs = 15
window.winnub = 129
window.timeSetint = 0
window.map = area(15)
container('中级')
bindArea(map)
bindAllSquare(map)
bindlever()
}
__main()