-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman2.html
409 lines (264 loc) · 10.1 KB
/
hangman2.html
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
.hide{
display:none;
}
.image{
background-image: url("phi.jpg");
height: 100vh;
width: 100vw;
background-size: cover;
position: absolute;
}
h1{
font-size:6em;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id="hide">
<span id="hint">Hint: Philosophy</span><br>
<span id="gameTimer"></span>
<br>
<span id="timer"></span><br><span id="disclaimer"> countdown from 10 above, get at least one letter right before that runs out, you better work well under pressure!</span>
</div>
</div>
<div id="game">
<p>Press any letter to start playing</p>
</div>
<script>
console.log("welcome to hangman! Get ready to rumble! below see the amount of letters that you the unknown word has");
var alphabet = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var timer = 10;
var dateUponLaunch = new Date().getTime();
var minutes = 0;
var tenSeconds = 0;
var seconds = 0;
function secondTimer(){
var thisRun = new Date().getTime() ;
minutes = Math.floor(( thisRun % dateUponLaunch)/(1000*60));
tenSeconds = Math.floor(((( thisRun % dateUponLaunch)/1000)/10)%6);
seconds = Math.floor((( thisRun % dateUponLaunch)/1000)%10);
timer--;
document.getElementById("gameTimer").innerHTML = "Game Time :" + minutes + ":"+tenSeconds+ seconds + " s ";
};//global function secondTimer();
function timeReducer(){
if(hangManGame.correctGuesses == hangManGame.randomWord.length){
hangManGame.displayValues();
}
if(timer < 0) {
document.querySelector("#timer").innerHTML = Math.round(timer, -1);
hangManGame.losses++;
hangManGame.displayValues();
hangManGame.resetPerRoundValues();
}
else{
document.querySelector("#timer").innerHTML = Math.round(timer, -1);
}
};
var hangManGame = {
setOfWords : ["existentialism","epistemology", "esthetics", "morality", "metaphysics","logic","ethics","politics","axiology"],
characters : [],
lines : [],
guesses : [],
listOfWords :[],//gonna sort these alpabetically, I know there is a predefined sort function already but I wanted to see if I could do it without using it. I know that this wouldn't be the way to go in the context of a real world project with a deadline but I wanted to challenge myself.
chances : 12,
correctGuesses : 0,
losses : 0,
wins : 0,
guess : "",
wordNumber : 1 ,
correctLetterForRun : "",
thisWasACorrectGuess : false,
randomWord : "",
needToTriggerTimer : true,
longestString : "",
gameWon : false,
//gonna sort these alpabetically, I know there is a predefined sort function already but I wanted to see if I could do it without using it. I know that this wouldn't be the way to go in the context of a real world project with a deadline but I wanted to challenge myself further.
sortTheWordsDoneInAlphOrder : function() {
for (var q = 0; q < this.listOfWords.length; q++)
{
if(hangManGame.listOfWords[q].length > hangManGame.longestString.length){
hangManGame.longestString = hangManGame.listOfWords[q];
}
}
function organizeWordsByAssignedLetter(character){
for (var i = 0 ; i < hangManGame.listOfWords.length ; i++) {
for (var z = i+1; z < hangManGame.listOfWords.length; z++) {
var allCharactersTillNowHaveBeenSame = 0;
if (character == 0) {
if (alphabet.indexOf(hangManGame.listOfWords[z].charAt(character)) < alphabet.indexOf(hangManGame.listOfWords[i].charAt(character))) { //1 works
var WordToSwap = hangManGame.listOfWords[z];
hangManGame.listOfWords[z] = hangManGame.listOfWords[i];
hangManGame.listOfWords[i] = WordToSwap;
}
}
else if(character > 0){
for (var y = 0 ; y < character; y++) {
if(hangManGame.listOfWords[i].charAt(y) == hangManGame.listOfWords[z].charAt(y)){
allCharactersTillNowHaveBeenSame++;
}
}
if (allCharactersTillNowHaveBeenSame == character){
//left off here
if (alphabet.indexOf(hangManGame.listOfWords[z].charAt(character)) < alphabet.indexOf(hangManGame.listOfWords[i].charAt(character)) && alphabet.indexOf(hangManGame.listOfWords[i].charAt(character)) != -1) { //1 works
var WordToSwap = hangManGame.listOfWords[z];
//console.log(" round: " + character+ " comparing:" +listOfWords[i] + " run number or z " + z + " word number or i " + i + " swapping "+ listOfWords[i]+ " with " + WordToSwap);
hangManGame.listOfWords[z] = hangManGame.listOfWords[i];
hangManGame.listOfWords[i] = WordToSwap;
// console.log("now:");
//console.log(listOfWords.join("-"));
}
}
}
}
}
};
for (var i = 0; i < hangManGame.longestString.length ; i++) {
organizeWordsByAssignedLetter(i);
}
},
displayValues : function(){
var valuesToDisplay =
"<p>word number: " + this.wordNumber+ "</p>"+
"<p>You chose: " + this.guess + "</p>" +
"<p>word:</p>" + this.lines.join(' ')+
"<p>Letters already used:" + this.guesses.join() + "</p>" +
"<p>Correct Guesses: " + this.correctGuesses+ "</p>" +
"<p>Losses:" + this.losses +"</p>" +
"<p>wins: " + this.wins + "</p>"+
"<p>Wrong guess chances: " + this.chances + "</p>" +
"<p>Words you have guessed correctly:</p>" + this.listOfWords.join("<br>");
//return document.createElement("span").innerText;
if(!this.gameWon){
document.querySelector("#game").innerHTML = valuesToDisplay;
}
else{
var endGame = "<h1>I think Therefore I am!</h1>" + "<h1> You took " + minutes + ":"+tenSeconds + seconds +" To be enlightened</h1>" ;
document.querySelector("#hide").setAttribute("class", "hide");
document.querySelector("#game").innerHTML = endGame;
document.querySelector("#game").setAttribute("class", "image");
}
},
//checks to see if the word has already been chosen before
// if(this.guesses.includes(this.guess))
youAlreadyChoseThisLetter : function(){
this.displayValues();
alert("you already chose: " + hangManGame.guess +" choose something else");
this.chances--;
timer = 10;
},
//checks if you won to restart the game
// else if(this.correctGuesses == this.characters.length)
youJustGotACorrectWord : function(){
// if(!this.listOfWords.includes(this.randomWord)){
var index = this.setOfWords.indexOf(this.randomWord);
if (index > -1) {
this.setOfWords.splice(index, 1);
}
console.log(this.setOfWords.join("-"));
var that = this;
this.wins++;
this.listOfWords.push(this.randomWord);
setTimeout(function (){that.sortTheWordsDoneInAlphOrder();}, 300);
setTimeout(function(){alert("congrats, you got the word right!")}, 100);
setTimeout(function (){that.resetPerRoundValues();}, 500);
},
// ranOutOfTime : function(){
// this.losses--;
// this.displayValues();
// this.resetPerRoundValues();
//},
ranOutOfChances : function(){
this.losses++;
this.displayValues();
setTimeout(hangManGame.resetPerRoundValues, 200);
},
wrongGuess : function(){
this.chances--;
this.displayValues();
},
generateRandomWordAndLines : function(){
this.randomWord = this.setOfWords[Math.floor(Math.random() * this.setOfWords.length)];
for (var i = 0; i < this.randomWord.length; i++)
{
this.characters.push(this.randomWord.toLowerCase().charAt(i));
console.log(this.randomWord);
this.lines.push("_");
}
},//ends generateRandomWord
resetPerRoundValues : function(){
if(hangManGame.setOfWords.length != 0){
if(confirm("do you wanna play again?")){
hangManGame.characters = [];
hangManGame.lines = [];
hangManGame.guesses = [];
timer = 10;
hangManGame.correctGuesses = 0;
hangManGame.generateRandomWordAndLines();
hangManGame.wordNumber++;
hangManGame.chances = 12;
alert("new word!");
hangManGame.displayValues();
}//ends if statement
}//ends the if statement
else{
this.gameWon = true;
alert("You Guessed all the words right!!!");
this.displayValues();
}
},//ends the object
};
// generates a random word to start the program upon launch
hangManGame.generateRandomWordAndLines();
document.onkeyup = function(){
if(hangManGame.needToTriggerTimer == true){
dateUponLaunch = new Date().getTime();
setInterval(function(){if(!hangManGame.gameWon)timeReducer()}, 10);
setInterval(function(){if(!hangManGame.gameWon)secondTimer()}, 1000);
hangManGame.needToTriggerTimer = false;
}
hangManGame.guess = event.key.toLowerCase();
if (hangManGame.guesses.includes(hangManGame.guess)){
hangManGame.youAlreadyChoseThisLetter();
}//ends the conditional if statement
else{
for(var i = 0; i < hangManGame.characters.length; i++) {
if (hangManGame.guess == hangManGame.characters[i] && !hangManGame.guesses.includes(hangManGame.guess))
{
hangManGame.lines[i] = hangManGame.characters[i];
hangManGame.correctGuesses++;
timer = 10;
hangManGame.correctLetterForRun = hangManGame.characters[i];
hangManGame.thisWasACorrectGuess = true;
}//ends the if statement checking the letters repeatedly to see if they match the input
}//ends the for loop making that checking possible
if (hangManGame.correctGuesses == hangManGame.randomWord.length){
hangManGame.youJustGotACorrectWord();
}
if(!hangManGame.thisWasACorrectGuess){
hangManGame.wrongGuess();
}
//adds the guess for this round go the
if (alphabet.includes(hangManGame.guess)) {
hangManGame.guesses.push(hangManGame.guess);
}
else{
alert("that is not a valid key to press, don't be silly, this word is in english, not unicode, by the way, you lose a chance for that");
}
if(hangManGame.chances == 0){
hangManGame.ranOutOfChances();
}
hangManGame.displayValues();
//this if statement is here in order to check whether or not the chances ==0 in order to know whether or not to launch a new game
hangManGame.thisWasACorrectGuess = false;
}//ends the else
}//ends the event
</script>
</body>
</html>