-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
152 lines (110 loc) · 3.96 KB
/
scripts.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
/* JavaScript code for memory card game */
// card tracker
cardsClicked = 0;
function cardClicked(what) {
//make sure the card hasn't been removed
if (!what.classList.contains("removed")) {
if (what.classList.contains("clicked")) {
//it's already clicked, act appropriately
what.classList.remove("clicked");
cardsClicked--;
} else {
//it's not already clicked
what.classList.add("clicked");
cardsClicked++;
if (cardsClicked == 2) {
//compare card values
cardCompare();
}
}
}
}
function cardCompare() {
clickedCards = document.querySelectorAll(".clicked"); //collection of clicked cards
//first clicked element will be clickedCards[0]
//second clicked element will be clickedCards[1]
matched = false; //track if the cards matched
if (clickedCards[0].classList.contains("pic1") && clickedCards[1].classList.contains("pic1")) {
matched = true; //they matched pic 1
} else if (clickedCards[0].classList.contains("pic2") && clickedCards[1].classList.contains("pic2")) {
matched = true; //they matched pic 2
} else if (clickedCards[0].classList.contains("pic3") && clickedCards[1].classList.contains("pic3")) {
matched = true; //they matched pic 3
} else if (clickedCards[0].classList.contains("pic4") && clickedCards[1].classList.contains("pic4")) {
matched = true; //they matched pic 4
}
else if (clickedCards[0].classList.contains("pic5") && clickedCards[1].classList.contains("pic5")) {
matched = true; //they matched pic 5
}
else if (clickedCards[0].classList.contains("pic6") && clickedCards[1].classList.contains("pic6")) {
matched = true; //they matched pic 6
}
else if (clickedCards[0].classList.contains("pic7") && clickedCards[1].classList.contains("pic7")) {
matched = true; //they matched pic 7
}
else if (clickedCards[0].classList.contains("pic8") && clickedCards[1].classList.contains("pic8")) {
matched = true; //they matched pic 8
}
else if (clickedCards[0].classList.contains("pic9") && clickedCards[1].classList.contains("pic9")) {
matched = true; //they matched pic 9
}
else if (clickedCards[0].classList.contains("pic10") && clickedCards[1].classList.contains("pic10")) {
matched = true; //they matched pic 10
}
if (matched) {
//hide cards
removeCards(clickedCards[0], clickedCards[1]);
} else {
//unflip cards
unflipCards(clickedCards[0], clickedCards[1]);
}
}
function removeCards(cardA, cardB) {
pause = setTimeout(function() {
cardA.classList.remove("clicked");
cardB.classList.remove("clicked");
cardA.classList.add("removed");
cardB.classList.add("removed");
cardsClicked = 0;
checkWinning();
}, 500);
}
function unflipCards(cardA, cardB) {
pause = setTimeout(function() {
cardA.classList.remove("clicked");
cardB.classList.remove("clicked");
cardsClicked = 0;
}, 500);
}
function checkWinning() {
remainingCards = document.querySelectorAll(".card"); //get all cards
//cycle through all cards and check for matched class
for (c = 0; c < remainingCards.length; c++) {
if ( !remainingCards[c].classList.contains("removed") ) {
return;
}
}
document.getElementById("MainTable").innerHTML = "You Won!";
}
function shuffleCards() {
table = document.querySelector("#MainTable");
cardCount = table.children.length;
cardToMove = table.children[0];
table.appendChild( cardToMove);
for (c = 0; c < cardCount; c++) {
randomCard = Math.floor( Math.random() * cardCount );
cardToMove = table.children[randomCard];
table.appendChild( cardToMove );
}
}
// stuff to do when page loads
window.onload = function() {
shuffleCards();
cardList = document.querySelectorAll(".card"); // collection of cards
cardCount = cardList.length; // how many cards are on the table
for (c = 0; c < cardCount; c++) {
cardList[c].onclick = function() {
cardClicked(this);
}
}
}