-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
172 lines (135 loc) · 5.17 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
let duration = 1000;
let blocksContainer = document.querySelector(".memory-game-blocks");
let blocks = Array.from(blocksContainer.children)
let timerMin = document.querySelector('.timer .min')
let timerSec = document.querySelector('.timer .sec')
let orderRange = Array.from(Array(blocks.length).keys())
document.querySelector(".control-buttons span").onclick = function(){
let yourName = prompt("أدخل أسمك هنا؟")
if(yourName == null || yourName == ""){
document.querySelector(".name span").innerHTML = "Unknown"
}else {
document.querySelector(".name span").innerHTML = yourName
}
document.querySelector(".control-buttons").remove()
for(let i = 0;i < blocks.length;i++ ){
blocks[i].classList.add("is-flipped")
}
setTimeout(function(){
for(let i = 0;i < blocks.length;i++ ){
blocks[i].classList.remove("is-flipped")
}
},3000)
let ti = setInterval(function(){
let timeMin = parseInt(timerMin.innerHTML)
let timeSec = parseInt(timerSec.innerHTML)
timerSec.innerHTML -= 1
if (timeMin > 0 && timeSec == 0){
timerMin.innerHTML -= 1
timerSec.innerHTML = 59
}
if(timeSec== 0 && timeMin == 0 ){
timerMin.textContent = `00`
timerSec.textContent = `00`
document.querySelector(".lose").classList.remove("loser-off")
document.querySelector(".lose").classList.add("loser-on")
clearInterval(ti)
}
for(let i = 0;i < blocks.length;i++ ){
let hasMatch= blocks.filter(hasMatch => hasMatch.classList.contains("has-match"))
if (hasMatch.length == 20 ){
document.querySelector(".win").classList.remove("winner-off")
document.querySelector(".win").classList.add("winner-on")
return clearInterval(ti)
}
}
},1000)
}
document.querySelector('.lose button').onclick = function(){
restart()
document.querySelector(".lose").classList.add("loser-off")
function restart(){
shuffle(orderRange)
blocks.forEach((block, index) => {
block.style.order = orderRange[index]
block.addEventListener("click", function () {
flipBlock(block);
})
})
for(let i = 0;i < blocks.length;i++ ){
blocks[i].classList.add("is-flipped")
}
setTimeout(function(){
for(let i = 0;i < blocks.length;i++ ){
blocks[i].classList.remove("is-flipped")
blocks[i].classList.remove('has-match')
}
},4000)
timerMin.innerHTML = "01"
timerSec.innerHTML = "30"
setInterval(function(){
let timeMin = parseInt(timerMin.innerHTML)
let timeSec = parseInt(timerSec.innerHTML)
timerSec.innerHTML -= 1
if (timeMin > 0 && timeSec == 0){
timerMin.innerHTML -= 1
timerSec.innerHTML = 59
}
if(timeSec== 0 && timeMin == 0 ){
timerMin.textContent = `00`
timerSec.textContent = `00`
document.querySelector(".lose").classList.remove("loser-off")
document.querySelector(".lose").classList.add("loser-on")
}
},1000)
}
}
shuffle(orderRange)
blocks.forEach((block, index) => {
block.style.order = orderRange[index]
block.addEventListener("click", function () {
flipBlock(block);
})
})
function flipBlock(selectedBlock){
selectedBlock.classList.add("is-flipped")
let allFlippedBlocks = blocks.filter(flippedBlock => flippedBlock.classList.contains("is-flipped"))
if (allFlippedBlocks.length == 2){
stopClicking()
checkMatchedBlock(allFlippedBlocks[0],allFlippedBlocks[1])
}
}
function stopClicking() {
blocksContainer.classList.add("no-clicking")
setTimeout(() => {
blocksContainer.classList.remove("no-clicking")
},duration)
}
function checkMatchedBlock(firstBlock, secondBlock){
let triesElement = document.querySelector('.tries span')
if (firstBlock.dataset.fruits === secondBlock.dataset.fruits) {
firstBlock.classList.remove('is-flipped')
secondBlock.classList.remove('is-flipped')
firstBlock.classList.add('has-match')
secondBlock.classList.add('has-match')
}else {
triesElement.innerHTML = parseInt(triesElement.innerHTML)+1
setTimeout(() => {
firstBlock.classList.remove('is-flipped')
secondBlock.classList.remove('is-flipped')
}, duration);
}
}
function shuffle(array) {
let current = array.length;
while(current > 0) {
let temp;
let random;
random = Math.floor(Math.random() * current);
current--;
temp = array[current]
array[current] = array[random]
array[random ] = temp
}
return array
}