-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangmanpage.js
More file actions
135 lines (132 loc) · 4.68 KB
/
hangmanpage.js
File metadata and controls
135 lines (132 loc) · 4.68 KB
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
let man = document.getElementById("man");
let line = document.getElementById("line");
let guess = document.getElementById("guess");
let message = document.getElementById("message");
const guessbutton = document.getElementById("guessbutton");
let gletters = document.getElementById("guessedletters");
let gwords = document.getElementById("guessedwords");
const hangmanart = {
0: " <br> <br> ",
1: " O <br> <br> ",
2: " O <br> | <br> ",
3: " O <br>/| <br> ",
4: " O <br>/|\\<br> ",
5: " O <br>/|\\<br>/ ",
6: ":-(<br>/|\\<br>/ \\",
7: ":-)<br>/|\\<br>/ \\",
}
let guesscount = 1;
let falsecount = 0;
man.innerHTML = hangmanart[falsecount];
const wordlist = [ "parrot", "carrot", "strawberry", "apple", "map", "human", "doctor", "call of duty", "lahmacun", "forest", "player",
"music", "microphone", "television", "telephone", "private", "special", "tree", "son", "sun", "kiwi", "young",
"Leader", "hang man", "old", "new", "brain", "surgery", "delicious", "congratulations"];
const choosenword = wordlist[Math.floor(Math.random() * wordlist.length)];
let theline = [];
let guessedwords = ["Guessed words:"];
let guessedletters = ["Guessed letters:"];
function setGame(){
for (let i = 0; i < choosenword.length; i++){
if (choosenword[i] === " "){
theline.push("-");
}
else {
theline.push("_");
}
}
line.textContent = theline.join(" ");
}
function chooseBackgroundTheme(lightstyle, darkstyle){
if (window.localStorage.getItem("theme") === "lightstyle.css"){
document.body.style.backgroundColor = lightstyle;
}
else if (window.localStorage.getItem("theme") === "darkstyle.css"){
document.body.style.backgroundColor = darkstyle;
}
}
function winfonts(){
chooseBackgroundTheme("#46871", "#96cc76");
message.style.color = "#4f9642";
line.style.color = "#4f9642";
man.innerHTML = hangmanart[7];
line.textContent = "You found the word '" + choosenword + "' at " + guesscount + ". try!";
message.textContent = "YOU WIN!";
document.body.style.transition = "0.5s";
guess.disabled = true;
guessbutton.disabled = true;
}
function losefonts(){
man.innerHTML = hangmanart[6];
message.textContent = "YOU LOSE!";
line.textContent = "You could not find the word '" + choosenword + "'!";
line.style.color = "#f27474";
chooseBackgroundTheme("#eba19b", "#734b48");
document.body.style.transition = "0.5s";
guess.disabled = true;
guessbutton.disabled = true;
}
function dogamelogic(){
if (guess.value.length == 1){
if (theline.includes(guess.value)){
message.textContent = "The letter already in the word!";
}
else if (guessedletters.includes(guess.value)){
message.textContent = "You already guessed the letter!";
}
else{
let founded = false;
for (let i = 0; i < choosenword.length; i++){
if (guess.value === choosenword[i]){
theline[i] = guess.value;
founded = true;
}
}
line.textContent = theline.join(" ");
if (founded){
chooseBackgroundTheme("#caffab", "#7fa16c")
document.body.style.transition = "0.5s";
}
else{
falsecount++;
man.innerHTML = hangmanart[falsecount];
guessedletters.push(guess.value);
chooseBackgroundTheme("#ffb0ab", "#9c6764");
document.body.style.transition = "0.5s";
}
message.textContent = "";
}
guesscount++;
}
else if (guess.value.length > 1){
if (guessedwords.includes(guess.value)){
message.textContent = "You already guessed the word!";
}
else {
if (guess.value === choosenword){
winfonts();
}
else{
falsecount++;
man.innerHTML = hangmanart[falsecount];
guessedwords.push(guess.value);
message.textContent = "";
chooseBackgroundTheme("#ffb0ab", "#9c6764");
document.body.style.transition = "0.5s";
guesscount++;
}
}
}
guess.value = "";
gletters.textContent = guessedletters.join(" ");
gwords.textContent = guessedwords.join(" ");
if (falsecount == 6 || !theline.includes("_")){
losefonts();
}
}
guess.addEventListener("keydown", function(event) {
if (event.key === "Enter") {
dogamelogic();
}
});
setGame();
guessbutton.onclick = function(){dogamelogic()}