-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordguessgame.js
188 lines (138 loc) · 4.02 KB
/
wordguessgame.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
const letters = document.querySelectorAll(".scoreboard-letter");
const loadingDiv = document.querySelector(".info-bar");
const ANSWER_LENGTH = 5;
const ROUNDS = 6;
// Making an async function with the typing logic
async function init() {
let currentGuess = "";
let currentRow = 0;
let done = false;
let isLoading = true;
// Fetching the data over here
const res = await fetch("https://words.dev-apis.com/word-of-the-day");
const resObj = await res.json();
const word = resObj.word.toUpperCase();
const wordParts = word.split("");
setLoading = false;
isLoading = false;
//Add Letter Function
function addLetter(letter) {
if (currentGuess.length < ANSWER_LENGTH) {
// add letter to the end
currentGuess += letter;
} else {
// replace the last letter
currentGuess = currentGuess.substring(0, currentGuess.length - 1) + letter;
}
//making sure that we start our line from the first row
letters[ANSWER_LENGTH * currentRow + currentGuess.length - 1].innerText = letter;
}
// Commit Function
async function commit() {
if (currentGuess.length != ANSWER_LENGTH) {
// do nothing
return;
}
//POST API HERE
isLoading = true;
setLoading(true);
const res = await fetch ("https://words.dev-apis.com/validate-word",{
method: "POST",
body: JSON.stringify({word: currentGuess}),
});
const resObj = await res.json();
const validWord = resObj.validWord;
isLoading = false;
setLoading(false);
if (!validWord) {
markInvalidWord();
return;
}
// TODO validate the word
// TODO do all the marking as "correct" "close" or "wrong"
const guessParts = currentGuess.split("");
const map = makeMap(wordParts);
for (let i = 0; i < ANSWER_LENGTH; i++) {
//mark as correct
if (guessParts[i] === wordParts[i]) {
letters[currentRow * ANSWER_LENGTH + i].classList.add("correct");
map[guessParts[i]]--;
}
}
for (let i = 0; i < ANSWER_LENGTH; i++) {
if(guessParts[i] === wordParts[i]){
//do nothing we already did it
} else if (wordParts.includes(guessParts[i]) && map[guessParts[i] > 0]) {
//mark as close
letters[currentRow * ANSWER_LENGTH + i].classList.add("close")
map[guessParts[i]]--;
} else {
letters[currentRow * ANSWER_LENGTH + i].classList.add("wrong");
}
}
currentRow++;
currentGuess = "";
// Losing condition here
if (currentGuess === word) {
alert("you win!");
done = true;
} else if (currentRow === ROUNDS) {
alert(`you lose, the word was ${word}`);
done = true;
}
}
//Backspace function
function backspace() {
currentGuess = currentGuess.substring(0, currentGuess.length - 1);
letters = [ANSWER_LENGTH * currentGuess + currentGuess.length].innerText = "";
}
//MarkInvalidWord Function
function markInvalidWord () {
for (i = 0; i < ANSWER_LENGTH; i++){
letters[currentRow * ANSWER_LENGTH + i].classList.remove("invalid");
setTimeout(function (){
letters[currentRow * ANSWER_LENGTH + i].classList.add("invalid")
}, 10);
}
}
//Keyboard Interaction
document.addEventListener("keydown", function handleKeyPress(event) {
if(done || isLoading) {
//do nothing
return;
}
const action = event.key;
console.log(action);
if (action === "Enter") {
commit();
} else if (action === "Backspace") {
backspace();
} else if (isLetter(action)) {
addLetter(action.toUpperCase());
} else {
//do nothing
}
});
}
//is Letter function
function isLetter(letter) {
return /^[a-zA-Z]$/.test(letter);
}
function setLoading(isLoading) {
loadingDiv.classList.toggle('show', isLoading);
}
//takes an array as an argument and returns an object where keys are elements from the array,
// and values are the frequency of each element in the array
function makeMap (array) {
const obj = {};
for (let i = 0; i < array.length; i++) {
const letter = array[i]
if (obj[letter]) {
obj[letter]++;
} else {
obj[letter] = 1
}
}
return obj;
}
init();