-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
295 lines (209 loc) · 7.3 KB
/
index.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
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
let words = Object.keys(wordsList) // an array of words
function randomGramCombination () {
// returns a random bigram or trigram string
let grams = [bigrams, trigrams]
let gram = grams[range(0, grams.length - 1)]
let combination = gram[range(0, gram.length - 1)]
return combination
}
// Generate random Gram
let currentGram = randomGramCombination()
//console.log(currentGram)
// DOM query selectors
const gramEl = qs(".current-gram")
// const winLoseEl = qs(".win-lose-text")
const inputEl = qs(".text-input")
gramEl.innerText = currentGram
const alphabetContainer = qs(".alphabet-container")
function generateAlphabetElements() {
alphabetLetters.forEach(letter => {
let letterEl = document.createElement('div')
letterEl.classList.add('letter-wrapper')
letterEl.innerText = letter.toUpperCase()
alphabetContainer.appendChild(letterEl)
})
}
generateAlphabetElements()
function colorizeUsedLetter(currWord) {
const lettersEl = qsa('.letter-wrapper')
lettersEl.forEach(letterEl => {
// let isSameLetter = letterEl.innerText == currLetterKey
let letterElLetter = letterEl.innerText
let isLetterInValidWord = currWord.includes(letterElLetter)
if (isLetterInValidWord) {
letterEl.classList.add('used-letter')
}
})
}
let globalScore = 0
let lastWordScore = 0
function getWordScore(currWord) {
let currWordArray = currWord.split("")
// console.log(currWordArray)
let score = 0
currWordArray.forEach(_letter => {
for (const [lettersScore, lettersArray] of Object.entries(letterValues)) {
let isWordLetterInArray = lettersArray.includes(_letter.toLowerCase())
// console.log(`currLetterInWord: ${_letter}\ncurrScoreKey: ${lettersScore}: ${lettersArray}\nisWordLetterInArray? ${isWordLetterInArray}`)
if (isWordLetterInArray) {
score += Number(lettersScore)
// console.log(`Added ${lettersScore} points. Current word's score is now: ${score}`)
}
}
})
// console.log(`Current word's score was: ${score}`)
lastWordScore = score
globalScore += score
return globalScore
}
let currentWord = "";
// will get filled in with each unique letters used
let usedLetters = []
let usedWords = []
function wonWord(currWord = currentWord.toUpperCase(), currGram = currentGram.toUpperCase()) {
const isWordInGram = currWord.includes(currGram)
const isWordValid = words.includes(currWord.toLowerCase())
const isWordNotAlreadyUsed = !usedWords.includes(currWord)
return isWordInGram && isWordValid && isWordNotAlreadyUsed
}
function handleWordsCount() {
const wordsCountEl = qs('.words-count')
wordsCountEl.innerText = usedWords.length
}
function handleUsedLetters(_currWord) {
let wordAsArray = [..._currWord]
// console.log("Used Letters before submitting: " + usedLetters)
wordAsArray.forEach(wordLetter => {
let isWordLetterNotInUsedLetters = !usedLetters.includes(wordLetter.toUpperCase());
let isWordLetterInAlphabetLetters = alphabetLetters.includes(wordLetter.toLowerCase());
if(isWordLetterNotInUsedLetters && isWordLetterInAlphabetLetters) {
usedLetters.push(wordLetter.toUpperCase())
}
})
// console.log("Used Letters after submitting: " + usedLetters)
// Is true when the player used as many letters as there are
if (usedLetters.length === alphabetLetters.length) {
// console.log(usedLetters)
console.log("You have used all letters, congrats!")
// Gives N score when used all the letters
rewardPlayerForUsingAllLetters()
clearUsedLettersHighlight()
}
// lastWordScore = 0
}
function handleSubmission(currWord) {
// let isWon = wonWord()
// winLoseEl.innerText = wonWord() ? "Valid" : "Invalid"
// Resets the current gram if player just completed the current one
if (wonWord()) {
currentGram = randomGramCombination()
gramEl.innerText = currentGram
colorizeUsedLetter(currWord)
let wordScore = getWordScore(currWord)
globalScore = wordScore
// console.log(wordScore)
const scoreEl = qs('.score')
scoreEl.innerText = wordScore
usedWords.push(currWord.toUpperCase())
handleUsedLetters(currWord)
displayUpScore(lastWordScore)
lastWordScore = 0
handleWordsCount()
// // pushes the current unique letter to the used letter array
// if (!usedLetters.includes(ukey)) {
// usedLetters.push(ukey)
}
}
let ctrlA = {
'Control': false,
'a': false
}
function isCtrlA () {
return ctrlA['Control'] === true && ctrlA['a'] === true
}
let currentCtrlA = false
// Prevents the default behavior of the spacebar scrolling down the page
el('keydown', (e) => {
if ((e.key === ' ' || e.key === 'Tab') && e.target === document.body) {
e.preventDefault();
}
}, window)
el('keydown', (e) => {
let key = e.key
let lkey = key.toLowerCase()
let ukey = key.toUpperCase()
if (key === 'Control') {
ctrlA['Control'] = true
}
if (ctrlA['Control'] === true && lkey === 'a') {
ctrlA['a'] = true
// currentCtrlA = true
}
// console.log(key)
// console.log(alphabet.includes(lkey))
// Removes the last character inputted if backspace is pressed
if (key === "Backspace") {
if (isCtrlA()) {
currentWord = ""
inputEl.innerText = currentWord
}
currentWord = currentWord.slice(0, -1)
inputEl.innerText = currentWord
}
// Resets "Control" to false if "a" is not pressed right after
if (ctrlA['Control'] === true && lkey !== 'a') {
ctrlA['Control'] = false
ctrlA['a'] = false
// currentCtrlA = false
}
// Clears the displayed word if Enter is pressed
// Needs to be edited later to verify if the word passed matches the current gram and if the word exists
if (key === "Enter") {
handleSubmission(currentWord)
currentWord = ""
inputEl.innerText = ""
return
}
// Breaks if the passed key is not a letter of the alphabet
if (!alphabet.includes(lkey)) return
// Prevents the displayed word and variable to be more than 45 characters-long
if ((currentWord.length + 1) >= config.maxWordLength) return
currentWord += ukey
//generateColoredLettersInInput()
inputEl.innerText = currentWord.toUpperCase()
})
// Need to generate separate/individual HTML elements for each word letter, and add colored classes to the gram letters
function generateColoredLettersInInput(currWord, currGram) {
let isGramInWord = currWord.includes(currGram)
// sm
// mesmerizing
}
function displayUpScore(currWordScore) {
const plusScoreEl = qs('.plus-score')
plusScoreEl.innerText = "+" + currWordScore
plusScoreEl.classList.add('fade-up')
sleep(500).then(() => {
plusScoreEl.classList.remove('fade-up')
plusScoreEl.innerText = ""
})
lastWordScore = 0
}
function rewardPlayerForUsingAllLetters() {
lastWordScore += config.scoreToAddWhenAllLetters
// globalScore += config.scoreToAddWhenAllLetters
}
function clearUsedLettersHighlight() {
const lettersEl = qsa('.letter-wrapper')
lettersEl.forEach(letterEl => {
letterEl.classList.remove('used-letter')
})
// Emptying the usedLetters array by setting its length to 0.
usedLetters.length = 0
}
handleWordsCount()
// const exeFadeBtn = qs('.exefade')
// function executeFade(e) {
// const plusScoreEl = qs('.plus-score')
// plusScoreEl.classList.toggle('fade-up')
// }
// el('click', executeFade, exeFadeBtn)