forked from barryclark/jekyll-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
197 lines (185 loc) · 3.83 KB
/
main.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Memory Game
// © 2014 Nate Wiley
// License -- MIT
// best in full screen, works on phones/tablets (min height for game is 500px..) enjoy ;)
// Follow me on Codepen
(function () {
var Memory = {
init: function (cards) {
this.$game = $(".game");
this.$modal = $(".modal");
this.$overlay = $(".modal-overlay");
this.$restartButton = $("button.restart");
this.cardsArray = $.merge(cards, cards);
this.shuffleCards(this.cardsArray);
this.setup();
},
shuffleCards: function (cardsArray) {
this.$cards = $(this.shuffle(this.cardsArray));
},
setup: function () {
this.html = this.buildHTML();
this.$game.html(this.html);
this.$memoryCards = $(".card");
this.paused = false;
this.guess = null;
this.binding();
},
binding: function () {
this.$memoryCards.on("click", this.cardClicked);
this.$restartButton.on("click", $.proxy(this.reset, this));
},
// kinda messy but hey
cardClicked: function () {
var _ = Memory;
var $card = $(this);
if (
!_.paused &&
!$card.find(".inside").hasClass("matched") &&
!$card.find(".inside").hasClass("picked")
) {
$card.find(".inside").addClass("picked");
if (!_.guess) {
_.guess = $(this).attr("data-id");
} else if (
_.guess == $(this).attr("data-id") &&
!$(this).hasClass("picked")
) {
$(".picked").addClass("matched");
_.guess = null;
} else {
_.guess = null;
_.paused = true;
setTimeout(function () {
$(".picked").removeClass("picked");
Memory.paused = false;
}, 600);
}
if ($(".matched").length == $(".card").length) {
_.win();
}
}
},
win: function () {
this.paused = true;
setTimeout(function () {
Memory.showModal();
Memory.$game.fadeOut();
}, 1000);
},
showModal: function () {
this.$overlay.show();
this.$modal.fadeIn("slow");
},
hideModal: function () {
this.$overlay.hide();
this.$modal.hide();
},
reset: function () {
this.hideModal();
this.shuffleCards(this.cardsArray);
this.setup();
this.$game.show("slow");
},
// Fisher--Yates Algorithm -- https://bost.ocks.org/mike/shuffle/
shuffle: function (array) {
var counter = array.length,
temp,
index;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
},
buildHTML: function () {
var frag = "";
this.$cards.each(function (k, v) {
frag +=
'<div class="card" data-id="' +
v.id +
'"><div class="inside">\
<div class="front"><img src="' +
v.img +
'"\
alt="' +
v.name +
'" /></div>\
<div class="back"><img src="https://i.pinimg.com/564x/78/aa/a2/78aaa2fce3f3b882ade6dc4ebf29caa6.jpg"\
alt="Codepen" /></div></div>\
</div>';
});
return frag;
}
};
var cards = [
{
name: "1",
img:"IMG_20200406_002709.jpg",
id: 1
},
{
name: "2",
img:"IMG_20200426_004426.jpg",
id: 2
},
{
name: "3",
img:"IMG_20200501_032337.jpg",
id: 3
},
{
name: "4",
img:"IMG_20200506_063558.jpg",
id: 4
},
{
name: "5",
img:"IMG_20200515_182654.jpg",
id: 5
},
{
name: "6",
img:"IMG_20200520_160154.jpg",
id: 6
},
{
name: "7",
img:"IMG_20200530_140554.jpg",
id: 7
},
{
name: "8",
img:"IMG_20200610_030344.jpg",
id: 8
},
{
name: "9",
img:"IMG_20200612_183949.jpg",
id: 9
},
{
name: "10",
img:"IMG_20200612_190026.jpg",
id: 10
},
{
name: "11",
img:"IMG_20200619_192330.jpg",
id: 11
},
{
name: "12",
img:"received_314054643103370.jpeg",
id: 12
}
];
Memory.init(cards);
})();