forked from KiberVG/MemoryMatchingBootcampHW
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
170 lines (139 loc) Β· 5.45 KB
/
script.js
File metadata and controls
170 lines (139 loc) Β· 5.45 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
/*
Students! You will all be completing this matching card game.
Follow the directions throughout this file to slowly build out
the game's features.
*/
// These are all the symbols that the game is going to use
const symbols = ['π', 'π', 'π', 'π', 'π', 'π', 'π', 'π₯'];
// You're going to need this to display the cards on the screen (remember there should be two of each card)
let cards = [];
// These will be used when the user starts choosing cards
let firstCard = null, secondCard = null;
// You will need to lock the board to stop users from choosing cards when they choose two wrong cards
// (Don't have to worry about this too much)
let lockBoard = false;
let matchnum = 0;
/*
You must initialize the game board. You have been given a shuffleArray() function.
This function should also reset the entire game board by making sure there's no HTML inside of the game-board div.
Use the createCard() function to initialize each cardElement and add it to the gameBoard.
*/
function initGame() {
// Write your code here
document.getElementById('game-board').innerHTML = ''
document.getElementById('heading').innerHTML = 'Memory Card Matching Game'
cards.length=0
let x=0;
for (let i=0; i < 2; i++){
while (x<symbols.length){
createCard(symbols[x], x)
console.log(x)
x++
}
shuffleArray(symbols)
shuffleArray(cards)
x=0;
}
console.log(cards)
document.getElementById('restart-btn').addEventListener('click', initGame);
}
/*
The card will have the class 'card' and it would be a good idea to somehow save what its symbol is
within the element itself, since we'll need it for later and there's no easy way to get it from the arrays.
Also make sure to add the event listener with the 'flipCard' function
*/
function createCard(symbol, x) {
// Write your code here
cards.push(symbol)
const card = document.createElement('div');
card.classList.add('card');
card.addEventListener('click', () => flipCard(card, x));
document.getElementById('game-board').appendChild(card);
card.dataset.term = cards[x]
console.log(card.dataset.term)
}
/*
This function will handle all the logic for flipping the card. You can check if a variable doesn't
have a value attached to it or is null by doing if (variable === null) {} or if (variable == null) {} or if (!variable){}
If a card get's flipped, add the 'flipped' class and also display the symbol.
Also, if this is the first card you picked, then set the firstCard variable to the card you just picked.
If it's the second, then set the secondCard variable to it. Also, if that's the second card, then you
want to check for a match using the checkForMatch() function.
*/
function flipCard(card, x) {
// If the board is supposed to be locked or you picked the same card you already picked
if (lockBoard || card === firstCard) return;
// Write your code here
console.log('clicked')
card.innerHTML+= (card.dataset.term)
console.log(x)
if(firstCard==null) {
firstCard=card
firstCard.classList.add('flipped');
}
else if(secondCard==null){
secondCard=card
secondCard.classList.add('flipped')
checkForMatch();
}
}
/*
If there's a match between the first two cards that you picked, you want to take those cards out of the
game and then reset the board so that there is firstCard == null and secondCard == null.
Otherwise, you should unflip the card and continue playing normally.
*/
function checkForMatch() {
// Write your code here
if(firstCard.dataset.term===secondCard.dataset.term){
console.log('match found')
matchnum++
console.log("matchnum is" + matchnum)
disableCards();
resetBoard();
if (matchnum==8){
youWin();
}
}
else{
unflipCards();
}
}
/*
Disable both of the cards by adding the "matched" class to them. The "matched" class will add CSS
properties to make sure that they can no longer be clicked at all. Then use the resetBoard() function
to reset the firstCard, secondCard, and lockBoard variables. (That's been written for you already)
*/
function disableCards() {
// Write your code here
firstCard.classList.add('matched')
secondCard.classList.add('matched')
}
function youWin() {
let win = document.getElementById('heading')
win.innerHTML= "YOU WIN π"
}
/* --------------------- Everything under has already been done for you -------------------------- */
function unflipCards() {
// We lock the board so that the user can't touch the board while it is unflipping
lockBoard = true;
// The cards will be flipped back after 1 second and the board will be reset
// The 1 second is to give the user time to actaully see the card so they can memorize them before they unflip
setTimeout(() => {
firstCard.classList.remove('flipped');
secondCard.classList.remove('flipped');
firstCard.textContent = '';
secondCard.textContent = '';
resetBoard();
}, 1000);
}
function resetBoard() {
[firstCard, secondCard, lockBoard] = [null, null, false];
}
// Function to shuffle an array
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}
initGame();