-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
161 lines (141 loc) · 3.77 KB
/
game.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
// cardArray
const cardsArray = [
{
name: 'avi',
img: 'GP-images/avi.png',
},
{
name: 'tess',
img: 'GP-images/tess.png',
},
{
name: 'rachel',
img: 'GP-images/rachel.png',
},
{
name: 'tom',
img: 'GP-images/tom.png',
},
{
name: 'daniela',
img: 'GP-images/daniela.png',
},
{
name: 'josh',
img: 'GP-images/josh.png',
},
{
name: 'otha',
img: 'GP-images/otha.png',
},
{
name: 'kevin',
img: 'GP-images/kevin.png',
},
]
const game = document.querySelector('#game-grid')
const newGame = () => {
let counterNum = document.querySelector("#click-count")
counterNum.innerHTML = 0
game.innerHTML = ""
gameGrid.sort(() => 0.5 - Math.random())
gameGrid.forEach (student => {
const card = document.createElement('div')
card.classList.add('card')
card.dataset.name = student.name
const front = document.createElement('div')
front.classList.add('front')
const back = document.createElement('div')
back.classList.add('back')
back.style.backgroundImage = `url(${student.img})`
// card.style.backgroundImage = `url(${student.img})`
game.appendChild(card)
card.appendChild(front)
card.appendChild(back)
})
}
let refreshGame = document.querySelector("#new-game")
refreshGame.addEventListener("click", newGame)
// duplicating cards
let gameGrid = cardsArray.concat(cardsArray)
gameGrid.sort(() => 0.5 - Math.random())
let firstGuess = ''
let secondGuess = ''
let count = 0
let previousTarget = null;
gameGrid.forEach (student => {
const card = document.createElement('div')
card.classList.add('card')
card.dataset.name = student.name
const front = document.createElement('div')
front.classList.add('front')
const back = document.createElement('div')
back.classList.add('back')
back.style.backgroundImage = `url(${student.img})`
// card.style.backgroundImage = `url(${student.img})`
game.appendChild(card)
card.appendChild(front)
card.appendChild(back)
})
// add eventListener to cards
game.addEventListener('click', function(event) {
let clickCount = document.querySelector("#click-count")
let num = clickCount.innerText
let int = parseInt(num)
let clicked = event.target
if (clicked.className === 'card' ||
clicked === previousTarget ||
clicked.parentNode.classList.contains('selected')
) {
return
}
if (count < 2){
count++
if (count === 1) {
firstGuess = clicked.parentNode.dataset.name
clicked.parentNode.classList.add('selected')
} else {
secondGuess = clicked.parentNode.dataset.name
clicked.parentNode.classList.add('selected')
int++
clickCount.innerText = int
}
if (firstGuess !== '' && secondGuess !== ''){
if (firstGuess === secondGuess) {
setTimeout(match, delay)
setTimeout(resetGuesses, delay)
match();
resetGuesses();
} else {
setTimeout(resetGuesses, delay)
}
}
previousTarget = clicked;
}
});
let delay = 500;
const match = () => {
let selected = document.querySelectorAll('.selected')
selected.forEach(card => {
card.classList.add('match')
})
if (document.querySelectorAll('.match').length === 16){
gameOver(event)
let modal = document.querySelector(".gameOver")
modal.style.display = "block"
let refreshBtn = document.querySelector(".closeBtn")
refreshBtn.addEventListener('click', function(){
newGame()
modal.style.display = "none"
})
}
}
const resetGuesses = () => {
firstGuess = ''
secondGuess = ''
count = 0
let selected = document.querySelectorAll('.selected')
selected.forEach(card => {
card.classList.remove('selected')
})
}