forked from Passive-Coder/Memory-game
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (129 loc) Β· 4.05 KB
/
index.js
File metadata and controls
136 lines (129 loc) Β· 4.05 KB
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
var moves;
var misses;
var activeCards = 0;
var content = ["π", "π", "π", "π", "π", "πβπ©", "π", "π","π", "π", "π", "π", "π", "πβπ©", "π", "π"];
var flippedCards;
var activatedCards;
var countdown = null;
var round = 1;
var totalPoints = 0;
var levelPoints;
function displayResults(){
var labels = $(".footer-style").find("h1");
$("#moves").html(`Moves: ${moves}`);
$("#round").html(`Round: ${round}`)
$("#misses").html(`Misses: ${misses}`);
}
function initialize(){
$("div.tile.hide").removeClass("hide");
$("div.card.action").removeClass("action");
$("div.stats").addClass("hide");
round++;
levelPoints = 0;
activeCards = 0;
moves = 0;
misses = 0;
var indices = [];
flippedCards = [];
activatedCards = [];
var cardBacks = $(".back");
var length = content.length;
var random;
for(var i = 0;i < length; i++){
indices.push(i);
}
for(var i = 0; i < content.length; i++){
random = Math.floor(Math.random()*indices.length);
$(cardBacks[i]).html(content[indices[random]]);
indices.splice(random,1);
}
displayResults();
}
$(document).ready(function(){
round = 0;
initialize();
});
function checkCards(){
if($(flippedCards[0]).find(".back").html() === $(flippedCards[1]).find(".back").html()){
$(flippedCards[0]).addClass("hide");
$(flippedCards[1]).addClass("hide");
new Audio("./sounds/correct.mp3").play();
levelPoints += 10;
totalPoints += 10;
var classes = $("div.tile.hide");
if(classes.length === 16){
$("#points").html(`Points: ${levelPoints}`);
$("#result-title").html(`Round ${round} Results:`);
var accuracy = (moves-misses)/(moves)*100;
$("#accuracy").html(`Accuracy: ${Math.round(accuracy)}%`);
$("div.stats").removeClass("hide");
$("div.stats").css("opacity", "1");
}
}
else{
for(var i = 0; i < flippedCards.length; i++){
if(activatedCards.includes(flippedCards[i].find(".card").attr("id"))){
misses++;
levelPoints -= 5;
totalPoints -= 5;
break;
}
}
for(var i = 0; i < flippedCards.length; i++){
if(!activatedCards.includes(flippedCards[i].find(".card").attr("id"))){
activatedCards.push(flippedCards[i].find(".card").attr("id"));
}
}
$(flippedCards[0]).find(".card").removeClass("action");
$(flippedCards[1]).find(".card").removeClass("action");
}
$("#total").html(`Total Points: ${totalPoints}`);
moves++;
displayResults();
activeCards = 0;
flippedCards = [];
countdown = null;
}
function handleClick(){
if(activeCards < 2){
if(!($(this).find(".card").hasClass("action"))){
activeCards++;
flippedCards.push($(this));
$(this).find(".card").toggleClass("action");
new Audio("./sounds/card-flip.mp3").play();
}
}
if(activeCards === 2 && countdown !== null){
var check = 1;
for(var i = 0; i < flippedCards.length; i++){
if(flippedCards[i].is($(this))){
check = 0;
break;
}
}
if(check){
clearTimeout(countdown);
countdown = null;
checkCards();
activeCards++;
flippedCards.push($(this));
$(this).find(".card").toggleClass("action");
new Audio("./sounds/card-flip.mp3").play();
}
}
else if(activeCards === 2){
for(var i = 0; i < flippedCards.length; i++){
flippedCards[i].off("click");
}
countdown = setTimeout(checkCards,2000);
for(var i = 0; i < flippedCards.length; i++){
flippedCards[i].on("click", handleClick);
}
}
}
$(".tile").on("click", handleClick);
$("button").on("click", function(){
new Audio("./sounds/button-click.mp3").play();
initialize();
}
);