-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
152 lines (145 loc) · 3.77 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
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
// var container = new Vue({
// el: "#app",
// data: {
// header: "Welcome to Vue JS!"
// }
// });
var secretPairs = new Vue({
el: "#app",
data() {
return {
tacos: "I like Al pastor tacos",
images: [
{
url: "https://octodex.github.com/images/steroidtocat.png",
id: 0
},
{
url: "https://octodex.github.com/images/megacat-2.png",
id: 1
},
{
url: "https://octodex.github.com/images/dodgetocat_v2.png",
id: 2
},
{
url: "https://octodex.github.com/images/mcefeeline.jpg",
id: 3
},
{
url: "https://octodex.github.com/images/ironcat.jpg",
id: 4
},
{
url: "https://octodex.github.com/images/gracehoppertocat.jpg",
id: 5
},
{
url: "https://octodex.github.com/images/spidertocat.png",
id: 6
},
{
url: "https://octodex.github.com/images/octocat-de-los-muertos.jpg",
id: 7
},
{
url: "https://octodex.github.com/images/saritocat.png",
id: 8
},
{
url: "https://octodex.github.com/images/plumber.jpg",
id: 9
},
{
url: "https://octodex.github.com/images/linktocat.jpg",
id: 10
},
{
url: "https://octodex.github.com/images/kimonotocat.png",
id: 11
}
],
finalArray: [],
selectedIndexes: [],
cardIndexes: [],
matchedIndexes: [],
timer: "00:00:00",
moves: 0,
endGame: false
};
},
mounted() {
this.images = this.images.slice(0).concat(this.images);
this.finalArray = this.randomizeCards(this.images);
console.log(this.finalArray);
var counter = 0;
String.prototype.toHHMMSS = function() {
var sec_num = parseInt(this, 10);
var hours = Math.floor(sec_num / 3600);
var minutes = Math.floor((sec_num - hours * 3600) / 60);
var seconds = sec_num - hours * 3600 - minutes * 60;
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
if (seconds < 10) {
seconds = "0" + seconds;
}
return hours + ":" + minutes + ":" + seconds;
};
var timer = setInterval(() => {
if (this.endGame) {
clearInterval(timer);
}
counter++;
this.timer = counter.toString().toHHMMSS();
}, 1000);
},
methods: {
flip(card_id, index) {
// if (this.selectedIndexes.length > 2) {
// console.log("hello");
// return;
// }
if (this.selectedIndexes.length < 2) {
this.moves++;
this.selectedIndexes.push(index);
this.cardIndexes.push(card_id);
if (this.selectedIndexes.length === 2) {
if (this.cardIndexes[0] === this.cardIndexes[1]) {
this.matchedIndexes.push(card_id);
if (this.finalArray.length / 2 === this.matchedIndexes.length) {
this.endGame = true;
alert("Game Over");
}
this.selectedIndexes = [];
this.cardIndexes = [];
} else {
setTimeout(() => {
this.selectedIndexes = [];
this.cardIndexes = [];
}, 500);
}
}
}
},
randomizeCards(array) {
var m = array.length,
t,
i;
// While there remain elements to shuffle…
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
// return images;
}
}
});