This repository has been archived by the owner on Nov 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
148 lines (134 loc) · 4.21 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Hangman</title>
<!-- Bootstrap core CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous">
<!-- Custom styles for this template -->
<link href="./assets/style.css" rel="stylesheet">
</head>
<body>
<div class="container">
<main role="main">
<div class="jumbotron">
<h1 class="display-3">Hangman</h1>
<div class="row">
<div class="col">
<canvas id="canvas" width="300" height="300">
Sorry, your browser doesn't support the <canvas> element.
</canvas>
</div>
<div class="col">
<p class="lead" id="theWord"></p>
<p class="lead" id="failedLetters"></p>
<hr>
<p><a onclick="startGame()" class="btn btn-lg btn-success" href="#" role="button">New Game</a></p>
</div>
</div>
</div>
</main>
</div> <!-- /container -->
<script>
var mysteryWord = "initial";
var knownWord = [];
var failedLetters = [];
var tries = 6;
var c = document.getElementById("canvas");
var ctx = c.getContext("2d");
// Setup hangman drawing
// base
drawLine(10, 250, 100, 250);
// rope
drawLine(100, 10, 100, 30);
// pole
drawLine(10, 10, 10, 250);
// top
drawLine(10, 10, 150, 10);
// When user clicks New Game button
function startGame() {
ctx.clearRect(15,29,220,220);
tries = 6;
knownWord = [];
failedLetters = [];
document.getElementById('failedLetters').innerHTML = failedLetters;
mysteryWord = prompt("Enter the word you want people to guess");
if(mysteryWord !== null || mysteryWord !== "") {
// Creates the '_' spaces for user to fill in
for (var i = 0; i < mysteryWord.length; i++) {
knownWord.push('_');
}
updateLabel();
}
}
function updateLabel() {
document.getElementById("theWord").innerHTML = knownWord.join(' ');
}
// Primary game logic
document.onkeyup = function(event) {
if(knownWord.includes('_') && tries > 0) {
var letter = String.fromCharCode(event.keyCode).toLowerCase();
if(wordContains(letter)) {
updateLabel();
} else if (!failedLetters.includes(letter)){
failedLetters.push(letter);
tries--;
updateHangman(tries);
document.getElementById('failedLetters').innerHTML = failedLetters;
}
}
}
function updateHangman(tries) {
switch (tries) {
case 5:
// head
ctx.beginPath();
ctx.arc(100, 65, 35, 0, 2 * Math.PI);
ctx.stroke();
break;
case 4:
// body
drawLine(100, 100, 100, 180);
break;
case 3:
// left leg
drawLine(100, 180, 70, 230);
break;
case 2:
// right leg
drawLine(100, 180, 130, 230);
break;
case 1:
// left arm
drawLine(100, 100, 70, 150);
break;
case 0:
// right arm
drawLine(100, 100, 130, 150);
break;
}
}
// x and y is where to begin, x2 y2 is where to stop drawing
function drawLine(x, y, x2, y2) {
ctx.moveTo(x,y);
ctx.lineTo(x2,y2);
ctx.stroke();
}
function wordContains(char) {
var containsLetterFlag = false
for (var i = 0; i < mysteryWord.length; i++) {
// if submitted char is same as mysteryWord[i]
if (char === mysteryWord.charAt(i)) {
// fill knownWord slot with the char
knownWord[i] = char;
containsLetterFlag = true;
}
}
return containsLetterFlag;
}
</script>
</body>
</html>