-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
111 lines (89 loc) · 2.84 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
let game = {
lockMode: false,
firstCard: null,
secondCard: null,
cards_init : ['html',
'css3',
'unity',
'python',
'github',
'bootstrap',
'mysql',
'javascript'],
cardsStart : null,
setCard: function(id){
let card = this.card.filter(card => card.id === id)[0];
if(card.flip || this.lockMode){
return false;
}
if (!this.firstCard){
this.firstCard = card;
this.firstCard.flip = true
return true;
} else {
this.secondCard = card;
this.secondCard.flip = true
this.lockMode = true;
return true;
}
},
checkMatch: function(){
if(!this.firstCard || !this.secondCard){
return false;
}
return this.firstCard.icon === this.secondCard.icon
},
clearCards: function(){
this.firstCard = null;
this.secondCard = null;
this.lockMode = false;
},
unflipCards: function(){
this.firstCard.flip = false;
this.secondCard.flip = false;
this.clearCards();
},
checkGameOver: function(){
for(let card of this.cardsStart){
console.log(card[0].flip)
}
return this.cardsStart.filter(card => !card[0].flip).length == 0
},
createCards: function() {
this.cardsStart = []
this.cards_init.forEach((card) => {
this.cardsStart.push(this.createFrontAndBack(card));
})
this.card = this.cardsStart.flatMap(pair => pair);
this.shuffleCards(this.card);
return this.card
},
createId: function (card) {
return card + parseInt(Math.random() * 1000);
},
createFrontAndBack: function(card) {
return [{
id: this.createId(card),
icon: card,
flip: false,
}, {
id: this.createId(card),
icon: card,
flip: false,
}]
},
shuffleCards: function (cards){
// Pegar o último indice do array de cartas
let currentIndex = this.card.length;
// Variável para pegar o primeiro indice do array
let randomIndex = 0;
// Ir trocando indice do array até que o último indice seja 0
while(currentIndex != 0){
// Colocando o último indice dentro da variável randomIndex
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// Ir trocando a última carta pela primeira para embaralhar os indices
[this.card[randomIndex], this.card[currentIndex]] = [this.card[currentIndex], this.card[randomIndex]];
}
}
}