Skip to content

Commit 091ba68

Browse files
authored
Merge pull request #158 from elalmamia/hangmanGame-elalmamia-branch
added a hangman game
2 parents 93209cb + be1f338 commit 091ba68

File tree

14 files changed

+815
-0
lines changed

14 files changed

+815
-0
lines changed

hangman/ELALMAMIA/app.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
let letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
2+
const alphabet = document.getElementById('alphabet');
3+
const passwordBoard = [
4+
'A bad workman always blames his tools',
5+
'A bird in hand is worth two in the bush',
6+
'An apple a day keeps the doctor away',
7+
'Better to wear out than to rust out',
8+
'Don’t judge a book by its cover',
9+
'Good things come to those who wait.',
10+
'If you can’t beat them, join them',
11+
'It’s no use crying over spilt milk',
12+
];
13+
const passwordDiv = document.querySelector('#board');
14+
const imgDiv = document.querySelector('#hangin-dude');
15+
const random = Math.floor(Math.random() * passwordBoard.length);
16+
const password = passwordBoard[random];
17+
const yes = new Audio('yes.wav');
18+
const no = new Audio('no.wav');
19+
const win = new Audio('nice-work.wav');
20+
const lose = new Audio('oh-my-god-1.wav');
21+
let fail = 1;
22+
let countDown;
23+
24+
const start = function () {
25+
letters.split('').forEach(letter => {
26+
const html = `<div class="letter">${letter}</div>`;
27+
alphabet.insertAdjacentHTML('beforeend', html);
28+
});
29+
showPassword();
30+
showHangman(fail);
31+
};
32+
window.onload = start;
33+
34+
const passwordDashed = password.split('').map(letter => {
35+
if (letter === ' ') return ' ';
36+
else if (letter === '’') return '’';
37+
else if (letter === ',') return ',';
38+
else return '_';
39+
});
40+
const showPassword = function () {
41+
passwordDiv.innerHTML = passwordDashed.join('');
42+
};
43+
const showHangman = function (nr) {
44+
imgDiv.innerHTML = `<img src="img/${nr}.svg" alt="" />`;
45+
};
46+
47+
const checkForLetter = function (e) {
48+
if (e.target.classList.contains('letter')) {
49+
if (password.toUpperCase().split('').includes(e.target.textContent)) {
50+
yes.play();
51+
password
52+
.toUpperCase()
53+
.split('')
54+
.forEach((letter, i, arr) => {
55+
if (letter === e.target.textContent) {
56+
passwordDashed[i] = letter;
57+
showPassword();
58+
}
59+
});
60+
61+
deactivateLetter(true, e.target);
62+
} else {
63+
no.play();
64+
fail++;
65+
showHangman(fail);
66+
deactivateLetter(false, e.target);
67+
}
68+
if (fail == 6) {
69+
finish(false);
70+
}
71+
if (password.toUpperCase() === passwordDashed.join('')) {
72+
finish(true);
73+
}
74+
}
75+
};
76+
alphabet.addEventListener('click', checkForLetter);
77+
const deactivateLetter = function (hit, letter, audio) {
78+
letter.style.border = hit
79+
? '1px solid rgb(50, 177, 149)'
80+
: '1px solid rgba(255, 0, 0, 0.338)';
81+
letter.style.backgroundColor = hit
82+
? 'rgb(50, 177, 149)'
83+
: 'rgba(255, 0, 0, 0.338)';
84+
letter.style.color = 'white';
85+
letter.style.cursor = 'default';
86+
};
87+
88+
const finish = function (succes) {
89+
if (succes) {
90+
alphabet.innerHTML = `<h1>NICE WORK!</h1><div class='btn'>PLAY AGAIN</div>`;
91+
win.play();
92+
clearInterval(countDown);
93+
} else {
94+
alphabet.innerHTML = `<h1>YOU LOST!</h1><div class='btn'>TRY AGAIN</div>`;
95+
lose.play();
96+
clearInterval(countDown);
97+
}
98+
document
99+
.querySelector('.btn')
100+
.addEventListener('click', () => location.reload());
101+
};
102+
103+
const timer = function () {
104+
const timer = document.querySelector('#timer');
105+
let time = new Date(60000);
106+
const options = {
107+
minute: '2-digit',
108+
second: '2-digit',
109+
};
110+
const tick = function () {
111+
time -= 1000;
112+
timer.textContent = Intl.DateTimeFormat('en-US', options).format(time);
113+
if (time == 0) {
114+
finish(false);
115+
clearInterval(countDown);
116+
}
117+
};
118+
tick();
119+
countDown = setInterval(tick, 1000);
120+
return countDown;
121+
};
122+
timer();

hangman/ELALMAMIA/img/1.svg

Lines changed: 63 additions & 0 deletions
Loading

hangman/ELALMAMIA/img/2.svg

Lines changed: 70 additions & 0 deletions
Loading

hangman/ELALMAMIA/img/3.svg

Lines changed: 74 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)