-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgame.js
More file actions
231 lines (211 loc) · 8.4 KB
/
game.js
File metadata and controls
231 lines (211 loc) · 8.4 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
'use strict';
//arrays to hold data
var animalsArray = ['imgs/animals/alligator.jpg', 'imgs/animals/bear.png', 'imgs/animals/cat.jpg', 'imgs/animals/chicken.jpg', 'imgs/animals/cow.jpg', 'imgs/animals/deer.jpg', 'imgs/animals/dog.jpg', 'imgs/animals/eagle.jpg', 'imgs/animals/elephant.jpg', 'imgs/animals/flamingo.jpg', 'imgs/animals/giraffe.jpg', 'imgs/animals/gorilla.jpg', 'imgs/animals/hippo.jpg', 'imgs/animals/horse.jpg', 'imgs/animals/lion.jpg', 'imgs/animals/lizard.jpg', 'imgs/animals/mouse.jpg', 'imgs/animals/parrot.jpg', 'imgs/animals/pig.jpg', 'imgs/animals/sheep.jpg', 'imgs/animals/sloth.jpg', 'imgs/animals/snake.jpg', 'imgs/animals/tiger.jpg', 'imgs/animals/turtle.jpg'];
var cardsArray = ['imgs/cards/10clubs.jpg', 'imgs/cards/10hearts.png', 'imgs/cards/acehearts.jpg', 'imgs/cards/acespades.jpg', 'imgs/cards/jackdiamonds.jpg', 'imgs/cards/jackspades.jpg', 'imgs/cards/joker1.jpg', 'imgs/cards/joker2.png', 'imgs/cards/kinghearts.jpg', 'imgs/cards/kingspades.jpg', 'imgs/cards/queenhearts.jpg', 'imgs/cards/queenspades.jpg', 'imgs/cards/10spades.png', 'imgs/cards/10diamonds.png', 'imgs/cards/jackhearts.png', 'imgs/cards/jackclubs.png', 'imgs/cards/queendiamonds.jpg', 'imgs/cards/queenclubs.jpg', 'imgs/cards/kingdiamonds.png', 'imgs/cards/kingclubs.png', 'imgs/cards/aceclubs.png', 'imgs/cards/acediamonds.png', 'imgs/cards/joker3.jpg', 'imgs/cards/joker4.jpg' ];
var moviesArray = ['imgs/posters/alien.jpg', 'imgs/posters/avengers.jpg', 'imgs/posters/backtothefuture.jpg', 'imgs/posters/bladerunner.jpg', 'imgs/posters/darkknight.jpg', 'imgs/posters/deadpool.jpg', 'imgs/posters/findingnemo.jpg', 'imgs/posters/forceawakens.jpg', 'imgs/posters/ghostbuster.jpg', 'imgs/posters/greatoutdoors.jpg', 'imgs/posters/hackers.jpg', 'imgs/posters/hotfuzz.jpg', 'imgs/posters/indianajones.jpg', 'imgs/posters/jaws.jpg', 'imgs/posters/johnwick.jpg', 'imgs/posters/jurassicpark.jpg', 'imgs/posters/martian.jpg', 'imgs/posters/meangirls.jpg', 'imgs/posters/notebook.jpg', 'imgs/posters/rocky.jpg', 'imgs/posters/silenceofthelambs.jpg', 'imgs/posters/startrek.jpg', 'imgs/posters/starwars.jpg'];
var cardDictionary = {
animals: animalsArray,
cards: cardsArray,
movies: moviesArray,
};
// Variables to capture game level
var level;
var level1;
var key1;
var imageArray = [];
var altImage = 'imgs/back.png';
// counts correct pairs
var countCorrect = 0;
// counts total clicks
var countTotal = 0;
// selects game table
var picSelector;
// creates array which clicked choices will be stored
var choiceArray = [];
// creates array to prevent the same image from being chosen
var choiceIndexArray = [];
// creates array to house correctly answered pairs
var correctPairs = [];
// allows for flipping of cards
var firstChoice = null;
// allows for disabling clicking during timeOut function
var selecting = false;
// holds the current player object
var playerObject;
var shuffledArray;
// function to parse from local storage and grab level
function getGameParam(){
level = JSON.parse(localStorage.getItem('current_player'));
level1 = level.level;
key1 = level.cardset;
};
function shuffleArray(array){
for (var i = array.length - 1; i > 0; i--){
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
};
return array;
};
function grabItems(array, difficulty) {
var itemsNeeded = 0;
if (difficulty === 'easy') {
itemsNeeded = 3;
} else if (difficulty === 'medium') {
itemsNeeded = 6;
} else {
itemsNeeded = 12;
};
for (var i = 0; i < itemsNeeded; i++) {
imageArray.push(array[i]);
imageArray.push(array[i]);
};
};
//function to flip cards and allow for setTimout
function flipCards(event, faceDown) {
selecting = false;
event.target.setAttribute('src', faceDown);
event.target.setAttribute('class', 'flipback');
firstChoice.setAttribute('src', faceDown);
firstChoice.setAttribute('class', 'flipback');
firstChoice = null;
event.target.disable = false;
};
function pushCctPairs() {
correctPairs.push(choiceArray[0]);
choiceArray = [];
choiceIndexArray = [];
countCorrect += 1;
firstChoice = null;
};
function finished(){
playerObject = updatePlayerInfo(countTotal);
pullPushHighScoreArray(playerObject);
alert('Congratulations! You solved the game in ' + countTotal + ' clicks!');
window.location = 'about-info.html';
};
function choiceNotMatching(event, faceDown) {
selecting = true;
setTimeout(function(){flipCards(event, faceDown);}, 1000);
choiceArray = [];
choiceIndexArray = [];
};
function pullPushHighScoreArray(object) {
var jsonArray;
var updatedArray;
var newArray;
if (localStorage.getItem('high_score_array')) {
jsonArray = JSON.parse(localStorage.getItem('high_score_array'));
jsonArray.push(object);
updatedArray = JSON.stringify(jsonArray);
localStorage.setItem('high_score_array', updatedArray);
} else {
newArray = JSON.stringify([object]);
localStorage.setItem('high_score_array', newArray);
};
};
function updatePlayerInfo(countTotal) {
var playerObject;
var returnPlayer;
playerObject = JSON.parse(localStorage.getItem('current_player'));
playerObject.score = countTotal;
returnPlayer = JSON.stringify(playerObject);
localStorage.setItem('current_player', returnPlayer);
return playerObject;
};
function buildTable(imageArray){
var iA = 0;
var table = document.getElementById('game_table');
var tRow;
var tData;
var image;
for (var r = 0; r < (imageArray.length / 6); r++){
tRow = document.createElement('tr');
for (var c = 0; c < 6; c++){
tData = document.createElement('td');
image = document.createElement('img');
image.setAttribute('faceup', imageArray[iA]);
image.setAttribute('facedown', altImage);
image.setAttribute('src', altImage);
image.setAttribute('index', iA);
iA += 1;
tData.appendChild(image);
tRow.appendChild(tData);
};
table.appendChild(tRow);
};
};
function clickHandler(event){
//getting attributes the clicked image
var choiceIndex = event.target.getAttribute('index');
var faceUp = event.target.getAttribute('faceup');
var faceDown = event.target.getAttribute('facedown');
//stops from selecting image after 2 wrong clicks
if (selecting){
return;
};
//prevents a null image
if (faceUp === null){
return;
};
countTotal += 1;
//beginning flip of clicked photo
event.target.setAttribute('src', faceUp);
event.target.setAttribute('class', 'flipfront');
choiceIndexArray.push(choiceIndex);
//if statements made to determine what will happen upon clicking of photo
//comparing clicked images
if (choiceIndexArray[0] !== choiceIndexArray[1]) {
console.log(choiceIndexArray);
choiceArray.push(faceUp);
//comparing if clicked choice is already matched in order to
//prevent the same correct choices to be clicked again
if (correctPairs.indexOf(choiceArray[0]) === -1 && correctPairs.indexOf(choiceArray[1]) === -1) {
//determining the first clicked choice and enabling it to switch
//back if the second choice is wrong
if (!firstChoice){
firstChoice = event.target;
};
//script after two choices are made
if(choiceArray.length === 2){
//script if choices are MATCHING
if(choiceArray[0] === choiceArray[1]){
pushCctPairs();
//script after all correct choices are made
if (countCorrect === (imageArray.length / 2)) {
//timeout to allow seeing the final choice
setTimeout(finished, 500);
}
//what will happen if choices are NOT MATCHING
} else {
selecting = true;
choiceNotMatching(event, faceDown);
};
};
//else if choice has already been matched
} else {
alert('Please make another choice.');
choiceArray = [];
choiceIndexArray = [];
firstChoice.setAttribute('src', faceDown);
firstChoice.setAttribute('class', 'flipback');
firstChoice = null;
};
//else if same card is picked twice
} else {
selecting = false;
setTimeout(function(){flipCards(event, faceDown);}, 0),
alert('please click a separate card, start over');
choiceArray = [];
choiceIndexArray = [];
};
};
//main
getGameParam();
shuffledArray = shuffleArray(cardDictionary[key1]);
grabItems(shuffledArray, level1);
imageArray = shuffleArray(imageArray);
buildTable(imageArray);
picSelector = document.getElementById('game_table');
picSelector.addEventListener('click', clickHandler);