-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch.js~
138 lines (116 loc) · 2.78 KB
/
match.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
var images = ["bunny.jpg", "cat.jpg", "chameleon.jpg", "chicks.jpg", "duckling.jpg", "piglets.jpg", "pony.jpg", "puppy.jpg"];
var sounds = ["./sounds/boing_boing.wav", "./sounds/snaps.wav", "./sounds/chameleon.wav", "./sounds/chicklets.wav", "./sounds/ducklings.wav", "./sounds/pig.m4a", "./sounds/snaps.wav", "./sounds/dog.m4a" ];
var count = images.length * 2;
var rowLength = 4;
function randColor(){
return '#'+Math.floor(Math.random()*16777215).toString(16);
}
function MatchCard(imgSrc, count){
this.image = new Image();
this.image.src = imgSrc;
this.image.dataset.card = count; //use this to match
this.selected = false;
this.order = Math.random();
}
/**
* Function to shuffle cards.
*
*/
function shuffle(a,b)
{
if(a.order < b.order)
{
return -1;
}
if(a.order > b.order)
{
return 1;
}
else return 0;
}
/*
*/
function buildDeck(imgSrc)
{
var deck = [];
var deckLength = imgSrc.length;
for(var x = 0; x < deckLength; x++)
{
deck.push(new MatchCard(imgSrc[x], x));
deck.push(new MatchCard(imgSrc[x], x));
}
deck.sort(shuffle);
return deck;
}
/*
Adds images to the board in their hidden state.
*/
var matches = []
var kidCheer;
var cardAudio;
function addImages(deck)
{
$("div").each(function(index)
{
$(this).append(deck[index].image);
});
$("img").addClass("hidden"); /*Make all images transparent*/
/*When you click on an image if it is hidden it becomes shown and vice versa*/
$("img").click( function() {
if($(this).hasClass("hidden") && !$(this).hasClass("matched"))
{
$(this).removeClass("hidden");
var cardAudSrc = sounds[$(this).data("card")].audio;
console.log(cardAudSrc);
cardAudio.attr("src", cardAudSrc); //Plays the card's audio.
matches.push($(this));
/*When two cards are up and they don't match, flip all cards over.
If the cards do match it plays a congratulation tone.
*/
if(matches.length === 2)
{
if(matches[0].data("card") === matches[1].data("card"))
{
kidCheer.play();
for(x in matches)
{
$(matches[x]).addClass("matched");
}
matches.length = 0;
}
else
{
window.setTimeout(function()
{
for(x in matches)
{
$(matches[x]).addClass("hidden");
}
matches.length = 0;
}, 500);
}
}
}
else
{
/*Flip card over and remove from set of matches*/
$(this).addClass("hidden");
var thisIndex = matches.indexOf($(this));
matches.splice(thisIndex, 1);
}
});
}
$(document).ready(function() {
kidCheer = $("audio")[0];
cardAudio = $("#cardAudio");
var deck = buildDeck(images);
for(var outer = 0; outer < count; ++outer)
{
for(var inner = 0; inner < rowLength; ++inner)
{
$("body").append("<div class='card' style='background-color:"+randColor()+";'>");
++outer;
}
}
addImages(deck);
});