-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblob.js
More file actions
162 lines (134 loc) · 4.19 KB
/
blob.js
File metadata and controls
162 lines (134 loc) · 4.19 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
$(document).ready(function() {
var possibleWords = ("jazz rhythm blob mouse moose qwerty quoth word wyvern " +
"rigged tune town twofold yggdrasil yucky urgent spleen ultra iodine itchy " +
"octopus ominous pencil porcupine aardvark apple sultan sung down derp duffle " +
"fungus friend future ghost geist geese goose hungry hugged huge jax juke "+
"lump loose kingdoms knotted kneesock zero zeus zebra cowboy xenophobia " +
"axalotle buxomly coexist xenon dewaxed klaxons xenopus vexed agenized " +
"vulture ventriloquism belittle bemuse nimble neche mulch mystify ghastly " +
"kilt your york spork corvette communique plebiscite rapprochement mince" +
"impressionism facade balustrade terrace casserole confit mustard mayonnaise " +
"caramel custard fondant fondue marmalade meringue mauve appeasement beige " +
"carmine maroon violet vermilion turquoise").split(" ");
var word = possibleWords[Math.floor(Math.random() * (possibleWords.length + 1))];
//console.log("the word is not " + word); //for troubleshooting
var lettersUsed = [];
var shownWord = function(){
return word.split("").map(function(letter){
return (lettersUsed.indexOf(letter) > -1) ? letter : "_";
});
};
var printWord = function(){
$("#word").text(shownWord().join(" "));
};
var blinkScreen = function(){
$("body").fadeOut(100).fadeIn(100);
};
var endGame = function(message){
$("div").animate({
width: ['toggle', 'swing']
});
$("#end").text(message);
$("#sub-end").text("Would you like to play again? (y/n)");
$("div").animate({
width: ['toggle', 'swing']
});
$("html").keypress(function(key) {
key = String.fromCharCode(key.which);
if (key === "y") {
location.reload(true);;
} else {
$("#end").text("'y' not?");
}
});
};
var win = function(condition){
return endGame("You win!");
};
var lose = function(){
blinkScreen();
endGame("Sorry, you lost.");
blinkScreen();
};
var tryLetter = function(letter){
return word.split("").indexOf(letter) > -1 ? false : true;
};
var letterTyped = function(letter){
acceptableChars = "abcdefghijklmnopqrstuvwxyz".split("");
if (lettersUsed.indexOf(letter) > -1 || acceptableChars.indexOf(letter) < 0) {
blinkScreen();
return false;
} else {
lettersUsed.push(letter);
return tryLetter(letter);
}
};
var initialize = function(){
difficulty = 0;
printWord();
for (var i = 0; i <= difficulty; i++){
hang(i);
}
var idx = difficulty + 1;
$("html").keypress(function(key) {
key = String.fromCharCode(key.which);
if (idx === 9){
hang(idx);
return lose();
}
else if (letterTyped(key)){
hang(idx);
idx++;
}
else if (word === shownWord().join("")) {
printWord();
return win();
}
else {
printWord();
}
});
};
var hang = function(lineNumber) {
var canvas = document.getElementById("hangman");
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
if (lineNumber === 0){
ctx.strokeStyle = "rgb(200,0,0)";
ctx.beginPath();
ctx.moveTo(100,100);
ctx.lineTo(0,100);
ctx.stroke();
} else if (lineNumber === 1) {
ctx.lineTo(0,0);
ctx.stroke();
} else if (lineNumber === 2) {
ctx.lineTo(50,0);
ctx.stroke();
} else if (lineNumber === 3) {
ctx.lineTo(50,15);
ctx.stroke();
} else if (lineNumber === 4) {
ctx.arcTo(20, 5, 20, 30, 20);
ctx.stroke();
} else if (lineNumber === 5) {
ctx.quadraticCurveTo(40,50,20,75);
ctx.stroke();
} else if (lineNumber === 6) {
ctx.bezierCurveTo(76,100,40,62.5, 50, 50);
ctx.stroke();
} else if (lineNumber === 7) {
ctx.bezierCurveTo(100,150,70,10, 75, 75);
ctx.stroke();
} else if (lineNumber === 8) {
ctx.bezierCurveTo(100,10,20,70, 50, 15);
ctx.stroke();
} else if (lineNumber === 9) {
ctx.moveTo(40,25);
ctx.fillStyle = "rgb(200,0,170)";
ctx.fill();
}
}
};
initialize();
});