forked from b00tc4mp/eurofirms-bootcamp-202402
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tarek blkraiet
committed
Jun 30, 2024
1 parent
2a0d09f
commit 8da84b8
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const body = new Component | ||
body.container = document.body | ||
|
||
const title = new Component('h1') | ||
title.setText('hangman') | ||
body.add(title) | ||
|
||
const startForm = new startForm | ||
|
||
startForm.onSubmit(words => { | ||
sessionStorage.secret = words | ||
|
||
body.remove(startForm) | ||
|
||
const charBoxes = new charBoxes(words) | ||
|
||
body.add(charBoxes) | ||
|
||
const guessForm = new guessForm | ||
|
||
const charsUsed = [] | ||
let assertionsCount = 0 | ||
let failsCount = 0 | ||
|
||
guessForm.onSubmit(char => { | ||
if (charsUsed.includes(char)) { | ||
alert('character was already used. Try with another one.') | ||
|
||
return | ||
} | ||
charsUsed.push(char) | ||
|
||
let charFound = false | ||
for (const i in sessionStorage.secret) { | ||
const currentChar = seesionStorage.secret[i] | ||
|
||
if (currentChar === char) { | ||
charFound = true | ||
|
||
charBoxes.showChar(i) | ||
|
||
assertionsCount++ | ||
} | ||
} | ||
if (charFound) { | ||
if (assertionsCount === seesionStorage.secret.length) { | ||
setTimeout(function () { | ||
alert('You win') | ||
|
||
body.remove(charBoxes) | ||
body.remove(guessForm) | ||
body.remove(hangman) | ||
|
||
body.add(startForm) | ||
}, 100) | ||
} | ||
|
||
return | ||
} | ||
failsCount++ | ||
|
||
hangman.setFails(failsCount) | ||
|
||
if (failsCount === 6) { | ||
setTimeout(function () { | ||
alert('Game Over.') | ||
|
||
body.remove(charBoxes) | ||
body.remove(guessForm) | ||
body.remove(hangman) | ||
|
||
body.add(startForm) | ||
}, 100) | ||
} | ||
|
||
|
||
}) | ||
|
||
body.add(guessForm) | ||
|
||
const hangman = new hangman(150, 200) | ||
hangman.setLocation(100, 300) | ||
|
||
body.add(hangman) | ||
}) | ||
|
||
body.add(startForm) |