-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHangmanGame.html
More file actions
191 lines (175 loc) · 7.21 KB
/
HangmanGame.html
File metadata and controls
191 lines (175 loc) · 7.21 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hangman Game</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<style>
body {
background-color: #2f2f2f;
color: #f4f4f4;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
overflow: hidden;
}
.container {
background-color: #FED172;
padding: 2em;
border-radius: 1em;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
text-align: center;
width: 100%;
max-width: 550px;
}
canvas {
border: 2px solid #BB3414;
margin-top: 20px;
display: block;
margin-left: 30px;
}
.text{
color:#BB3414 ;
}
.btn{
background-color:#BB3414 ;
color: white;
}
.btn:hover{
background-color:#3f1005 ;
}
.color{
color: #BB3414;
}
</style>
</head>
<body>
<div class="container">
<h3 class="text">Select one Category</h3>
<div id="options-container" class="mb-3"></div>
<div id="user-input-section" class="fs-2 my-3 color"></div>
<canvas id="canvas" width="400" height="150"></canvas>
<div id="letter-container" class="d-flex flex-wrap justify-content-center gap-2 mt-3"></div>
<div id="attempts" class="mt-3 text-danger"></div>
<div id="result-popup" class="alert alert-dark mt-3 d-none"></div>
<button id="new-game-button" class="btn mt-3">Reset Game</button>
</div>
<script>
const letterContainer = document.getElementById("letter-container");
const optionsContainer = document.getElementById("options-container");
const userInputSection = document.getElementById("user-input-section");
const resultPopup = document.getElementById("result-popup");
const newGameButton = document.getElementById("new-game-button");
const canvas = document.getElementById("canvas");
const attemptsDisplay = document.getElementById("attempts");
let options = {
plants: ["Rose", "Tulip", "Bamboo", "Cactus", "Fern", "Orchid"],
animals: ["Hedgehog", "Rhinoceros", "Squirrel", "Panther"],
countries: ["India", "Hungary", "Switzerland", "Zimbabwe"]
};
let chosenWord = "";
let winCount = 0;
let count = 0;
let drawingFunctions;
const displayOptions = () => {
optionsContainer.innerHTML = '';
for (let key in options) {
optionsContainer.innerHTML += `<button class='btn m-2' onclick="generateWord('${key}')">${key}</button>`;
}
};
const generateWord = (option) => {
chosenWord = options[option][Math.floor(Math.random() * options[option].length)].toUpperCase();
userInputSection.innerHTML = chosenWord.replace(/./g, '<span class="dashes">_</span>');
optionsContainer.innerHTML = '';
createLetterButtons();
};
const createLetterButtons = () => {
letterContainer.innerHTML = '';
for (let i = 65; i < 91; i++) {
let button = document.createElement("button");
button.classList.add("btn", "btn-light", "m-1");
button.innerText = String.fromCharCode(i);
button.addEventListener("click", function () {
let charArray = chosenWord.split("");
let dashes = document.getElementsByClassName("dashes");
if (charArray.includes(button.innerText)) {
charArray.forEach((char, index) => {
if (char === button.innerText) {
dashes[index].innerText = char;
winCount += 1;
if (winCount == charArray.length) {
resultPopup.classList.remove("d-none");
resultPopup.innerHTML = `You Win! The word was <b>${chosenWord}</b>`;
}
}
});
} else {
count += 1;
drawingFunctions.drawMan(count);
attemptsDisplay.innerText = `Incorrect Attempts: ${count}`;
if (count == 6) {
resultPopup.classList.remove("d-none");
resultPopup.innerHTML = `You are Out! The word was <b>${chosenWord}</b>`;
}
}
button.disabled = true;
});
letterContainer.append(button);
}
};
const initializer = () => {
winCount = 0;
count = 0;
userInputSection.innerHTML = "";
optionsContainer.innerHTML = "";
resultPopup.classList.add("d-none");
letterContainer.innerHTML = "";
attemptsDisplay.innerText = "";
displayOptions();
drawingFunctions = canvasCreator();
drawingFunctions.initialDrawing();
};
const canvasCreator = () => {
let context = canvas.getContext("2d");
context.beginPath();
context.strokeStyle = "#BB3414";
context.lineWidth = 2;
const drawLine = (fromX, fromY, toX, toY) => {
context.moveTo(fromX, fromY);
context.lineTo(toX, toY);
context.stroke();
};
const head = () => {
context.beginPath();
context.arc(70, 30, 10, 0, Math.PI * 2, true);
context.stroke();
};
const body = () => { drawLine(70, 40, 70, 80); };
const leftArm = () => { drawLine(70, 50, 50, 70); };
const rightArm = () => { drawLine(70, 50, 90, 70); };
const leftLeg = () => { drawLine(70, 80, 50, 110); };
const rightLeg = () => { drawLine(70, 80, 90, 110); };
const initialDrawing = () => {
context.clearRect(0, 0, canvas.width, canvas.height);
drawLine(10, 130, 130, 130); // Base
drawLine(10, 10, 10, 131); // Left Pole
drawLine(10, 10, 70, 10); // Top Bar
drawLine(70, 10, 70, 20); // Rope
};
const drawMan = (step) => {
if (step === 1) head();
if (step === 2) body();
if (step === 3) leftArm();
if (step === 4) rightArm();
if (step === 5) leftLeg();
if (step === 6) rightLeg();
};
return { initialDrawing, drawMan };
};
newGameButton.addEventListener("click", initializer);
window.onload = initializer;
</script>
</body>
</html>