-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
127 lines (114 loc) · 2.82 KB
/
app.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
document.addEventListener('DOMContentLoaded', () => {
// baralho
const cardArray = [
{
name: 'cloud',
img: 'images/cloud.jpg'
},
{
name: 'aerith',
img: 'images/aerith.jpg'
},
{
name: 'tifa',
img: 'images/tifa.jpg'
},
{
name: 'barret',
img: 'images/barret.jpg'
},
{
name: 'red13',
img: 'images/red13.jpg'
},
{
name: 'sephiroth',
img: 'images/sephiroth.jpg'
},
{
name: 'cloud',
img: 'images/cloud.jpg'
},
{
name: 'aerith',
img: 'images/aerith.jpg'
},
{
name: 'tifa',
img: 'images/tifa.jpg'
},
{
name: 'barret',
img: 'images/barret.jpg'
},
{
name: 'red13',
img: 'images/red13.jpg'
},
{
name: 'sephiroth',
img: 'images/sephiroth.jpg'
},
]
cardArray.sort(() => 0.5 - Math.random())
const grid = document.querySelector('.grid')
const resultDisplay = document.querySelector('#result')
const newGame = document.querySelector('#retry')
var cardsChosen = []
var cardsChosenId = []
var cardsWon = []
// criando board
function createBoard() {
for (let i = 0; i < cardArray.length; i++) {
var card = document.createElement('img')
card.setAttribute('src', 'images/cover.jpg')
card.classList.add('back')
card.setAttribute('data-id', i)
card.addEventListener('click', flipCard)
grid.appendChild(card)
}
}
// verificando se são iguais
function checkForMatch() {
var cards = document.querySelectorAll('img')
const optionOneId = cardsChosenId[0]
const optionTwoId = cardsChosenId[1]
if (cardsChosen[0] === cardsChosen[1]) {
cards[optionOneId].setAttribute('src', 'images/found.jpg')
cards[optionTwoId].setAttribute('src', 'images/found.jpg')
cards[optionOneId].removeEventListener('click', flipCard)
cards[optionTwoId].removeEventListener('click', flipCard)
cardsWon.push(cardsChosen) // adicionando pro contador
}
else {
cards[optionOneId].setAttribute('src', 'images/cover.jpg')
cards[optionOneId].classList.add('back')
cards[optionTwoId].setAttribute('src', 'images/cover.jpg')
cards[optionTwoId].classList.add('back')
// faz card voltar a ser jogável:
cards[optionOneId].addEventListener('click', flipCard)
cards[optionTwoId].addEventListener('click', flipCard)
}
cardsChosen = []
cardsChosenId = []
resultDisplay.textContent = cardsWon.length
if (cardsWon.length === cardArray.length/2) {
resultDisplay.textContent = "Congrats! You found them all!"
newGame.classList.add('new-game')
}
}
// virando o card
function flipCard() {
this.classList.remove('back')
this.classList.add('selected')
this.removeEventListener('click', flipCard)
var cardId = this.getAttribute('data-id')
cardsChosen.push(cardArray[cardId].name)
cardsChosenId.push(cardId)
this.setAttribute('src', cardArray[cardId].img)
if (cardsChosen.length === 2) {
setTimeout(checkForMatch, 500) // miliseconds
}
}
createBoard()
})