-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
464 lines (443 loc) · 12.4 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
var temSquare = function() {
var t = `
<div class="square">
`
return t
}
var temSingle = function(x, y) {
var t =`
<div id='id-${x}${y}'class="single">
<span class='value'></span>
</div>
`
return t
}
var addSquare = function() {
var s = document.querySelector('.container')
var square = temSquare()
for (var i = 0; i < 10; i++) {
appendHtml(s, square)
}
}
var addSingle = function() {
var square = document.querySelectorAll('.square')
for (var i = 0; i < square.length; i++) {
for (var j = 0; j < 10; j++) {
var single = temSingle(i, j)
appendHtml(square[i], single)
}
}
}
var clonedSquare = function(array) {
var s = []
for (var i = 0; i < array.length; i++) {
var line = []
for (var j = 0; j < array[i].length; j++) {
line.push(array[i][j])
}
s.push(line)
}
return s
}
// 辅助函数, 给数字 +1
// 这里会判断下标是否合法
const plus1 = function(array, x, y) {
var n = array.length
if (x >= 0 && x < n && y >= 0 && y < n) {
if (array[x][y] != '雷') {
array[x][y] += 1
}
}
}
// 辅助函数, 用来给 9 周边的 8 个格子 +1
const markAround = function(array, x, y) {
if (array[x][y] == '雷') {
// 左边 3 个
plus1(array, x - 1, y - 1)
plus1(array, x - 1, y)
plus1(array, x - 1, y + 1)
// 上下 2 个
plus1(array, x, y - 1)
plus1(array, x, y + 1)
// 右边 3 个
plus1(array, x + 1, y - 1)
plus1(array, x + 1, y)
plus1(array, x + 1, y + 1)
}
}
var markedSquare = function(array) {
var square = clonedSquare(array)
for (var i = 0; i < square.length; i++) {
var line = square[i]
for (var j = 0; j < line.length; j++) {
markAround(square, i, j)
}
}
return square
}
// 生成一个随机数组 10 * 10
//生成0或1的值
var random = function() {
var num = Math.random()
if(num <= 0.5){
return 0
}else {
return 1
}
}
//生成随机数0-9
var random9 = function () {
var num = Math.random()*9 ;
num = parseInt(num, 10);
return num
}
//生成一个全为0的矩阵
var CreateArray = function() {
var len = 10
var width = 10
var array = []
for (var i = 0; i < len; i++) {
array[i] = []
for (var j = 0; j < width; j++) {
array[i][j] = 0
}
}
return array
}
//随机插入雷, n为雷的个数
var boom = function(array, n) {
for (var i = 0; i < n; ) {
var x = random9()
var y = random9()
var id = '#id-'+String(x) + String(y)
var s = document.querySelector(id)
//避免x,y出现重复的情况
if (! s.classList.contains('boom')) {
i++
s.classList.add('boom')
array[x][y] = '雷'
}
}
return array
}
//生成带雷矩阵
var CreateBoomArray = function(num) {
var array = CreateArray()
//设置随机雷的个数
var boomnum = num
var boomArray = boom(array,boomnum)
return boomArray
}
var showNumber = function() {
var array = CreateArray()
for (var i = 0; i < len; i++) {
for (var j = 0; j < width; j++) {
number = array[i][j]
}
}
return number
}
var addHide = function() {
var elements = document.querySelectorAll('.value')
for (var i = 0; i < elements.length; i++) {
var e = elements[i]
e.classList.add('hide')
}
}
// 把新建随机数组的值赋值到HTML里面
var showArray = function(array) {
var square = document.querySelectorAll('.square')
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10; j++) {
var number = array[i][j]
// log('number',number)
var id = '#id-'+String(i) + String(j)
var s = document.querySelector(id)
var child = find(s, '.value')
child.innerText = array[i][j]
// log('child',child)
// var span = $(id).children('span')
// span.innerHTML = number
}
}
}
// 屏蔽右键菜单
var forbidRightShow = function() {
var container = e('.container')
container.oncontextmenu = function(){
return false
}
}
//从html里面获取数组
var divArray = function() {
var len = 10
var width = 10
var divarray = []
for (var i = 0; i < len; i++) {
divarray[i] = []
for (var j = 0; j < width; j++) {
var id = '#id-'+String(i) + String(j)
var s = document.querySelector(id)
var span = s.children[0]
if(span.innerHTML != '雷') {
divarray[i][j] = parseInt(span.innerHTML)
}else {
divarray[i][j] = span.innerHTML
}
}
}
return divarray
}
// 从html里面获取此时雷的个数
var getBoomNum = function() {
var htmlArray = divArray()
var boomNum = 0
for (var i = 0; i < htmlArray.length; i++) {
for (var j = 0; j < htmlArray.length; j++) {
if(htmlArray[i][j] == '雷') {
boomNum += 1
}
}
}
return boomNum
}
var initShow = function() {
addSquare()
addSingle()
forbidRightShow()
//导入雷矩阵
var array = CreateBoomArray(15)
// showArray(array)
// //导入雷旁边的个数
// var divarray = divArray()
var markarray = markedSquare(array)
showArray(markarray)
addHide()
}
var refresh = function(event) {
var container = e('.container')
var showMark = e('#id-showmark')
container.innerHTML = null
addSquare()
addSingle()
var item = event.target
var boom = parseInt(item.value)
showMark.innerHTML = boom
var array = CreateBoomArray(boom)
var markarray = markedSquare(array)
showArray(markarray)
addHide()
}
//点击是0则显示zero样式
var plus2 = function(array, x, y) {
var n = array.length
if (x >= 0 && x < n && y >= 0 && y < n) {
var id = '#id-'+String(x) + String(y)
var s = e(id)
var judge0 = s.classList.contains('showZero')
if (array[x][y] == 0 && !judge0) {
s.classList.add('showZero')
//点击为0 则递归处理这个格子外面的8个格子
markAround2(array, x, y)
}
else if(array[x][y] != 0 && array[x][y] !='雷') {
//出现数字则显示出雷的个数
removeHide(s)
}
// if(array[x][y] !=0 && array[x][y] != '雷') {
// log('点击到数字')
// removeHide(s)
// }
}
}
var markAround2 = function(array, x, y) {
// 左边 3 个
if(array[x][y] == 0 ){
plus2(array, x - 1, y - 1)
plus2(array, x - 1, y)
plus2(array, x - 1, y + 1)
// 上下 2 个
plus2(array, x, y - 1)
plus2(array, x, y + 1)
// 右边 3 个
plus2(array, x + 1, y - 1)
plus2(array, x + 1, y)
plus2(array, x + 1, y + 1)
}
else if(array[x][y] != 0 && array[x][y] != '雷') {
console.log('出现问题', array[x][y])
var id = '#id-'+String(x) + String(y)
var s = e(id)
removeHide(s)
}
}
var removeClassAll = function(className) {
var selector = '.' + className
var elements = document.querySelectorAll(selector)
for (var i = 0; i < elements.length; i++) {
var e = elements[i]
e.classList.remove(className)
}
}
var removeHide = function(item) {
var span = item.children[0]
if(span.classList.contains('hide')){
span.classList.remove('hide')
}
}
/*
1.遍历格子上的每个数,把没有显示出来的都给显示出来,如果显示出来则不动
*/
//当扫到雷时把游戏里面所有的空格都显示出来
var endshow = function(item,divarray) {
var all = document.querySelectorAll('.single')
for (var i = 0; i < all.length; i++) {
var single = all[i]
var value = single.children[0].innerHTML
if(value == 0 && !single.classList.contains('redFlag') && !single.classList.contains('boom')) {
single.classList.add('showZero')
}else {
single.children[0].classList.remove('hide')
}
}
}
var addTime = function() {
var startTime = e('#start-time')
var time = parseInt(startTime.innerHTML)
var time = time + 1
startTime.innerHTML = time
}
var controlTime = function(index){
var startTime = e('#start-time')
if (index =='start') {
if (!startTime.classList.contains('time')) {
startTime.classList.add('time')
timer1 =setInterval('addTime()',1000)
}
}else if (index =='stop') {
clearInterval(timer1)
}
}
var ChangeFlagNum = function(index) {
var showMark = e('#id-showmark')
var num = parseInt(showMark.innerHTML)
if(num <= 0) {
showMark.innerHTML= 0
}else {
if (index =='down') {
num -= 1
showMark.innerHTML = num
} else if (index == 'up') {
num += 1
showMark.innerHTML = num
}
}
}
var markFlag = function(item, initNum) {
if (!item.classList.contains('redFlag')) {
var template = `
<img class='image' src="./image/红旗.png" alt="1">
`
var innerHTML = item.innerHTML
item.innerHTML = null
appendHtml(item, template)
ChangeFlagNum('down')
} else {
var template = `
<span class='hide'>${initNum}</span>
`
var innerHTML = item.innerHTML
item.innerHTML = null
appendHtml(item, template)
ChangeFlagNum('up')
item.classList.remove('redFlag')
return
}
item.classList.add('redFlag')
}
var markBoom = function() {
var template = `
<img src="./image/boom2.png" alt="">
`
var single = document.querySelectorAll('.single')
for (var i = 0; i < single.length; i++) {
var item = single[i]
var span = item.children[0]
if(span.innerHTML == '雷') {
item.innerHTML = null
appendHtml(item, template)
}
}
}
var judegeWin = function() {
var boom = getBoomNum()
if(boom == 0){
alert('你赢了')
}
}
// bind click
var bindSingle = function() {
var divarray = divArray()
var container = e('.container')
var showMark = e('#id-showmark')
bindEvent(container, 'mousedown', function(event){
var num = parseInt(showMark.innerHTML)
controlTime('start')
var item = event.target
// btnNum 0为左键,1为中建,右键为2
if(item.classList.contains('value') ||item.classList.contains('image')){
item = event.target.parentNode
}
var btnNum = event.button
var value = item.children[0].innerHTML
log('value', value)
//获取click的方格ID的数组编号
var xIndex = parseInt(item.id.split('-')[1][0])
var yIndex = parseInt(item.id.split('-')[1][1])
//根据左右键执行不同程序
if(btnNum == 0) {
if(value == '雷') {
markBoom()
controlTime('stop')
removeHide(item)
//遍历出所有元素。
endshow(item)
toolTip('提示','你输了!',callback)
}
else if(value == 0) {
//显示该格的8个按钮
judegeWin()
item.classList.add('showZero')
markAround2(divarray, xIndex, yIndex)
}else {
log('cowu')
judegeWin()
removeHide(item)
}
}
//right click
else if(btnNum == 2 ) {
var initNum = divarray[xIndex][yIndex]
log('init',initNum)
if(num >= 0) {
markFlag(item, initNum)
}
judegeWin()
}
})
}
//
var bindButtons = function(selector, eventName, callback){
var elements = document.querySelectorAll(selector)
for (var i = 0; i < elements.length; i++) {
var button = elements[i]
var createBoom = button.value
bindEvent(button, 'click', callback )
}
}
var _main = function() {
initShow()
bindSingle()
bindButtons('button', 'click',refresh)
}
_main()