-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.js
225 lines (183 loc) · 7.04 KB
/
logic.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
// GLABAL VARIABLES
//-------------------------------------------------------------------------------------
// Array of word options (all lowercase)
var wordsList =
["kapkan",
"tachanka",
"glaz",
"fuze",
"iq",
"blitz",
"bandit",
"jager",
"rook",
"doc",
"twitch",
"montagne",
"thermite",
"pulse",
"castle",
"ash",
"thatcher",
"smoke",
"sledge",
"mute",
"frost",
"buck",
"valkyrie",
"blackbeard",
"capitao",
"caveira",
"echo",
"hibana",
"jackal",
"mira",
"lesion",
"ying",
"ela",
"zofia",
"dokkaebi",
"vigil",
"finka",
"lion",
"alibi",
"maestro",
"maverick",
"clash",
"kaid",
"nomad",
"gridlock",
"mozzie"];
// Solution will be held here
var chosenWord = "";
// This will break the solution into individual letters to be stored in an array
var lettersInChosenWord = [];
// This will be the numbwer of blanks we show based on the solution
var numBlanks = 0;
// Holds a mix of blank and solved letters (ex: a_p_le)
var blanksAndSuccesses = [];
// Holds wrong guesses
var wrongGuesses = [];
// Game counters
var winCounter = 0;
var lossCounter = 0;
var numGuesses = 0;
// FUNCTIONS
//-------------------------------------------------------------------------------------
/* It's how we will start and restart the game. It's not being run here.
It is just being made for future use. */
function startGame() {
// Resets guesses to specified number
numGuesses = 9;
document.getElementById("loss-counter").innerHTML = lossCounter;
document.getElementById("guesses-left").innerHTML = numGuesses;
document.getElementById("win-counter").innerHTML = winCounter;
// Soultion is chosen randomly from wordList
chosenWord = wordsList[Math.floor(Math.random() * wordsList.length)];
// The word is brocken into individual letters
lettersInChosenWord = chosenWord.split("");
// We count the number of letters in the word
numBlanks = lettersInChosenWord.length;
// We print the solution in the console (for testing)
console.log(`Chosen word: ${chosenWord}`);
// CRITICAL LINE - Here we reset the guess and success array at each round
blanksAndSuccesses = [];
// CRITICAL LINE - Here we reset the wrong guesses from the previous round
wrongGuesses = [];
document.getElementById("op-name").innerHTML = "";
document.getElementById("result").style.backgroundColor = "";
document.getElementsByTagName("IMG")[0].removeAttribute("src");
/* Fill up the blanksAndSuccesses list with appropriate number of blanks,
which is based on number of letters in the solution. */
for (var i = 0; i < numBlanks; i++) {
blanksAndSuccesses.push('_');
}
// Print the initial blanks in console.
console.log(blanksAndSuccesses);
//Reprint the guessesLeft to 9
document.getElementById("guesses-left").innerHTML = numGuesses;
// Print the blanks at the beginng of each round in the HTML
document.getElementById("word-blanks").innerHTML = blanksAndSuccesses.join(' ');
// Clears the wrong guesses from the previous round
document.getElementById("wrong-guesses").innerHTML = wrongGuesses.join(' ');
}
// It's where we will do all of the comparisons for matches
function checkLetters(letter) {
// This boolean will be toggled based on whether or not a user letter is found
var letterInWord = false;
if (numGuesses > -1) {
// Checks if a letter exists inside the array at all
for (var i = 0; i < numBlanks; i++) {
if (chosenWord[i] === letter) {
letterInWord = true;
}
}
// If the letter exists somewhere in the word, then figure out exactly where (which index?)
if (letterInWord) {
// Loop through the word
for (var x = 0; x < numBlanks; x++) {
// Populate the blancksAndSucesses with every instance of the letter
if (chosenWord[x] === letter) {
// Here we set the specific space in blanks and letters equal to the letter when it matches
blanksAndSuccesses[x] = letter;
}
}
console.log(blanksAndSuccesses);
}
// If the letter doesn't exist at all...
else {
// ...then we add the letter to the list of wrong letters, and we subtract one of the guesses
wrongGuesses.push(letter);
numGuesses--;
}
}
}
// Here we will have all of the code that needs to be run after each guess is made
function roundComplete() {
if (numGuesses > -1) {
// First, log initial status update in the console telling us how many wins, losses, and guesses left
console.log(`Win counter: ${winCounter} | Loss counter: ${lossCounter} | Guesses: ${numGuesses}`);
// Update the HTML to reflect the number of guesses. Also update the correct guess.
document.getElementById("guesses-left").innerHTML = numGuesses;
// This will print the array of guesses and blanks onto the page
document.getElementById("word-blanks").innerHTML = blanksAndSuccesses.join(" ");
// This will print the wrong guesses onto the page
document.getElementById("wrong-guesses").innerHTML = wrongGuesses.join(" ");
// If you got all letters to match the solution...
if (lettersInChosenWord.toString() === blanksAndSuccesses.toString()) {
//... add to win counter and give the user an alert
winCounter++;
document.getElementById("op-name").innerHTML = chosenWord.toUpperCase();
document.getElementById("result").style.backgroundColor = "#228B22";
document.getElementsByTagName("IMG")[0].setAttribute("src", `ops-pictures/${chosenWord.toLowerCase()}.png`);
document.getElementsByTagName("IMG")[0].style.width = "500px";
// Update the winCounter in the HTML and restart the game
document.getElementById("win-counter").innerHTML = winCounter;
}
//if we've run out of guesses
else if (numGuesses < 1) {
//add to the lossCounter
lossCounter++;
// give user an alert
document.getElementById("op-name").innerHTML = chosenWord.toUpperCase();
document.getElementById("result").style.backgroundColor = "#B22222";
document.getElementsByTagName("IMG")[0].setAttribute("src", `ops-pictures/${chosenWord.toLowerCase()}.png`);
document.getElementsByTagName("IMG")[0].style.width = "500px";
// Update the loss counter in HTML
document.getElementById("loss-counter").innerHTML = lossCounter;
}
}
}
// MAIN PROCESS (Controls actual code running)
//-------------------------------------------------------------------------------------
// Starts Game
startGame();
// Captures keys pressed
document.onkeyup = function(event) {
// converts all key klicks to lowercase
var letterGuessed = String.fromCharCode(event.keyCode).toLowerCase();
// runs code to check correctness
checkLetters(letterGuessed);
// runs code after each round
roundComplete();
}