-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
390 lines (332 loc) · 9.68 KB
/
Copy pathscript.js
File metadata and controls
390 lines (332 loc) · 9.68 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Name:Abdul Hakim
// Date:250918
// Purpose:Typing Game
// +++++++
// Styling
// +++++++
// Create Div for titleSpace as container
var titleSpace = document.createElement('div') ;
titleSpace.id = 'titleSpace' ;
document.body.appendChild(titleSpace) ;
//Mainword
var mainWord = document.createElement('h1') ;
mainWord.id = 'mainWord' ;
mainWord.classList.add("typewriter");
mainWord.innerText = 'CHOCOLATE' ;
document.body.appendChild(mainWord) ;
//Create Div called center as container
var center = document.createElement('div');
center.id='center';
document.body.appendChild(center);
//Instruction
var instruc =document.createElement('h4');
instruc.id = 'instruc' ;
instruc.classList = 'center';
center.appendChild(instruc);
//Inputfield
var typeInput = document.createElement('input') ;
typeInput.setAttribute('type', 'text');
typeInput.classList = 'center';
typeInput.autofocus = true;
typeInput.id = 'input'
center.appendChild(typeInput) ;
//Create Div for score with ID = scoreSpace
var scoreSpace = document.createElement('div') ;
scoreSpace.id = 'scoreSpace' ;
document.body.appendChild(scoreSpace) ;
//score Number
var scoreNum = document.createElement('h5') ;
scoreNum.id = 'scoreNum' ;
scoreNum.innerText = '0' ;
scoreSpace.appendChild(scoreNum) ;
//Score Title
var scoreTitle = document.createElement('h5')
scoreTitle.id = 'scoreTitle'
scoreTitle.innerText = 'SCORE'
scoreSpace.appendChild(scoreTitle)
//Create Div for Timer with ID = TimerSpace
var timerSpace = document.createElement('div') ;
timerSpace.id = 'timerSpace' ;
document.body.appendChild(timerSpace) ;
//timer Number
var timer = document.createElement('h5') ;
timer.id = 'timer';
timer.innerText = '0' ;
timerSpace.appendChild(timer) ;
//timer Title
var timerTitle = document.createElement('h5') ;
timerTitle.id = 'timerTitle'
timerTitle.innerText = 'TIME'
timerSpace.appendChild(timerTitle)
// ++++++++
// Gameplay
// ++++++++
//once loaded run intro
window.addEventListener('load',intro)
//declaring and initialising working variables
let time = 60 ;
score = 0 ;
let isPlaying;
const wordInput = typeInput ;
const currentWord = mainWord ;
const scoreDisplay = scoreNum ;
const timeDisplay = timer ;
const message = instruc ;
const praise = [
'Good job',
'Right on',
'Well done',
'Excellent',
'Outstanding',
'Wonderful',
'You Rock!',
'You Rule!',
'Amazing',
'Easy!',
]
const promptWrong = [
'Are you sure?',
'Have another look?',
'Check again?'
]
// +++++++++++++++
// Make Password
// +++++++++++++++
//make random string : Easy
var randomStr = function(){
var length = 5
charSet ='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
retVal=''
for(var i = 0 , len = charSet.length ; i<length ; i++){
retVal += charSet.charAt(Math.floor(Math.random() * len))
}
return retVal
}
//make random string : Medium
var randomStrNum = function(){
var length = 7
charSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
retVal = ''
for(var i = 0 , len = charSet.length ; i<length ; i++){
retVal += charSet.charAt(Math.floor(Math.random() * len))
}
return retVal
}
//make random string : Hard
var randomSym = function(){
var length = 10
charSet = '~!@#$%^*()_+{}[]|:/?><ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890'
retVal = ''
for(var i =0 , len = charSet.length ; i<length ; i++){
retVal += charSet.charAt(Math.floor(Math.random()* len))
}
return retVal
}
//function Letters into array
var words = [];
for(var i = 0 ; i< 200 ; i++){
var prepStr = randomStr()
words.push(prepStr)
}
//function Letters+Num into array
var wordNum = []
for(var i = 0 ; i<200 ; i++){
var prepStrNum = randomStrNum()
wordNum.push(prepStrNum)
}
//function Letter+Num+Sym into array
var symArr = []
for(var i =0 ; i<200 ; i++){
var prepSym = randomSym()
symArr.push(prepSym)
}
console.log(symArr)
// +++++++++++++++
// Game Functions
// +++++++++++++++
//Start Intro
function intro(){
function welcomeWord(){
currentWord.innerText = 'Lets Type!'
message.innerText = `Type in ${time} secs.`;
wordInput.setAttribute('placeholder','Press Enter to start')
var box = document.getElementById('input') ;
wordInput.addEventListener('keyup', pressY)
}
setTimeout(welcomeWord() , 2000)
}
//Event Listener : Press y to Start
var pressY = function(event){
if(event.keyCode === 13){
input.value = ''
init()
}
}
//Function Initialise game
function init(){
wordInput.removeEventListener('keyup', pressY);
wordInput.setAttribute('placeholder','');
wordInput.addEventListener('input',startGame) ;
message.innerText=`Start typing!`
pickArr() ;
setInterval(countdown,1000) ;
setInterval(checkStatus,1000) ;
}
//Function starting game
var startGame = function(){
if(matchWord()){
isPlaying = true
pickArr();
wordInput.value = '';
currentWord.style.color = 'black' ;
currentWord;;
score ++ ;
}
scoreDisplay.innerText = score
}
//Function match words
var matchWord=function(){
if(wordInput.value === currentWord.innerText){
instruc.style.color = 'green'
showPraise();
console.log('Match')
currentWord.style.webkitAnimationPlayState="running";
return true
}
else{
message.innerText != null ;
instruc.style.color = 'black' ;
instruc.innerText = 'typing...';
}
}
//Function pick and show random word
var showWord = function(){
index = 0;
const randIndexWord = Math.floor(Math.random()*words.length)
currentWord.innerText = "";
for (var i = 0 ; i<words[randIndexWord].length; i++){
var eachLetter = document.createElement('span')
var splittedWords = words[randIndexWord].split("");
eachLetter.id = i;
eachLetter.innerHTML = splittedWords[i];
document.getElementById("mainWord").appendChild(eachLetter);
}
}
//Function pick and show random wordNum
var showWordNum = function(){
index = 0;
const randIndexWordNum = Math.floor(Math.random()*wordNum.length)
currentWord.innerText = ''
for(var i =0;i<wordNum[randIndexWordNum].length;i++){
var eachLetter = document.createElement('span')
var splittedWords = wordNum[randIndexWordNum].split('')
eachLetter.id = i;
eachLetter.innerHTML = splittedWords[i];
document.getElementById('mainWord').appendChild(eachLetter);
}
}
//Function pick and show random symArr
var showSymArr = function(){
index = 0
const randIndxSym = Math.floor(Math.random()*symArr.length)
currentWord.innerText = ''
for(var i =0 ; i<symArr[randIndxSym].length;i++){
var eachLetter = document.createElement('span')
var splittedWords = symArr[randIndxSym].split('')
eachLetter.id = i;
eachLetter.innerHTML = splittedWords[i]
document.getElementById('mainWord').appendChild(eachLetter)
}
}
//Function to decide if show word , word+num or word+num+symbol
var pickArr = function(){
if(score <= 3){ //score is more than 3
showWord(words)
}
else if (score < 7){ // score is more than 5
showWordNum(wordNum)
}
else{
showSymArr(symArr)
}
}
//Function `pick and show random praise
var showPraise = function(){
const randIndexPraise = Math.floor(Math.random()*praise.length)
message.innerText = praise[randIndexPraise] ;
}
//pick and show random prompt
var showPrompt = function(){
const randIndexPrompt = Math.floor(Math.random()*promptWrong.length)
message.innerText = promptWrong[randIndexPrompt];
}
//Function set Timer
var countdown= function(){
if(time > 0){
if(time >= 11){
time -- ;
isPlaying = true ;
}
else if(time <= 11){
time -- ;
timeDisplay.style.color = 'red';
timeDisplay.classList.add("blink");
isPlaying = true ;
}
}
else if(time === 0){
timeDisplay.style.color = 'red';
isPlaying = false;
}
timeDisplay.innerHTML=time;
}
//Function set Timeout for 10 secs to restart game
var timeoutdur = 10;
function minustimeout(){
timeoutdur --
return timeoutdur
}
//Function check game status
var checkStatus = function(){
if(!isPlaying && time === 0){ ;
setTimeout(function(){location.reload()},8000)
wordInput.style.visibility ='hidden';
timeDisplay.classList.remove("blink")
mainWord.classList.remove("typewriter");
message.classList.add("blink");
mainWord.classList.add("blink");
setInterval(minustimeout(),1000)
message.innerText = `Game will restart in ${timeoutdur} secs `
message.style.color = 'red' ;
message.style.fontSize = '4rem' ;
currentWord.innerHTML = 'GAME OVER' ;
currentWord.style.color = 'red' ;
wordInput.removeEventListener('keyup' , checkLetterByLetter);
}
else{
isPlaying = true ;
}
}
//Function detecting each letter of word and making it light up green.
var index = 0;
var checkLetterByLetter = function() {
var events = event.key;
var checkForLetter = currentWord.innerText;
var check = checkForLetter.indexOf(events);
var dom = document.getElementsByTagName("span");
var indexVal = document.getElementsByTagName("span")[index]
var indexInVal = indexVal.innerHTML
console.log(indexVal.innerHTML)
console.log(events);
if (indexInVal === events) {
dom[index].style.color = "green";
index += 1;
}
if (wordInput.value.length >= currentWord.innerText.length){
console.log('not tally')
showPrompt();
message.style.color = 'red'
}
}
//Event Listener : To run above function
wordInput.addEventListener('keyup' , checkLetterByLetter);