-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.js
620 lines (549 loc) · 16 KB
/
hangman.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
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// Declare variables
const fs = require('fs');
let port;
let reader;
let inputDone;
let outputDone;
let inputStream;
let outputStream;
let answer = '';
let maxWrong = 7;
let mistakes;
let guessed = [];
let answerspaced = '';
let wordStatus = '';
let spacedword = '';
let blankwordStatus = '';
let wins = 0;
let losses = 0;
let scrollInterval;
let writeToLCD = true;
let total = wins + losses;
// this function returns a promise that resolves after n milliseconds
const wait = (n) => new Promise((resolve) => setTimeout(resolve, n));
// Function to connect to the serial port
async function connect() {
try {
// Prompt user to select any serial port.
port = await navigator.serial.requestPort();
// Wait for the serial port to open.
await port.open({ baudRate: 9600 });
// Setup the output stream and read loop here.
const ports = await navigator.serial.getPorts();
console.log(ports);
// Call readLoop only once after opening the port
readLoop();
} catch (error) {
console.error('Error connecting to serial port:', error);
}
}
async function readLoop() {
// Check if the port object is defined
if (!port) {
console.error('Serial port is not initialized.');
return;
}
const reader = port.readable.getReader();
while (true) {
let result;
try {
result = await reader.read();
} catch (error) {
console.error('Error reading from the serial port:', error);
break;
}
const { value, done } = result;
if (done) {
console.log('Reader is done.');
break;
}
// Ensure that the value is a string before processing
if (typeof value === 'string' || value instanceof Uint8Array) {
// Convert Uint8Array to string
const receivedData = (typeof value === 'string') ? value : new TextDecoder().decode(value);
if (receivedData.trim().length > 0) {
console.log('Received:', receivedData);
// Call the function to handle the received data
handleGuess(receivedData);
chosenLetter = receivedData;
}
} else {
console.error('Received data is not a string or Uint8Array:', value);
}
}
// Release the lock after processing each chunk of data
reader.releaseLock();
// Call readLoop again after releasing the lock
readLoop();
}
async function Test() {
const ports = await navigator.serial.getPorts();
console.log(ports);
}
// Function to disconnect from the serial port
async function disconnect() {
// Close the input stream (reader).
if (reader) {
await reader.cancel();
reader.releaseLock();
}
// Close the output stream.
// Close the port.
if (port) {
await port.close();
}
}
// Function to write data to the serial port
async function writeToStream(data) {
// Write to output streamx
if (port && port.writable) {
const encoder = new TextEncoder();
const dataOut = encoder.encode(data);
const writer = port.writable.getWriter();
console.log('Writing:', data);
await writer.write(dataOut);
writer.releaseLock(dataOut);
return data;
}
return data;
}
// Function to send Ctrl+C to the serial port
function sendCtrlC() {
// Send CTRL-C and turn off echo on REPL
//writeToStream('\x03'); // ASCII code for CTRL-C
}
// Word bank for the hangman game
var WordBank = [
"banana",
"apple",
"cherry",
"dog",
"elephant",
"frog",
"giraffe",
"hamburger",
"icecream",
"jellyfish",
"kangaroo",
"lemon",
"monkey",
"noodle",
"orange",
"penguin",
"quail",
"rabbit",
"strawberry",
"turtle",
"unicorn",
"violet",
"watermelon",
"xylophone",
"yogurt",
"zebra",
"airplane",
"ball",
"car",
"dolphin",
"flower",
"diabetes",
"guitar",
"honey",
"island",
"jacket",
"kite",
"lion",
"moon",
"notebook",
"ocean",
"piano",
"queen",
"rainbow",
"sun",
"tree",
"umbrella",
"violin",
"waterfall",
"yacht",
"zeppelin",
"taco",
"pizza",
"sushi",
"cookie",
"chocolate",
"cheese",
"bread",
"butter",
"chicken",
"fish",
"bear",
"cat",
"horse",
"mouse",
"owl",
"panda",
"snake",
"tiger",
"whale",
"wolf",
"book",
"chair",
"clock",
"door",
"hat",
"house",
"key",
"light",
"pen",
"phone",
"ship",
"shoe",
"table",
"tent",
"train",
"beach",
"city",
"desert",
"forest",
"jungle",
"mountain",
"river",
"space",
"cloud",
"color",
"family",
"friend",
"game",
"music",
"number",
"rain",
"sleep",
"star",
];
// Function to generate a random GIF path
function randomGifwin() {
let gifs = [
"images/Excellent.gif",
"images/greatsucces.gif",
"images/win.gif",
"images/win2.gif",
"images/win3.gif",
"images/youwinthistime.gif"
// Add more gif paths here
];
let randomIndexwin = Math.floor(Math.random() * gifs.length);
let randomGifPathwin = gifs[randomIndexwin];
return randomGifPathwin;
}
function randomGifloss() {
let gifs = [
"images/loss1.gif",
"images/loss2.gif",
"images/patrick.gif",
"images/pepe-the-frog-laptop-cry.gif",
"images/L.gif"
// Add more gif paths here
];
let randomIndexloss = Math.floor(Math.random() * gifs.length);
let randomGifPathloss = gifs[randomIndexloss];
return randomGifPathloss;
}
// Set this variable to false when you want to stop writing to the LCD
function scrollLCD(text) {
let scrollIndex = 0;
const scrollInterval = setInterval(async () => {
const scrolledText = text.substring(scrollIndex, scrollIndex + 16); // Limit the scrolled text to 16 characters per line
if (writeToLCD) {
await writeToStream(scrolledText); // Send the scrolled text through writeToStream
}
console.log(scrolledText);
scrollIndex++;
if (scrollIndex > text.length - 16) {
clearInterval(scrollInterval);
}
}, 300); // Adjust the scroll speed as needed
}
// Function to send the message periodically
function sendPeriodicMessage(message, interval) {
setInterval(async () => {
if (writeToLCD) {
await writeToStream(answerspaced);
}
scrollLCD(message);
console.log(message);
}, interval);
}
/* Example usage
sendPeriodicMessage("Hello, world!", 2000); // Send "Hello, world!" every 2 seconds
*/
// Function to reset the hangman game
async function reset() {
// Event listener for keyboard key press
//console.log(WordBank);
const WordBank = fs.readFileSync('WordBank.txt', 'utf-8').split('\n');
clearInterval(sendPeriodicMessage);
clearInterval(scrollInterval);
writeToLCD = false;
mistakes = 0;
document.addEventListener('keydown', handleGuess);
guessed = [];
document.getElementById('hangmanPic').src = './images/Error0.png';
document.getElementById('randomGifwin').style.display = 'none';
document.getElementById('randomGifloss').style.display = 'none';
//readLoop();
let popup = document.getElementById('popup'); // Declare and initialize the popup variable
if (popup) {
popup.style.display = 'none';
}
wait
// Move randomGif initialization here
let gifPathwin = randomGifwin();
document.getElementById('randomGifwin').src = gifPathwin;
let gifPathloss = randomGifloss();
document.getElementById('randomGifloss').src = gifPathloss;
console.log("Gif Path:", gifPathwin);
console.log("Gif Path:", gifPathloss);
randomWord();
guessedWord();
//updateMistakes();
generateButtons();
console.log("Answer:", answer);
await wait(10);
//await writeToStream('');
//await writeToStream(mistakes);
let data = await writeToStream('___New Game!____' + '___New Game!____' + mistakes);
console.log("writeToStream:", data, data.length);
//let data = await writeToStream(spacedword + '________________' + mistakes);
//await writeToStream("abcdefghijklmnopqrstuvwxyzsecret3");
console.log("writeToStream:", data, data.length);
total = wins + losses;
mistakes = 0;
}
// Function to generate random word from the word bank
function randomWord() {
answer = WordBank[Math.floor(Math.random() * WordBank.length)];
}
// Function to generate on-screen keyboard buttons
function generateButtons() {
let buttonsHTML = 'abcdefghijklmnopqrstuvwxyz'.split('').map(letter =>
`
<button
class="btn btn-lg btn-primary m-2"
id='` + letter + `'
onClick="handleGuess('` + letter + `')"
>
` + letter + `
</button>
`).join('');
//document.getElementById('keyboard').innerHTML = buttonsHTML;
let keyboardElement = document.getElementById('keyboard');
if (keyboardElement) {
keyboardElement.innerHTML = buttonsHTML;
} else {
console.error("Element with ID 'keyboard' not found.");
}
}
document.addEventListener('keydown', handleGuess);
// Function to handle a guessed letter
async function handleGuess(guessedLetter) {
if (/^[a-zA-Z]$/.test(guessedLetter)) {
const chosenLetter = guessedLetter.toLowerCase(); // Directly use the guessedLetter parameter
// Use chosenLetter within this block
guessed.indexOf(chosenLetter) === -1 ? guessed.push(chosenLetter) : null;
console.log("Chosen Letter:", chosenLetter);
//console.log("Answer:", answer);
//console.log("Guessed:", guessed);
let data = await writeToStream(spacedword + ' ' + mistakes);
await writeToStream(spacedword + ' ' + mistakes);
//console.log(`(${data})(${data.length})`);
console.log("writingToStream:", data, data.length);
//await writeToStream("abcdefghijklmnopqrstuvwxyzsecret3");
let element = document.getElementById(chosenLetter);
if (element) {
element.setAttribute('disabled', true);
}
if (answer.indexOf(chosenLetter) >= 0) {
guessedWord();
checkIfGameWon();
} else if (answer.indexOf(chosenLetter) === -1) {
updateMistakes();
checkIfGameLost();
updateHangmanPicture();
}
}
// Assuming guessedLetter is a string representing the letter guessed
if (/^[a-zA-Z]$/.test(event.key.toLowerCase())) {
const chosenLetter = event.key.toLowerCase(); // Define chosenLetter here
// Use chosenLetter within this block
guessed.indexOf(chosenLetter) === -1 ? guessed.push(chosenLetter) : null;
console.log("Chosen Letter:", chosenLetter);
//console.log("Answer:", answer);
//console.log("Guessed:", guessed);
let data = await writeToStream(spacedword + ' ' + mistakes);
//console.log(`(${data})(${data.length})`);
console.log("writingToStream:", data, data.length);
//await writeToStream("abcdefghijklmnopqrstuvwxyzsecret3");
let element = document.getElementById(chosenLetter);
if (element) {
element.setAttribute('disabled', true);
}
if (answer.indexOf(chosenLetter) >= 0) {
guessedWord();
checkIfGameWon();
} else if (answer.indexOf(chosenLetter) === -1) {
updateMistakes();
checkIfGameLost();
updateHangmanPicture();
}
}
}
// Function to update the hangman picture
function updateHangmanPicture() {
document.getElementById('hangmanPic').src = './images/Error' + mistakes + '.png';
document.getElementById('hangmanPic').style.float = 'left';
document.getElementById('hangmanPic').style.height = '40%';
}
// Function to check if the game is won
async function checkIfGameWon() {
total = wins + losses;
if (wordStatus === answer) {
document.removeEventListener('keydown', handleGuess);
togglePopup();
wins++;
updateWinLossDisplay();
document.getElementById('randomGifwin').style.display = 'none';
toggleWinGif();
document.getElementById('randomGifwin').style.height = '450px';
document.getElementById('popupMessage').innerHTML = 'You Won!!!';
document.getElementById('popupMessage2').innerHTML = 'The answer was: ' + answer;
//updateMistakes();
guessedWord();
document.addEventListener('keydown', YNButton);
//await writeToStream(answerspaced + '________________' + mistakes);
writeToLCD = true;
//await wait(500);
total = wins + losses;
scrollLCD('Well Done! You have solved ' + wins + ' out of ' + total + ' puzzles!',200);
await wait(16000);
writeToLCD = false;
await writeToStream('____GAME_OVER___' + '____GAME_OVER___');
//await wait(10000);
// Wait for user input to reset or exit
//await waitForUserInput();
}
updateWinLossDisplay();
}
// Function to check if the game is lost
async function checkIfGameLost() {
total = wins + losses;
if (mistakes === maxWrong) {
losses++;
document.removeEventListener('keydown', handleGuess);
togglePopup();
document.getElementById('randomGifwin').style.display = 'none';
togglelossGif();
document.getElementById('randomGifloss').style.height = '700px';
document.getElementById('popupMessage').innerHTML = 'You Lost!!!';
document.getElementById('popupMessage2').innerHTML = 'The answer was: ' + answer;
//updateMistakes();
guessedWord();
document.addEventListener('keydown', YNButton);
writeToLCD = true;
//await wait(500);
scrollLCD ('Sorry! The correct word was ' + answer + ' you have solve '+ wins + ' puzzles out of ' + total + ' puzzles!', 200);
await wait(9500);
writeToLCD = false;
await writeToStream('____GAME_OVER___' + '____GAME_OVER___');
//await wait(10000);
}
updateWinLossDisplay();
}
async function YNButton(event) {
await wait(1000)
pressedKey = event.key.toLowerCase();
if (pressedKey === "y") {
reset();
//clearInterval(sendPeriodicMessage);
//clearInterval(scrollInterval);
document.removeEventListener('keydown', YNButton);
return;
}
else if (pressedKey === "n") {
// Close the Chrome tab or take any other action
window.location.href = 'WINNER.html';
//await writeToStream('____GAME_OVER___' + '____GAME_OVER___' + mistakes);
await wait(888);
await writeToStream('____GAME_OVER___' + '____GAME_OVER___' + mistakes);
//sendPeriodicMessage(wins + ' out of ' + total + ' puzzles!', 1000);
return;
}
}
// Function to remove spaces between underscores
function removeSpaces(wordStatus) {
return wordStatus.replace(/ /g, '');
}
// Function to display guessed letters in the word
async function guessedWord() {
wordStatus = answer
.split('')
.map(letter => (guessed.includes(letter.toLowerCase()) || guessed.includes(letter.toUpperCase()) ? letter : " _ "))
.join('');
//console.log(wordStatus,wordStatus.length);
document.getElementById('wordSpotlight').innerHTML = wordStatus;
blankwordStatus = removeSpaces(wordStatus);
answerspaced = answer;
while (answerspaced.length < 16) {
answerspaced += ' ';
}
spacedword = blankwordStatus;
while (spacedword.length < 16) {
spacedword += ' ';
}
await writeToStream(spacedword + '________________' + mistakes);
//await writeToStream(mistakes + wordStatus + ' ');
//await writeToStream('Test');
//await writeToStream(mistakes);
}
// Function to update the mistakes count on the screen
function updateMistakes() {
if (mistakes < 7) {
mistakes++;
document.getElementById('mistakes').innerHTML = mistakes;
}else{
mistakes =0;
}
console.log(mistakes);
return;
}
// Function to toggle the visibility of the popup
function togglePopup() {
let popup = document.getElementById('popup');
if (popup) {
popup.style.display = (popup.style.display === 'none') ? 'block' : 'none';
}
}
// Function to toggle the visibility of the gifs
function toggleWinGif() {
let randomGifwin = document.getElementById('randomGifwin');
if (randomGifwin) {
randomGifwin.style.display = (randomGifwin.style.display === 'none') ? 'block' : 'none';
}
updateWinLossDisplay();
}
function togglelossGif() {
let randomGifloss = document.getElementById('randomGifloss');
if (randomGifloss) {
randomGifloss.style.display = (randomGifloss.style.display === 'none') ? 'block' : 'none';
}
updateWinLossDisplay();
}
function updateWinLossDisplay() {
document.getElementById('winsCount').innerHTML = wins;
document.getElementById('lossesCount').innerHTML = losses;
}
// Call when the page loads
randomWord();
generateButtons();
disconnect(); // Add this line to close any existing port
//readLoop();
//connect();
updateWinLossDisplay(); // Initialize win and loss counts on page load