forked from meticulo3366/infinite_hangman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameLogic.js
402 lines (373 loc) · 13.1 KB
/
gameLogic.js
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
Game Logic was based on this Hangman example
https://github.com/samwhitley/svg-hangman.git
Except I added additional logic layers
*/
'use strict';
var letters = { 'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0 }
var solution = "";
var currentAnswer = "";
var wins = 0;
var loses = 0;
var submissions = 0;
var count = 0;
var _random = function (n) {
return Math.floor(Math.random() * n);
};
var _pickWord = function () {
var index = _random(words.length);
var currentWord = words[index];
return currentWord;
};
var _initCurrent = function () {
var solutionLength = solution.length,
str = "";
for (var i = 0; i < solutionLength; i++) {
if (solution.charAt(i) >= "a" && solution.charAt(i) <= "z") {
str += "_";
} else {
str += solution.charAt(i);
}
}
return str;
};
exports.removeCurrentWord = function() {
for (var i = 0; i < words.length; i++) {
if (solution === words[i]) {
words.splice(i, 1);
}
}
};
exports.inSolution = function (ltr) {
if (solution.indexOf(ltr) > -1) {
return true;
}
else {
return false;
}
};
exports.checkKey = function(ltr){
submissions++;
if (letters[ltr] == 0) {
letters[ltr] = 1
return true
} else {
return false;
};
};
exports.setCurrent = function (ltr) {
var solutionLength = solution.length;
var isCharNotInWord = true;
for (var i = 0; i < solutionLength; i++) {
if (ltr === solution.charAt(i)) {
currentAnswer = currentAnswer.substring(0,i) + ltr + currentAnswer.substring(i + 1);
isCharNotInWord = false;
}
}
if (isCharNotInWord == true) {
count = count + 1;
}
};
exports.isLost = function(){
var isGameLost = count >= 9;
if (isGameLost == true){
loses++;
};
return isGameLost
}
exports.getAttempts = function(){
return count;
}
exports.getStats = function(){
//create stats object
var stats = {}
stats.wins = wins
stats.loses = loses
stats.submissions = submissions
return stats
}
exports.newPuzzle = function () {
solution = _pickWord();
currentAnswer = _initCurrent();
count = 0;
letters = { 'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0 };
};
exports.isWon = function () {
var isGameWon = solution === currentAnswer;
if (isGameWon == true){
wins++;
};
return isGameWon
};
exports.getSolution = function () {
return solution;
};
exports.getCurrent = function () {
return currentAnswer;
};
exports.getRemainingWords = function () {
return words.length;
};
// play one letter
exports.playOnce = function(ltr){
}
var words = [
"a chain is only as strong as its weakest link",
"a change is as good as a rest",
"a drop in the bucket",
"a finger in many pies",
"a friend in need is a friend indeed",
"a good head on his shoulders",
"a good man is hard to find",
"a head for heights",
"a healthy mind in a healthy body",
"a heart of gold",
"a horse a horse my kingdom for a horse",
"a house divided against itself cannot stand",
"a kick in the teeth",
"a leopard cannot change its spots",
"a little knowledge is a dangerous thing",
"a little bit of what you fancy does you good",
"a penny saved is a penny earned",
"a person is known by the company he keeps",
"a picture paints a thousand words",
"a place for everything and everything in its place",
"a problem shared is a problem halved",
"a rose by any other name would smell as sweet",
"a stitch in time saves nine",
"a thing of beauty is a joy forever",
"a watched pot never boils",
"absence makes the heart grow fonder",
"actions speak louder than words",
"all work and no play makes Jack a dull boy",
"all is well that ends well",
"an apple a day keeps the doctor away",
"an army marches on its stomach",
"an Englishman is home is his castle",
"april is the cruellest month",
"as dead as a dodo",
"as plain as the nose on your face",
"as pleased as punch",
"ask no questions and hear no lies",
"attack is the best form of defence",
"banging your head against a brick wall",
"barking dogs seldom bite",
"beauty is in the eye of the beholder",
"beauty is only skin deep",
"beggars cannot be choosers",
"behind every great man there is a great woman",
"better late than never",
"better safe than sorry",
"better the devil you know than the devil you do not",
"better to have loved and lost than never to have loved at all",
"beware the ides of march",
"birds of a feather flock together",
"bite the hand that feeds you",
"blessed are the peacemakers",
"blood is thicker than water",
"born with a silver spoon in his mouth",
"bury your head in the sand",
"by the skin of your teeth",
"by the sweat of his brow",
"can not make head nor tail of it",
"cannot see beyond the end of your nose",
"caught with your hand in the till",
"charity begins at home",
"cleanliness is next to godliness",
"countenance more in sorrow than in anger",
"cross my palm with silver",
"cut off your nose to spite your face",
"cut your coat to suit your cloth",
"discretion is the better part of valour",
"do as you would be done by",
"do not bite the hand that feeds you",
"do not count your chickens before they are hatched",
"do not look a gift horse in the mouth",
"do not put all your eggs in one basket",
"doubt is the beginning not the end of wisdom",
"easy come easy go",
"eat drink and be merry",
"enough is as good as a feast",
"every cloud has a silver lining",
"every dog has his day",
"everybody wants to go to heaven but nobody wants to die",
"fail to plan then plan to fail",
"faint heart never won fair lady",
"faith will move mountains",
"familiarity breeds contempt",
"fifteen men on a dead mans chest",
"finders keepers losers weepers",
"fish and guests smell after three days",
"fools rush in where angels fear to tread",
"for everything there is a season",
"forewarned is forearmed",
"fortune favours the brave",
"get off on the right foot",
"get out of the wrong side of the bed",
"get the bit between your teeth",
"god helps those who help themselves",
"good fences make good neighbours",
"good things come to those who wait",
"great minds think alike",
"handsome is as handsome does",
"hard work never did anyone any harm",
"he wears his heart on his sleeve",
"he who can does he who cannot teaches",
"he who laughs last laughs longest",
"he who lives by the sword shall die by the sword",
"he who pays the piper calls the tune",
"hell has no fury like a woman scorned",
"hoist by your own petard",
"holding your cards close to your chest",
"home is where the heart is",
"if a job is worth doing it is worth doing well",
"if at first you do not succeed try try and try again",
"if music be the food of love play on",
"if wishes were horses beggars would ride",
"if you cannot beat them join them",
"if you cannot stand the heat get out of the kitchen",
"imitation is the sincerest form of flattery",
"in the kingdom of the blind the oneeyed man is king",
"it is better to give than to receive",
"it is better to light a candle than curse the darkness",
"it is better to travel hopefully than to arrive",
"jokers laugh today for you will not laugh tomorrow",
"keep your ear to the ground",
"keep your eye on the ball",
"keep your finger on the pulse",
"keep your head above water",
"keep your nose to the grindstone",
"keep your shoulder to the wheel",
"laugh and the world laughs with you weep and you weep alone",
"laughter is the best medicine",
"let bygones be bygones",
"let not the sun go down on your wrath",
"let sleeping dogs lie",
"let the punishment fit the crime",
"life begins at forty",
"life is what you make it",
"lightening never strikes twice in the same place",
"live for today for tomorrow never comes",
"love is blind",
"love of money is the root of all evil",
"love thy neighbour as thyself",
"make love not war",
"man does not live by bread alone",
"many are called but few are chosen",
"marry in haste repent at leisure",
"mighty oaks from little acorns grow",
"misery loves company",
"money does not grow on trees",
"money makes the world go round",
"more haste less speed",
"more honoured in the breach than in the observance",
"more power to your elbow",
"more than meets the eye",
"nail your colours to the mast",
"nature abhors a vacuum",
"necessity is the mother of invention",
"never judge a book by its cover",
"never put off until tomorrow what you can do today",
"no man is an island",
"no rest for the wicked",
"nothing is certain but death and taxes",
"old soldiers never die they just fade away",
"once more unto the breach dear friends once more",
"one foot in the grave",
"one good turn deserves another",
"only fools and horses work",
"opportunity only knocks once",
"out of sight out of mind",
"out of the mouths of babes and sucklings",
"people who live in glass houses should not throw stones",
"physician heal thyself",
"possession is nine tenths of the law",
"power corrupts absolute power corrupts absolutely",
"practise makes perfect",
"pride comes before a fall",
"procrastination is the thief of time",
"put your best foot forward",
"put your head on the block",
"put your money where your mouth is",
"quick quick quick today makes slow slow slow tomorrow",
"red sky at night shepherds delight",
"red sky in the morning shepherds warning",
"religion is the opium of the people",
"revenge is a dish best served cold",
"rome was not built in a day",
"set your heart on something",
"shaken not stirred",
"shoot yourself in the foot",
"shuffle off this mortal coil",
"sick to the stomach",
"smiling from ear to ear",
"smiling like a cheshire cat",
"smooth runs the water where the brook is deep",
"spare the rod and spoil the child",
"speak softly and carry a big stick",
"stick your neck out",
"still waters run deep",
"stupid is as stupid does",
"take the bit between your teeth",
"talk is cheap",
"tear your hair out",
"that which does not kill us makes us stronger",
"the black sheep of the family",
"the blind leading the blind",
"the boot is on the other foot",
"the boy is father to the man",
"the course of true love never did run smooth",
"the darkest hour is just before the dawn",
"the devil finds work for idle hands to do",
"the die has been cast",
"the early bird catches the worm",
"the hand is quicker than the eye",
"the hand that rocks the cradle rules the world",
"the longest journey starts with a single step",
"the more things change the more they stay the same",
"the pen is mightier than sword",
"the proof of the pudding is in the eating",
"the salt of the earth",
"the writing is on the wall",
"there are none so blind as those that will not see",
"there is always more fish in the sea",
"there is method in my madness",
"there is more than one way to skin a cat",
"there is no fool like an old fool",
"there is no smoke without fire",
"there is no such thing as a free lunch",
"those who do not learn from history are doomed to repeat it",
"time and tide wait for no man",
"to err is human to forgive divine",
"too many cooks spoil the broth",
"two heads are better then one",
"uneasy lies the head that wears a crown",
"very little is achieved by he who risks nothing",
"wash your mouth out with soap and water",
"waste not want not",
"wear your heart on your sleeve",
"when the cat is away the mice will play",
"where there is a will there is a way",
"you can it make an omelette without breaking eggs",
"you can it teach an old dog new tricks",
"you can it judge a book by its cover",
"youth is wasted on the young",
"transmission control protocol",
"file transfer protocol",
"hypertext markup language",
"extensible markup language",
"cascading style sheets",
"hypertext transfer protocol",
"document object model",
"javascript object notation",
"scalable vector graphics",
"uniform resource locator",
"secure sockets layer",
"domain name system",
"representational state transfer",
"simple object access protocol",
"simple mail transfer protocol",
"structured query language",
"world wide web",
"secure shell"
]