-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangMan.js
114 lines (96 loc) · 3.26 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
let userGuess = document.querySelector('#word')
let hint = document.querySelector('#hint')
let lives = document.querySelector('#lives')
let startBtn = document.querySelector('.start-btn')
let quitBtn = document.querySelector('.quit-btn')
let finalMessage = document.querySelector('#final-message')
let letters = document.querySelectorAll('.alphabet')
let letter;
let initialFill;
let tries = []
let chosenWord;
let chosenHint;
let failed = 0
const words = ["banana", "cotton", "facebook", "school", "ghana", "abuja"];
const hints = ["fruit", "fabric", "social-media", "place", "country", "state"];
function generateWord() { // Function that generates the word randomly
let randNum = Math.floor(Math.random() * words.length);
let word = words[randNum];
return word
}
function generateHint(word){
let index = words.indexOf(word);
let hint = hints[index];
return hint;
}
function showWord() {
userGuess.innerHTML = chosenWord.split('').
map(letter =>
(tries.indexOf(letter) >= 0 ? letter.toLowerCase() : "_ "))
.join('');
hint.innerHTML = chosenHint
//document.getElementById('display1').value = wordStatus;
}
function checkGameWon(){
if(userGuess.toLowerCase() === chosenWord){
for(let each of letters){
each.disabled = true;
each.style.backgroundColor = "#fdcc94"
}
}
message.innerHTML = "----YOU'VE WON!!!----"
}
/* ---when start is clicked to begin game--- */
function handleStart(){
console.log('clicked')
chosenWord = generateWord().toLowerCase()
chosenHint = generateHint(chosenWord)
showWord()
tries = []
failed = 0;
initialFill = chosenWord.replace(/[A-Z]/g, '_ ')
for(let each of letters){
each.disabled = false;
each.style.backgroundColor = "#fdcc94"
}
}
startBtn.addEventListener('click', handleStart)
/* ---To target each alphabet press/click--- */
document.querySelector('.alpha-container').addEventListener('click', e => {
if(e.target.matches('button')){
letter = e.target
let alpha = letter.innerHTML
console.log(alpha)
tries.indexOf(alpha) === -1 ? tries.push(alpha) : null
letter.style.backgroundColor = "grey"
letter.disabled = true;
if(chosenWord.includes(alpha)){
guessedWord();
checkGameWon();
}
else{
failed++;
checkGameLost();
}
}
})
//while(remainingLetters > 0){
//let guess = prompt("Guess the letter: ");
// letter = letter.toLowerCase();
// if(guess === null) break
// else if(guess.length !== 1) alert("Please Enter a single Letter");
// for(let i = 0; i < secretWord.length;i++){
// if(secretWord[i] === letter){
// userGuess[i] = letter;
// remainingLetters--;
// }
// }
// lives--;
// console.log(userGuess)
// if(lives > 0)
// //alert(`You have ${lives} lives left. Keep Guessing...`)
// console.log(`You have ${lives} lives left. Keep Guessing...`)
// if(lives === 0) {
// break;
// }
//}