-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.js
189 lines (166 loc) · 5.61 KB
/
hangman.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
var newGame; //initialize game
var secretWord; //word entered by user
var secretLen; //word length
var isWord; //function to check for & reject anything not a-z
var letterChoice; //hides & diables letter after it has been guessed
var guessCheck; //checks each guess against the secret word for matches
var missCount = 0; //running count of the number of incorrect guesses
var solCheck; //checks the solution grid after each turn
var startPlay; //starts game: calls isWord, makeBlanks, etc
var resetPlay; //re-sets variables for new game
var revealLetter; //reveals letters in solution grid
var buildScaffold; //updates scaffold picture based on count of incorrect guesses
var makeBlanks; //builds 'blank' spaces for letters in secret word
var lost=false; //checks for current loss
var won=false; //checks for current win
var opt = document.getElementsByClassName('opt'); //opt class (array of letter buttons)
var enableLetters; //enables and un-hides all letter buttons
$(document).ready(function(){
newGame();
});
//array variable for pictures of scaffold
var pics =
['https://cloud.githubusercontent.com/assets/8210523/6953499/b94c87da-d897-11e4-9982-3f10fad1e897.gif', 'https://cloud.githubusercontent.com/assets/8210523/6953511/c78898fc-d897-11e4-8275-9eb4b8ca3285.gif',
'https://cloud.githubusercontent.com/assets/8210523/6953512/c788cc0a-d897-11e4-8368-62d8eae60e2d.gif', 'https://cloud.githubusercontent.com/assets/8210523/6953514/c78b45ca-d897-11e4-8d12-8ff17e34fe72.gif',
'https://cloud.githubusercontent.com/assets/8210523/6953516/c78c371e-d897-11e4-9593-05e6e7b46062.gif', 'https://cloud.githubusercontent.com/assets/8210523/6953515/c78b57cc-d897-11e4-8153-7c1d6dd9bf21.gif',
'https://cloud.githubusercontent.com/assets/8210523/6953513/c78af7a0-d897-11e4-86c7-b99b55402127.gif', 'https://cloud.githubusercontent.com/assets/8210523/6953518/c7941164-d897-11e4-8f53-99ef9926adf9.gif',
'https://cloud.githubusercontent.com/assets/8210523/6953517/c7932a10-d897-11e4-9353-204622e1d001.gif', 'https://cloud.githubusercontent.com/assets/8210523/6953519/c794a070-d897-11e4-9a7d-b1dc29421e7a.gif',
'https://cloud.githubusercontent.com/assets/8210523/6953520/c7952662-d897-11e4-9785-0441d3ada7d4.gif'];
newGame = function(){
document.getElementById('secret').focus();
document.getElementById('play').disabled=false;
document.getElementById('play').onclick = function(){
startPlay();
};
enableLetters();
//add onclick to each letter (opt class)
for(i=0;i<26;i++){
opt[i].onclick = function(event){
//check that we have a secret word
if(secretLen===0 || secretLen===undefined){
alert('Please enter a secret word to guess!')
}
else{
letterChoice(event);
}
}
};
document.getElementById('restart').onclick = function(){
resetPlay();
};
};
letterChoice = function(event){
var letter = event.target.textContent;
var letterClass = event.target.className;
if (won==false && lost==false){
if(letterClass.search('guessed')<0){
event.target.className = event.target.className + ' guessed';
event.target.disabled = true;
guessCheck(letter);
}
}else{
alert('Click \'New Game\' below to play again!');
}
};
guessCheck = function(letter){
console.log(letter);
console.log(secretWord + ' ' + secretWord.search(letter));
document.getElementById('secret').focus();
if(secretWord.search(letter)>=0){
revealLetter(letter);
winCheck();
}
else{
missCount = missCount + 1;
buildScaffold();
loseCheck();
}
};
loseCheck = function(){
if(missCount==10){
alert('You lose!! The word was ' + secretWord);
lost = true;
}
};
winCheck = function (){
var correctCount = 0;
for(i=0;i<secretLen;i++){
idStr = 'a' + (i+1);
var guessClass = document.getElementById(idStr).className;
if(guessClass.search('correct')>=0){
correctCount = correctCount+1;
};
}
if(correctCount==secretLen){
won = true;
alert('You win!!');
}
}
revealLetter = function(letter){
var correctArray = [];
for(i=0;i<secretLen;i++){
if(secretWord[i]==letter){
correctArray.push(i);
}
};
console.log(correctArray);
for(i=0;i<correctArray.length;i++){
var reveal = correctArray[i] + 1;
var revealID = 'a' + reveal;
//broken
document.getElementById(revealID).className = 'letter correct';
}
};
buildScaffold = function(){
document.getElementById('boardpic').src=pics[missCount];
};
startPlay = function(){
secretWord = document.getElementById('secret').value.toUpperCase();
secretLen = secretWord.length;
document.getElementById('play').disabled=true;
isWord();
makeBlanks();
if (secretLen==0){
alert('Please enter a secret word to guess!');
resetPlay();
}
};
makeBlanks = function(){
var gridStr = '';
for(i=0;i<secretLen;i++){
gridStr = gridStr + '<td class=\'blank letter\' id=\'a' + (i+1) + '\'>' + secretWord.charAt(i) + '</td>';
};
document.getElementById('solution').innerHTML = '<tr>' + gridStr + '</tr>';
};
isWord = function(){
var nonAlph = /[^a-z]/i;
check = secretWord.match(nonAlph);
if(check){
confirm('Please use only letters in your secret word!');
resetPlay();
};
};
resetPlay = function(){
document.getElementById('secret').value='';
document.getElementById('solution').innerHTML='';
lost=false;
won=false;
missCount=0;
document.getElementById('boardpic').src=pics[0];
document.getElementById('play').disabled=false;
enableLetters();
};
enableLetters = function(){
for(i=0;i<26;i++){
opt[i].className = 'opt';
opt[i].disabled=false;
};
}
/*
TO DO:
-Kill the $(document).ready jQuery?
-Make letters non-existant until they are guessed
-Enable multiple words (allow spaces, add as a different class in solution grid)
-Arrow keys for letter selection
-On enter key event to trigger startPlay
*/