@@ -45,25 +45,28 @@ function initialize_hangman()
4545 return HANGMAN ;
4646}
4747//function that initialize a new game
48- function new_hangman_game ( hangman_status_var )
48+ function new_hangman_game ( hangman_var )
4949{
50- hangman_status_var [ 'curgame' ] = new Object ( ) ;
51- hangman_status_var [ 'curgame' ] [ 'word' ] = getWord ( ) ;
52- if ( hangman_status_var [ 'level' ] == 'easy' )
50+ hangman_var [ 'curgame' ] = new Object ( ) ;
51+ var newword = getWord ( ) ;
52+ hangman_var [ 'curgame' ] [ 'word' ] = newword . toUpperCase ( ) ;
53+ if ( hangman_var [ 'level' ] == 'easy' )
5354 {
54- hangman_status_var [ 'curgame' ] [ 'rem_chances' ] = 12 ;
55+ hangman_var [ 'curgame' ] [ 'rem_chances' ] = 12 ;
5556 }
56- else if ( hangman_status_var [ 'level' ] == 'medium' )
57+ else if ( hangman_var [ 'level' ] == 'medium' )
5758 {
58- hangman_status_var [ 'curgame' ] [ 'rem_chances' ] = 6 ;
59+ hangman_var [ 'curgame' ] [ 'rem_chances' ] = 6 ;
5960 }
60- else if ( hangman_status_var [ 'level' ] == 'hard' )
61+ else if ( hangman_var [ 'level' ] == 'hard' )
6162 {
62- hangman_status_var [ 'curgame' ] [ 'rem_chances' ] = 3 ;
63+ hangman_var [ 'curgame' ] [ 'rem_chances' ] = 3 ;
6364 }
64- hangman_status_var [ 'curgame' ] [ 'game_started' ] = false ;
65- hangman_status_var [ 'curgame' ] [ 'letter_guessed' ] = new Object ( ) ;
66- return hangman_status_var ;
65+ hangman_var [ 'curgame' ] [ 'game_started' ] = false ;
66+ hangman_var [ 'curgame' ] [ 'game_status' ] = 'play' ;
67+ hangman_var [ 'curgame' ] [ 'letter_guessed' ] = new Object ( ) ;
68+ //hangman_var['curgame']['letter_guessed']['A'] = true;
69+ return hangman_var ;
6770}
6871
6972//function that shows which level I have
@@ -125,11 +128,13 @@ function change_level(level)
125128 HANGMAN [ 'level' ] = 'hard' ;
126129 HANGMAN [ 'curgame' ] [ 'rem_chances' ] = 3 ;
127130 }
131+
132+ //I save the change to the local Storage (if possible)
133+ save_to_localstorage ( 'HANGMAN' , HANGMAN ) ;
134+ //finally I show the new level
135+ show_set_level ( HANGMAN [ 'level' ] ) ;
136+ show_cur_game ( HANGMAN ) ;
128137 }
129- //I save the change to the local Storage (if possible)
130- save_to_localstorage ( 'HANGMAN' , HANGMAN ) ;
131- //finally I show the new level
132- show_set_level ( HANGMAN [ 'level' ] ) ;
133138}
134139//function that shows the statistics
135140function show_statistics ( hangman_var )
@@ -140,8 +145,250 @@ function show_statistics(hangman_var)
140145 document . getElementById ( "game_won" ) . innerHTML = hangman_var [ 'won' ] ;
141146}
142147
148+ //function that checks if a letter is in a word and in case returns an array of positions
149+ function letter_in_word ( letter , word )
150+ {
151+ var positions = new Array ( ) ;
152+ for ( var i = 0 ; i < word . length ; i ++ )
153+ {
154+ var wl = word . charAt ( i ) ;
155+ if ( wl == letter )
156+ positions . push ( i ) ;
157+ }
158+ return positions ;
159+ }
160+
161+ //function that checks the letters in the word
162+ function check_word ( hangman_var )
163+ {
164+ //I take the word
165+ var word = hangman_var [ 'curgame' ] [ 'word' ] ;
166+ //I create a new variable with underscore of the same lenght of the word
167+ var word_underscore = new Array ( ) ;
168+ for ( var i = 0 ; i < word . length ; i ++ )
169+ word_underscore . push ( '_ ' ) ;
170+ //I take the letters already guessed
171+ var let_guess = hangman_var [ 'curgame' ] [ 'letter_guessed' ] ;
172+ //for each letter I check if it is in the word and in case I reveal it
173+ for ( lett in let_guess )
174+ {
175+ var positions = letter_in_word ( lett , word ) ;
176+ for ( var x = 0 ; x < positions . length ; x ++ )
177+ {
178+ var cur_pos = positions [ x ] ;
179+ word_underscore [ cur_pos ] = lett + ' ' ;
180+ }
181+ }
182+ return word_underscore ;
183+ }
184+
185+ //function that actually shows the word
186+ function show_word ( hangman_var )
187+ {
188+ //I check the word
189+ var word_underscore = check_word ( hangman_var ) ;
190+
191+ //before inserting I re-create the string
192+ str_underscore = '' ;
193+ for ( var z in word_underscore )
194+ str_underscore += word_underscore [ z ] ;
195+ //Finally I insert the new word inside the final destination
196+ document . getElementById ( "guessword" ) . innerHTML = str_underscore ;
197+ }
198+
199+ //function that checks if the word is complete
200+ function is_word_complete ( hangman_var )
201+ {
202+ //I check the word
203+ var word_underscore = check_word ( hangman_var ) ;
204+ //and I control if there are underscore remaining
205+ for ( var z in word_underscore )
206+ {
207+ if ( word_underscore [ z ] == '_ ' )
208+ return false ;
209+ }
210+ return true ;
211+ }
212+ //function that return the complete word
213+ function reveal_complete_word ( hangman_var )
214+ {
215+ //I take the word
216+ var word = hangman_var [ 'curgame' ] [ 'word' ] ;
217+ //I create a new variable
218+ var wsplit = new Array ( ) ;
219+ for ( var i = 0 ; i < word . length ; i ++ )
220+ {
221+ var wl = word . charAt ( i ) ;
222+ wsplit . push ( wl + ' ' ) ;
223+ }
224+ //before inserting I re-create the string
225+ str_revealed = '' ;
226+ for ( var z in wsplit )
227+ str_revealed += wsplit [ z ] ;
228+ //Finally I insert the new word inside the final destination
229+ document . getElementById ( "guessword" ) . innerHTML = str_revealed ;
230+ }
231+
232+ //function that hides in the keyboard the letters already guessed
233+ function hide_keyboard_letters ( hangman_var )
234+ {
235+ //I take the letters already guessed
236+ var let_guess = hangman_var [ 'curgame' ] [ 'letter_guessed' ] ;
237+ for ( lett in let_guess )
238+ {
239+ document . getElementById ( "let_" + lett ) . className += " hideletter" ;
240+ }
241+ }
242+ //function that shows all the letters in the keyboard
243+ function show_keyboard_letters ( )
244+ {
245+ var alphabet = new Array ( 'A' , 'B' , 'C' , 'D' , 'E' , 'F' , 'G' , 'H' , 'I' , 'J' , 'K' , 'L' , 'M' , 'N' , 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' , 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z' ) ;
246+ for ( var x in alphabet )
247+ {
248+ var lett = alphabet [ x ] ;
249+ document . getElementById ( "let_" + lett ) . className =
250+ document . getElementById ( "let_" + lett ) . className . replace
251+ ( / (?: ^ | \s ) h i d e l e t t e r (? ! \S ) / , '' ) ;
252+ }
253+ }
254+
255+ //function that shos the hangman image given the current guesses remaining and the difficulty level
256+ function show_img ( hangman_var )
257+ {
258+ var images_per_level = new Object ;
259+ images_per_level [ 'hard' ] = new Array ( 'gallows.gif' , 'man1_2.gif' , 'man1_1.gif' , 'man1_0.gif' ) ;
260+ images_per_level [ 'medium' ] = new Array ( 'gallows.gif' , 'man1_5.gif' , 'man1_4.gif' , 'man1_3.gif' , 'man1_2.gif' , 'man1_1.gif' , 'man1_0.gif' ) ;
261+ images_per_level [ 'easy' ] = new Array ( '' , 'gallow1.gif' , 'gallow2.gif' , 'gallow3.gif' , 'gallow4.gif' , 'gallow5.gif' , 'gallows.gif' , 'man1_5.gif' , 'man1_4.gif' , 'man1_3.gif' , 'man1_2.gif' , 'man1_1.gif' , 'man1_0.gif' ) ;
262+
263+ var level = hangman_var [ 'level' ] ;
264+ var rem_chances = hangman_var [ 'curgame' ] [ 'rem_chances' ] ;
265+ var img_to_show = '' ;
266+ if ( level == 'easy' )
267+ {
268+ var id = 12 - rem_chances ;
269+ img_to_show = images_per_level [ 'easy' ] [ id ] ;
270+ }
271+ else if ( level == 'medium' )
272+ {
273+ var id = 6 - rem_chances ;
274+ img_to_show = images_per_level [ 'medium' ] [ id ] ;
275+ }
276+ else if ( level == 'hard' )
277+ {
278+ var id = 3 - rem_chances ;
279+ img_to_show = images_per_level [ 'hard' ] [ id ] ;
280+ }
281+
282+ //finally I show the image
283+ //first I remove the previous one
284+ document . getElementById ( "cur_img" ) . innerHTML = '' ;
285+ if ( img_to_show != '' )
286+ {
287+ var oImg = document . createElement ( "img" ) ;
288+ oImg . setAttribute ( 'src' , 'img/' + img_to_show ) ;
289+ oImg . setAttribute ( 'alt' , ( 'Remaining chances: ' + rem_chances ) ) ;
290+ oImg . setAttribute ( 'height' , '150' ) ;
291+ oImg . setAttribute ( 'width' , '75' ) ;
292+ var imgID = document . getElementById ( "cur_img" ) ;
293+ imgID . appendChild ( oImg ) ;
294+ }
295+ }
296+
297+ //function that actually starts or recover a game
298+ function show_cur_game ( hangman_var )
299+ {
300+ if ( HANGMAN [ 'curgame' ] [ 'game_status' ] == 'play' )
301+ {
302+ //I load the current attempts left
303+ document . getElementById ( "rem_chances" ) . innerHTML = 'Remaining chances: ' + hangman_var [ 'curgame' ] [ 'rem_chances' ] ;
304+ //I show the word
305+ show_word ( hangman_var ) ;
306+ }
307+ else if ( HANGMAN [ 'curgame' ] [ 'game_status' ] == 'won' )
308+ {
309+ //I write the correct status
310+ document . getElementById ( "rem_chances" ) . innerHTML = '<span class="endgame">YOU WON!</span>' ;
311+ //I show the word
312+ show_word ( hangman_var ) ;
313+ //I show the new statistics
314+ show_statistics ( hangman_var ) ;
315+ }
316+ else if ( HANGMAN [ 'curgame' ] [ 'game_status' ] == 'lost' )
317+ {
318+ //I write the correct status
319+ document . getElementById ( "rem_chances" ) . innerHTML = '<span class="endgame">YOU LOST!</span>' ;
320+ //I reveal the word
321+ reveal_complete_word ( hangman_var ) ;
322+ //I show the new statistics
323+ show_statistics ( hangman_var ) ;
324+ }
325+ //I hide the letters that I cannot click any more
326+ hide_keyboard_letters ( hangman_var ) ;
327+ //I show the right image
328+ show_img ( hangman_var ) ;
329+ }
330+
331+ //function that force the start of a new game
332+ function start_new_game ( )
333+ {
334+ //I initialize a new game
335+ HANGMAN = new_hangman_game ( HANGMAN ) ;
336+ //I save the new variable in the localStorage
337+ save_to_localstorage ( 'HANGMAN' , HANGMAN ) ;
338+ //I show all the keyboard again
339+ show_keyboard_letters ( ) ;
340+ //I actually show the game
341+ show_cur_game ( HANGMAN ) ;
342+ }
143343
144344
345+ //function that guesses a letter
346+ function guess_letter ( lett )
347+ {
348+ if ( HANGMAN [ 'curgame' ] [ 'rem_chances' ] > 0 )
349+ {
350+ //I update the status of the game if necessary
351+ if ( ! HANGMAN [ 'curgame' ] [ 'game_started' ] )
352+ HANGMAN [ 'curgame' ] [ 'game_started' ] = true ;
353+
354+ //In any case I add the leter to the global variable
355+ HANGMAN [ 'curgame' ] [ 'letter_guessed' ] [ lett ] = true ;
356+
357+ //I get the word to find
358+ var word = HANGMAN [ 'curgame' ] [ 'word' ] ;
359+ //I check if there are some instances of the current letter in the word
360+ var positions = letter_in_word ( lett , word ) ;
361+ //If I have at least one position, then I found a letter in the word
362+ //and I have to check if all the word has been revealed
363+ if ( positions . length > 0 )
364+ {
365+ //check if all the word has been revealed
366+ if ( is_word_complete ( HANGMAN ) )
367+ {
368+ //I update the stats
369+ HANGMAN [ 'played' ] = HANGMAN [ 'played' ] + 1 ;
370+ HANGMAN [ 'won' ] = HANGMAN [ 'won' ] + 1 ;
371+ HANGMAN [ 'curgame' ] [ 'game_status' ] = 'won' ;
372+ }
373+ }
374+ //Otherwise I have to reduce the future changes and check if this was the last one!
375+ else
376+ {
377+ HANGMAN [ 'curgame' ] [ 'rem_chances' ] = HANGMAN [ 'curgame' ] [ 'rem_chances' ] - 1 ;
378+ if ( HANGMAN [ 'curgame' ] [ 'rem_chances' ] == 0 )
379+ {
380+ //I update the stats
381+ HANGMAN [ 'played' ] += 1 ;
382+ HANGMAN [ 'curgame' ] [ 'game_status' ] = 'lost' ;
383+ }
384+ }
385+ //I save the new variable in the localStorage
386+ save_to_localstorage ( 'HANGMAN' , HANGMAN ) ;
387+ //and I show the current situation
388+ show_cur_game ( HANGMAN ) ;
389+ }
390+ }
391+
145392//////////////////
146393// MAIN
147394//////////////////
177424show_set_level ( HANGMAN [ 'level' ] ) ;
178425//I show the statistics
179426show_statistics ( HANGMAN ) ;
427+ //I show the game (new o recovered)
428+ show_cur_game ( HANGMAN ) ;
0 commit comments