-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgameScript.js
More file actions
219 lines (188 loc) · 6.2 KB
/
gameScript.js
File metadata and controls
219 lines (188 loc) · 6.2 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
const screenWidth = window.innerWidth;
const screenHeight = window.innerHeight;
const cardWidth = 154;
let currentNum = 1;
let playGame = false;
const cardList = document.querySelector('.cardList');
const gameButton = document.getElementById('game-button');
const gameText = document.getElementById('game-text');
const resultText = document.getElementById('result-text');
const topBar = document.querySelector('.topbar');
const resetButton = document.getElementById('reset-game');
const difficultyDiv = document.querySelector('.difficulty');
const easy = document.getElementById('easy');
const normal = document.getElementById('normal');
const hard = document.getElementById('hard');
const ultimate = document.getElementById('ultimate');
const secret = document.getElementById('secret');
let numCards;
let difficulty;
easy.addEventListener('click', function(){
gameStart('easy');
difficulty = 'easy';
});
normal.addEventListener('click', function(){
gameStart('normal');
difficulty = 'normal';
});
hard.addEventListener('click', function(){
gameStart('hard');
difficulty = 'hard';
});
ultimate.addEventListener('click', function(){
gameStart('ultimate');
difficulty = 'ultimate';
});
secret.addEventListener('click', function(){
ultimate.style.display = "block";
secret.style.color = "purple";
});
resetButton.addEventListener('click', function(){
removeAllChildNodes(cardList);
resetButton.style.display = 'none';
gameButton.disabled = true;
topBar.style.left = '10px';
playGame = false;
currentNum = 1;
switch (difficulty){
case 'easy':
gameStart('easy');
break;
case 'normal':
gameStart('normal');
break;
case 'hard':
gameStart('hard');
break;
case 'ultimate':
gameStart('ultimate');
break;
}
})
function gameStart(difficulty){
console.log('lets play!');
if (difficulty === 'easy'){
numCards = 7;
gameText.style.borderColor = 'green';
} else if (difficulty === 'normal') {
numCards = 10;
} else if (difficulty === 'hard'){
numCards = 15;
gameText.style.borderColor = 'red';
} else if (difficulty === "ultimate"){
numCards = 50;
gameText.style.borderColor = 'purple';
}
easy.remove();
normal.remove();
hard.remove();
ultimate.remove();
gameText.textContent = 'Memorize the order of the cards!';
playCards();
}
function winGame(){
if (difficulty === "ultimate"){
//the best dog!
resultText.textContent = `The codeword is BILLY`;
} else if(difficulty === "hard"){
resultText.textContent = `Wow! Restart the site and click the 'u' for the Ultimate Challenge!`;
} else {
resultText.textContent = `Congratulations! You counted to ${numCards}!`;
}
resultText.style.display = "block";
resultText.style.color = "green";
setTimeout(function(){
resultText.style.opacity = '1';
}, 100)
}
function checkCorrect(num){
num = Number(num);
console.log('num:', num);
console.log('currentNum: ',currentNum);
if (num === currentNum){
if (currentNum === numCards){
winGame();
} else {
currentNum++;
gameText.textContent = `Click the cards in order from 1 to ${numCards}! (Current Number: ${currentNum})`;
}
return true;
} else {
return false;
}
}
const addCard = value => {
let card = document.createElement('div');
card.classList.add('card');
card.innerHTML = value;
card.id = value;
let randomXPos = randomizeNumber(0, screenWidth - cardWidth);
let randomYPos = randomizeNumber(90, screenHeight - cardWidth);
card.style.left = `${randomXPos}px`;
card.style.top = `${randomYPos}px`;
cardList.appendChild(card);
card.addEventListener('click', function(){
if (playGame === true){
let isCorrect = checkCorrect(card.id);
if (isCorrect === true){
console.log('CORRECT');
card.style.backgroundColor = 'green';
card.textContent = card.id;
if (Number(card.id) != numCards) {
card.style.opacity = '0';
card.style.pointerEvents = 'none';
setTimeout(function(){
card.remove();
}, 1000);
}
} else {
console.log('NOT CORRECT');
card.style.backgroundColor = 'red';
card.textContent = card.id;
card.style.zIndex = '2';
playGame = false;
const correctCard = document.getElementById(currentNum);
correctCard.style.backgroundColor = 'gray';
correctCard.textContent = currentNum;
correctCard.style.zIndex = '3';
resetButton.style.display = 'block';
}
}
})
};
console.log('Screen Width', screenWidth);
console.log('Screen Height', screenHeight);
function playCards(){
let i = numCards
gameButton.style.display = "block";
let addCardsInterval = setInterval(() => {
addCard(i);
i--;
if (i === 0){
clearInterval(addCardsInterval);
gameButton.disabled = false;
gameButton.textContent = 'Ready!';
gameButton.addEventListener('click', function(){
gameButton.style.display = 'none';
topBar.style.left = '0';
gameText.textContent = `Click the cards in order from 1 to ${numCards}! (Current Number: ${currentNum})`;
for (let i = 0; i < cardList.children.length; i++){
let card = cardList.children[i];
card.textContent = '?';
playGame = true;
}
})
}
}, 250);
}
//"Borrowed"
function removeAllChildNodes(parent) {
while (parent.firstChild) {
parent.removeChild(parent.firstChild);
}
}
function randomizeNumber(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}