-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
200 lines (154 loc) · 4.33 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
$(document).ready(startApp);
var canIClickCard = true;
var firstCardClicked = null;
var secondCardClicked = null;
var totalPossibleMatches = 9;
var matchCounter = 0;
var matches = 0;
var attempts = 0;
var accuracy = 0;
var gamesPlayed = 0;
var touched = 0;
var cards = [
'images/dnp.jpg',
'images/dnp.jpg',
'images/gnp.jpg',
'images/gnp.jpg',
'images/gcnp.jpg',
'images/gcnp.jpg',
'images/gsmnp.jpg',
'images/gsmnp.jpg',
'images/jtnp.jpg',
'images/jtnp.jpg',
'images/rmnp.jpg',
'images/rmnp.jpg',
'images/ynp.jpg',
'images/ynp.jpg',
'images/ysnp.jpg',
'images/ysnp.jpg',
'images/znp.jpg',
'images/znp.jpg',
];
function startApp(){
createGameBoardCards();
applyEventHandlers();
displaytIntroModal();
}
function applyEventHandlers(){
$(".card").click(chooseCard);
$("#button").click(resetButtonClick);
}
function displaytIntroModal(){
$('#intro_modal').modal('show');
}
function displaytWinModal(){
$('#win_modal').modal('show');
}
//Randomizes cards on the game board
function randomizeCards(){
cards.sort(function(a, b){return 0.5 - Math.random()});
}
//Dynamically creates the cards on the game board
function createGameBoardCards(){
randomizeCards();
for(var cardIndex=0; cardIndex < cards.length; cardIndex++){
var card = $('<div>').addClass('card');
var front = $('<div>').addClass('card-front');
var frontImage = $('<img>', {
class: 'image-size',
src: cards[cardIndex],
});
var back = $('<div>').addClass('card-back');
var backImage = $('<img>', {
class: 'background-image-size',
src: 'images/npslogo.png'
});
front.append(frontImage);
card.append(front);
back.append(backImage);
card.append(back);
$('.game-area-container').append(card);
}
}
function chooseCard(){
touched++;
if(canIClickCard === false){
return;
}
//Prevents same card from being clicked twice. If card clicked has class of hide-card, exit function
if($(event.currentTarget).find(".card-back").hasClass("hide-card")){
return;
}
$(event.currentTarget).find(".card-back").addClass("hide-card");
if(firstCardClicked === null){
firstCardClicked = event.currentTarget;
return;
}
else{
secondCardClicked = event.currentTarget;
attempts++;
var firstCardImageSource = $(firstCardClicked).find('img').attr('src');
var secondCardImageSource = $(secondCardClicked).find('img').attr('src');
if(firstCardImageSource === secondCardImageSource){
matchCounter++;
matches++;
firstCardClicked = null;
secondCardClicked = null;
displayStats();
if(matchCounter === totalPossibleMatches){
displayStats();
displaytWinModal();
}
else{
return;
}
}
else{
//If cards don't match, wait 1 second and flip cards back
canIClickCard = false;
setTimeout(flipCardsBack, 1000 );
displayStats();
return;
}
}
}
//Displays the back side of the card and resets first card and second card to null
function flipCardsBack(){
$(firstCardClicked).find(".card-back").removeClass("hide-card");
$(secondCardClicked).find(".card-back").removeClass("hide-card");
firstCardClicked = null;
secondCardClicked = null;
canIClickCard = true;
}
function displayStats(){
$("#games-played-value").text(gamesPlayed);
$("#attempts-value").text(attempts);
$("#accuracy-value").text(calculateGameAccuracy());
}
function calculateGameAccuracy(){
if(attempts === 0){
return 0;
}
accuracy = Math.round((matches/attempts) * 100);
return accuracy + "%";
}
function resetStats(){
if(touched > 0){
gamesPlayed++
}
accuracy = 0;
matches = 0;
attempts = 0;
matchCounter = 0;
firstCardClicked = null;
secondCardClicked = null;
touched = null;
displayStats();
}
function resetButtonClick(){
resetStats();
displayStats();
$('.game-area-container').empty();
createGameBoardCards();
applyEventHandlers();
}