-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
209 lines (176 loc) · 6.15 KB
/
Copy pathscript.js
File metadata and controls
209 lines (176 loc) · 6.15 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
const gameContainer = document.getElementById("game");
const newGameBtn = document.querySelector("#newGame");
const resetBtn = document.querySelector("#reset");
const scoreKeeper = document.querySelector("#score");
let score;
const hiScore = document.querySelector("#highScore");
//If no high score exists create one at 20.
if(!localStorage.highScore) {
localStorage.setItem("highScore", 20);
}
//Display hiscore
hiScore.innerText = "High Score: " + localStorage.highScore;
const COLORS = [
"red",
"blue",
"green",
"orange",
"purple",
"red",
"blue",
"green",
"orange",
"purple"
];
// here is a helper function to shuffle an array
// it returns the same array with values shuffled
// it is based on an algorithm called Fisher Yates if you want ot research more
function shuffle(array) {
let counter = array.length;
// While there are elements in the array
while (counter > 0) {
// Pick a random index
let index = Math.floor(Math.random() * counter);
// Decrease counter by 1
counter--;
// And swap the last element with it
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
let shuffledColors = shuffle(COLORS);
// this function loops over the array of colors
// it creates a new div and gives it a class with the value of the color
// it also adds an event listener for a click for each card
function createDivsForColors(colorArray) {
for (let color of colorArray) {
// create a new div
const newDiv = document.createElement("div");
// give it a class attribute for the value we are looping over
newDiv.classList.add(color);
// Add new data attribute to see which cards have been matched
newDiv.setAttribute("data-unmatched", "true");
// Add new data attribute to tell cards from main div
newDiv.setAttribute("data-card", "true");
// call a function handleCardClick when a div is clicked on
newDiv.addEventListener("click", handleCardClick);
// append the div to the element with an id of game
gameContainer.append(newDiv);
}
}
// TODO: Implement this function!
// Variable to keep track of the visible cards
let visibleCards = 0;
// Variable to keep track of the color of the cards on screen
let cardArray = [];
// Variable to keep track of previous card clicked
let previousCard;
// Variable to stop rapid clicking while waiting
let wait = false;
function handleCardClick(event) {
// you can use event.target to see which element was clicked
let currentCard = event.target;
//Should only be able to change at most two cards at a time.
if(visibleCards < 2 && (previousCard != currentCard)) {
//Change background color to be the color of the class
event.target.style.backgroundColor = currentCard.className;
//Store className in an array to see if they match
cardArray[visibleCards] = currentCard.className;
visibleCards++;
}
// Check to see if two cards have been clicked
if(visibleCards == 2 && !wait) {
wait = true;
score++;
scoreKeeper.innerText = "Score: " + score;
// If the cards do not match, keep them visible for 1 second then reset.
if(cardArray[0] != cardArray[1]) {
// Loop over all unmatched cards and set their background color to blank, after 1 second
setTimeout(function() {
let cards = document.querySelectorAll('[data-unmatched="true"]');
for(let card of cards) {
card.style.backgroundColor = "";
}
// Set variables back to zero, inside here so we can't click more cards during the 1 second timer
// I think that by putting the wait variable in these if statements and setting it to true as soon as the parent if goes off
// it will force it to be able to go through this code block only 1 time until either the setTimeout time goes past
// or in the case of a match it will be instant that we can click stuff again
visibleCards = 0;
cardArray = [];
previousCard = '';
wait = false;
}, 1000)
}
else if (cardArray[0] === cardArray[1]){ //If the cards do match keep them visible and disable the event handlers on them
// Also need to set unmatched to false
currentCard.dataset.unmatched = false;
previousCard.dataset.unmatched = false;
// reset visibleCards here so that we avoid the timer bug
visibleCards = 0;
// Call to another function to remove the listeners because I don't know what removing them while they are in the function will do.
removeListener(previousCard, currentCard);
// reset card array
cardArray = [];
previousCard = '';
wait = false;
}
}
if(visibleCards == 1) {
//Only store the previous card if we have already clicked a card once. Do not overwrite everytime we click, that causes problems
previousCard = currentCard;
}
//If game is over log the scores
let gameOver = true;
let cards = document.querySelectorAll('[data-card="true"]');
for(let card of cards) {
if(card.dataset.unmatched === 'true') {
gameOver = false;
}
}
if(gameOver) {
logScores();
}
}
function removeListener(card1, card2) {
card1.removeEventListener("click", handleCardClick);
card2.removeEventListener("click", handleCardClick);
}
// when the DOM loads
// create new listener for new game button
newGameBtn.addEventListener("click", newGame);
function newGame() {
// Clear old divs
let cards = document.querySelectorAll('[data-card="true"]');
for(let card of cards) {
card.remove();
}
// Reshuffle
shuffledColors = shuffle(COLORS)
//Create new divs
createDivsForColors(shuffledColors)
//Set score to 0
score = 0;
scoreKeeper.innerText = "Score: " + score;
}
resetBtn.addEventListener("click", reset);
function reset() {
let gameOver = true;
let cards = document.querySelectorAll('[data-card="true"]');
for(let card of cards) {
if(card.dataset.unmatched === 'true') {
gameOver = false;
}
}
if(gameOver) {
newGame();
}
}
function logScores() {
if(parseInt(localStorage.getItem("highScore")) > score) {
localStorage.setItem("highScore", score);
}
hiScore.innerText = "High Score: " + localStorage.getItem("highScore");
}
/* */