-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangmanpreveiw.js
506 lines (446 loc) · 11.1 KB
/
hangmanpreveiw.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
// Declare variables
let port;
let reader;
let inputDone;
let outputDone;
let inputStream;
let outputStream;
let answer = '';
let maxWrong = 7;
let mistakes = 0;
let guessed = [];
let wordStatus = '';
let wins = 0;
let losses = 0;
// 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);
}
} 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
function writeToStream(...data) {
// Write to output stream
if (port && port.writable) {
const writer = port.writable.getWriter();
data.forEach(async (data) => {
await writer.write(data);
});
writer.releaseLock();
}
}
// 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 = [
" ",
"apple",
"banana",
"cherry",
"dog",
"elephant",
"frog",
"giraffe",
"hamburger",
"jellyfish",
"kangaroo",
"lemon",
"monkey",
"noodle",
"orange",
"penguin",
"quail",
"rabbit",
"strawberry",
"turtle",
"unicorn",
"violet",
"watermelon",
"xylophone",
"yogurt",
"zebra",
"airplane",
"ball",
"car",
"dolphin",
"elephant",
"flower",
"diabetes",
"guitar",
"honey",
"island",
"jacket",
"kite",
"diabetic",
"lion",
"moon",
"notebook",
"ocean",
"piano",
"queen",
"rainbow",
"sun",
"tree",
"umbrella",
"violin",
"waterfall",
"xylophone",
"yacht",
"zeppelin",
"apple",
"banana",
"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",
"elephant",
"flower",
"guitar",
"honey",
"island",
"jacket",
"kite",
"lion",
"moon",
"notebook",
"ocean",
"piano",
"queen",
"rainbow",
"sun",
"tree",
"umbrella",
"violin",
"waterfall",
"xylophone",
"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;
}
// Function to reset the hangman game
function reset() {
mistakes = 0;
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';
}
// 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);
}
// 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;
}
// Event listener for keyboard key press
document.addEventListener('keydown', function (event) {
// Check if the pressed key is a letter
if (/^[a-zA-Z]$/.test(event.key)) {
handleGuess(event.key.toLowerCase()); // Convert to lowercase
}
});
// Function to handle a guessed letter
function handleGuess(chosenLetter) {
readLoop();
guessed.indexOf(chosenLetter) === -1 ? guessed.push(chosenLetter) : null;
console.log("Chosen Letter:", chosenLetter);
//console.log("Answer:", answer);
//console.log("Guessed:", guessed);
let element = document.getElementById(chosenLetter);
if (element) {
element.setAttribute('disabled', true);
}
if (answer.indexOf(chosenLetter) >= 0) {
guessedWord();
checkIfGameWon();
} else if (answer.indexOf(chosenLetter) === -1) {
mistakes++;
updateMistakes();
checkIfGameLost();
updateHangmanPicture();
}
writeToStream('LCD :SEG');
writeToStream('Mistakes: ' + mistakes + ' of 7');
}
// 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
function checkIfGameWon() {
if (wordStatus === answer) {
togglePopup();
wins++;
updateWinLossDisplay();
document.getElementById('randomGifwin').style.display = 'none';
toggleWinGif();
document.getElementById('randomGifwin').style.float = 'center';
document.getElementById('randomGifwin').style.height = '500px';
document.getElementById('popupMessage').innerHTML = 'You Won!!!';
document.getElementById('popupMessage2').innerHTML = 'Game Over! The answer was: ' + answer;
}
updateWinLossDisplay();
}
// Function to check if the game is lost
function checkIfGameLost() {
if (mistakes === maxWrong) {
togglePopup();
losses++;
document.getElementById('randomGifwin').style.display = 'none';
togglelossGif();
document.getElementById('randomGifloss').style.float = 'center';
document.getElementById('randomGifloss').style.height = '500px';
document.getElementById('popupMessage').innerHTML = 'You Lost!!!';
document.getElementById('popupMessage2').innerHTML = 'Game Over! The answer was: ' + answer;
}
updateWinLossDisplay();
}
// Function to display guessed letters in the word
function guessedWord() {
wordStatus = answer
.split('')
.map(letter => (guessed.includes(letter.toLowerCase()) || guessed.includes(letter.toUpperCase()) ? letter : " _ "))
.join('');
document.getElementById('wordSpotlight').innerHTML = wordStatus;
//writeToStream('Word: ' + wordStatus);
}
// Function to update the mistakes count on the screen
function updateMistakes() {
document.getElementById('mistakes').innerHTML = mistakes;
//writeToStream('Mistakes: ' + mistakes + ' of 7');
}
// 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