Skip to content

Commit 962915d

Browse files
committed
Desktop working
1 parent 0961ece commit 962915d

File tree

5 files changed

+354
-28
lines changed

5 files changed

+354
-28
lines changed

New/CHOICES.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
1- Hangman: based on http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../games/hangman/hangman1.htm
1+
1- Hangman: images and idea based on http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../games/hangman/hangman1.htm
2+
All the code has been rewritten
23
2- Web application, no widget
34
3- Use different images,
4-
4- if local and session storage are not available, the game works only for the current session, otherwise it remembers everything
5+
4- if local and session storage are not available, the game remeber the statistics only for the current session,
6+
otherwise it remembers everything
57
5- if native JSON is not available, I load a specific class
6-
6- I decided to use jQuery because of the heavy use of javascript and CSS change made by javascript

New/hangman.js

Lines changed: 265 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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
135140
function 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)hideletter(?!\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
//////////////////
@@ -177,3 +424,5 @@ else
177424
show_set_level(HANGMAN['level']);
178425
//I show the statistics
179426
show_statistics(HANGMAN);
427+
//I show the game (new o recovered)
428+
show_cur_game(HANGMAN);

New/hangman_desktop.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@
2020
<h1>Hangman</h1>
2121
<div id="container">
2222
<div id="game">
23-
<div id="hangman_img"></div>
23+
<div id="hangman_img">
24+
<div id="cur_img"></div>
25+
<div id="rem_chances"></div>
26+
</div>
2427
<div id="setup_scores">
2528
<ul class="letters">
26-
<li class="newgame">New game</li>
2729
<li id="level_easy" onclick="change_level('easy');">Easy</li>
2830
<li id="level_medium" onclick="change_level('medium');">Medium</li>
2931
<li id="level_hard" onclick="change_level('hard');">Hard</li>
32+
<li class="newgame" onclick="start_new_game();">New game</li>
3033
</ul>
3134
<ul class="scores">
3235
<li>Games Played: <span id="game_played">0</span></li>
@@ -42,17 +45,28 @@
4245
</div>
4346
<div id="keyboard">
4447
<ul class="letters">
45-
<li>A</li><li>B</li><li>C</li><li>D</li><li>E</li><li>F</li><li>G</li><li>H</li><li>I</li><li>J</li><li>K</li><li>L</li><li>M</li>
46-
<li>N</li><li>O</li><li>P</li><li>Q</li><li>R</li><li>S</li><li>T</li><li>U</li><li>V</li><li>W</li><li>X</li><li>Y</li><li>Z</li>
48+
<li id="let_A" onclick="guess_letter('A');">A</li><li id="let_B" onclick="guess_letter('B');">B</li>
49+
<li id="let_C" onclick="guess_letter('C');">C</li><li id="let_D" onclick="guess_letter('D');">D</li>
50+
<li id="let_E" onclick="guess_letter('E');">E</li><li id="let_F" onclick="guess_letter('F');">F</li>
51+
<li id="let_G" onclick="guess_letter('G');">G</li><li id="let_H" onclick="guess_letter('H');">H</li>
52+
<li id="let_I" onclick="guess_letter('I');">I</li><li id="let_J" onclick="guess_letter('J');">J</li>
53+
<li id="let_K" onclick="guess_letter('K');">K</li><li id="let_L" onclick="guess_letter('L');">L</li>
54+
<li id="let_M" onclick="guess_letter('M');">M</li><li id="let_N" onclick="guess_letter('N');">N</li>
55+
<li id="let_O" onclick="guess_letter('O');">O</li><li id="let_P" onclick="guess_letter('P');">P</li>
56+
<li id="let_Q" onclick="guess_letter('Q');">Q</li><li id="let_R" onclick="guess_letter('R');">R</li>
57+
<li id="let_S" onclick="guess_letter('S');">S</li><li id="let_T" onclick="guess_letter('T');">T</li>
58+
<li id="let_U" onclick="guess_letter('U');">U</li><li id="let_V" onclick="guess_letter('V');">V</li>
59+
<li id="let_W" onclick="guess_letter('W');">W</li><li id="let_X" onclick="guess_letter('X');">X</li>
60+
<li id="let_Y" onclick="guess_letter('Y');">Y</li><li id="let_Z" onclick="guess_letter('Z');">Z</li>
4761
</ul>
4862
</div>
4963
</div>
5064
<div id="help">
5165
<h2>How to play</h2>
5266
<ul>
67+
<li>If you want select the difficulty level. This sets the number of wrong attempts before hanging to 3, 6, or 12</li>
5368
<li>Press New Game</li>
54-
<li>Select the difficulty level. This sets the number of wrong attempts before hanging to 3, 6, or 12</li>
55-
<li>The game begins when you select your first character</li>
69+
<li>The game begins when you select your first character.</li>
5670
<li>You cannot change the difficulty level once the game has started until the &quot;New Game&quot; button is pressed</li>
5771
</ul>
5872
</div>

0 commit comments

Comments
 (0)