-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
105 lines (66 loc) · 2.51 KB
/
script.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
const FRONT = "card_front";
const BACK = "card_back";
const CARD = "card";
const ICON = "icon";
startGame();
function startGame() {
initCards(game.createCards());
}
function initCards(cards) {
let gameBoard = document.getElementById('gameBoard');
gameBoard.innerHTML = ''
cards.forEach(card => {
let cardElement = document.createElement('div');
cardElement.id = card.id;
cardElement.classList.add(CARD);
cardElement.dataset.icon = card.icon
createCardContent(card, cardElement);
cardElement.addEventListener('click', flipCard);
gameBoard.appendChild(cardElement);
});
}
function createCardContent(card, element){
createCardFace(FRONT, card, element);
createCardFace(BACK, card, element);
}
function createCardFace(face, card, element){
let cardElementFace = document.createElement('div');
cardElementFace.classList.add(face)
if(face === FRONT) {
let iconElement = document.createElement('img');
iconElement.classList.add(ICON);
iconElement.src = "./imagens/" + card.icon + ".png";
cardElementFace.appendChild(iconElement);
} else {
cardElementFace.innerHTML = "</>";
}
element.appendChild(cardElementFace);
}
function flipCard() {
if(game.setCard(this.id)) {
this.classList.add("flip");
if(game.secondCard){
if(game.checkMatch()) {
game.clearCards();
if(game.checkGameOver()){
let gameOverLayer = document.getElementById('gameOver');
gameOverLayer.style.display = 'flex';
};
} else {
setTimeout(() => {
let firstCardView = document.getElementById(game.firstCard.id);
let secondCardView = document.getElementById(game.secondCard.id);
firstCardView.classList.remove("flip");
secondCardView.classList.remove("flip");
game.unflipCards();
}, 1000)
}
}
}
}
function restart() {
game.clearCards();
startGame();
let gameOverLayer = document.getElementById('gameOver');
gameOverLayer.style.display = 'none';
}