-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
154 lines (145 loc) · 4.48 KB
/
script.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
const scores = JSON.parse(localStorage.getItem('Scores')) || []; // scores are pushed to this array which is later stored into local storage
let player = '';
const cards = [
//array of cards
'card1',
'card2',
'card3',
'card4',
'card5',
'card6',
'card1',
'card2',
'card3',
'card4',
'card5',
'card6'
];
//Fisher-Yates algorithm to shuffle cards
function shuffle(array) {
let counter = array.length;
while (counter > 0) {
let index = Math.floor(Math.random() * counter);
counter--;
let temp = array[counter];
array[counter] = array[index];
array[index] = temp;
}
return array;
}
function hideStart() {
const start = document.getElementById('flex-box');
start.setAttribute('class', 'hidden');
}
let shuffledCards = shuffle(cards); //this variable holds freshly shuffled cards
let start; // global scope initiated for timer's setInterval function
//button to start game
const startButton = document.getElementById('start-button');
// add click listener to game board
startButton.addEventListener('click', function(e) {
e.preventDefault();
hideStart();
displayScores(); //calls function to display timer and score
displayCards(shuffledCards); //calls function to display shuffled cards
start = setInterval(countSecs, 1000);
player = document.querySelector('#name').value;
});
function displayScores() {
const gameBoard = document.getElementById('game');
const time = document.createElement('div'); // creates and build divs for timer and score counter
time.classList.add('time');
time.innerText = 'time';
const timePrint = document.createElement('div');
timePrint.setAttribute('id', 'timePrint');
timePrint.innerText = 0;
const attempts = document.createElement('div');
attempts.classList.add('score');
attempts.innerText = 'score';
const scorePrint = document.createElement('div');
scorePrint.setAttribute('id', 'scorePrint');
gameBoard.append(time); //adds freshly created divs to HTML
time.append(timePrint);
gameBoard.append(attempts);
attempts.append(scorePrint);
countSecs(timePrint);
}
function displayCards(shuffledCards) {
const gameBoard = document.getElementById('game');
for (let cardX of shuffledCards) {
const cardContainer = document.createElement('div'); //card container
cardContainer.classList.add('card-container');
const card = document.createElement('div'); //card holding front and back
card.classList.add('card');
const front = document.createElement('div'); //front of card
front.classList.add('front');
const back = document.createElement('div'); //back of card
back.classList.add(cardX);
gameBoard.append(cardContainer); //adds cards to HTML
cardContainer.append(card);
card.appendChild(front);
card.appendChild(back);
}
gameBoard.addEventListener('click', handleCardClick); //listens to click to handle card
}
let sec = 0;
function countSecs() {
//counts seconds when setInterval is initiated
sec++;
const timePrint2 = document.querySelector('#timePrint');
timePrint2.innerText = sec;
}
let tries = 0;
function countTries() {
//counts tries
tries++;
const scorePrint = document.querySelector('#scorePrint');
scorePrint.innerText = tries;
}
let cardsFlipped = 0;
let cardsMatched = 0;
let card1;
let card2;
function handleCardClick(e) {
// flips card that was clicked
countTries();
let clickedCard = e.target;
if (clickedCard.className === 'front') {
//this makes sure clicking your own card won't count as the second flipped card
cardsFlipped++;
if (cardsFlipped === 1) {
//flips first card
card1 = clickedCard;
clickedCard.parentElement.classList.add('flip');
}
if (cardsFlipped === 2) {
//flips second card
card2 = clickedCard;
clickedCard.parentElement.classList.add('flip');
checkIfMatch(card1, card2); // checks if first and second card match
}
}
}
function checkIfMatch(card1, card2) {
if (card1.nextElementSibling.className === card2.nextElementSibling.className) {
cardsMatched++; //counts number of matches (helpful to determine when to stop timer)
cardsFlipped = 0;
// alert("it's a match");
if (cardsMatched === cards.length / 2) {
clearInterval(start);
saveScore();
}
} else {
setTimeout(function() {
card1.parentElement.classList.remove('flip'); //flips cards back if they are not a match
card2.parentElement.classList.remove('flip');
cardsFlipped = 0;
}, 1000);
}
}
function saveScore() {
// saves scores to local Storage
scores.push({ playerName: player, score: tries });
localStorage.setItem('Scores', JSON.stringify(scores));
alert(JSON.stringify(scores));
location.reload();
}