-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
142 lines (117 loc) · 4.97 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Game</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
</head>
<body>
<div id = "board"style="width: 300px;height: 300px;display: flex;flex-wrap: wrap;">
<div class="card" id="0"style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="1" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="2" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="3" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="4" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="5" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="6" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="7" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="8" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="9" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="10" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="11" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="12" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="13" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="14" style="border:1px solid black; width: 70px;height: 70px;"></div>
<div class="card" id="15" style="border:1px solid black; width: 70px;height: 70px;"></div>
</div>
<div>
<button onclick="game.start()">start</button>
<div class="timer">00:00.000</div>
</div>
<script>
var game = {
//Запущенна ли игра
started:false,
//Последнее открытое поле
lastCard: false,
//Всего открыто полей
opened: 0,
//Возможные цвета полей (должно быть в 2 раза меньше чем кол-во полей)
colors:['red','blue','green','orange','yellow','deeppink','aqua','darkmagenta'],
//html коллекция полей
cards: document.getElementsByClassName('card'),
//id's полей и их цвета
cardColors:{},
start:function(){
if(this.started){
return
}
let ids = []
let arr = []
let i = 0
//Заполняем массив индексами полей и рандомными цветами
while(ids.length < this.cards.length){
let max = Math.floor(Math.random() * this.cards.length);
if (ids.indexOf(max) == -1) {
ids.push(max);
if(i>=this.colors.length){
i -= this.colors.length
}
this.cardColors[max] = this.colors[i]
i++
}
}
document.getElementById("board").addEventListener("click", (event)=> {
var color = this.cardColors[event.target.id]
//Закрашиваем поле
if(color && !document.getElementById(event.target.id).style.background){
document.getElementById(event.target.id).style.background = color
}else{
return
}
//Проверяем совпадает ли цвет предыдущего поля с текущим
if (!this.lastCard) {
this.lastCard = document.getElementById(event.target.id)
}else if(this.lastCard.style.background == color){
this.lastCard = false
this.opened += 2
if(this.opened >= this.cards.length){
this.opened = 0
let time = document.getElementsByClassName('timer')[0].innerText
alert(`Вы выиграли! \nЗатраченное время : ${time}`)
for(let card of document.getElementsByClassName('card')){
card.style.background = ""
}
this.started = false
this.timer.stop()
return
}
}else{
let card = this.lastCard
this.lastCard = false
//Закрашиваем поля обратно в белый
setTimeout(()=>{
card.style.background = ''
document.getElementById(event.target.id).style.background = ''
},500)
}
});
this.timer.start()
this.started = true
},
timer: {
start:function(){
var time = moment('00:00.000','mm:ss.SSS')
this.timerId=setInterval(function(){
time.add(1,'milliseconds')
document.getElementsByClassName('timer')[0].innerText = time.format('mm:ss.SSS')
}, 1);
},
stop:function(){
clearInterval(this.timerId)
}
}
}
</script>
</body>
</html>