Skip to content

Commit 0e1214c

Browse files
committed
Bug fixes on the game
1 parent 962915d commit 0e1214c

File tree

4 files changed

+77
-48
lines changed

4 files changed

+77
-48
lines changed

New/hangman.js

Lines changed: 51 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function hide_keyboard_letters(hangman_var)
236236
var let_guess = hangman_var['curgame']['letter_guessed'];
237237
for (lett in let_guess)
238238
{
239-
document.getElementById("let_"+lett).className += " hideletter";
239+
document.getElementById("let_"+lett).className = "hideletter";
240240
}
241241
}
242242
//function that shows all the letters in the keyboard
@@ -252,6 +252,18 @@ function show_keyboard_letters()
252252
}
253253
}
254254

255+
//function that checks if a letter has been already used
256+
function is_alredy_guessed_letter(letter, hangman_var)
257+
{
258+
var let_guess = hangman_var['curgame']['letter_guessed'];
259+
for (var guessed in let_guess)
260+
{
261+
if (guessed == letter)
262+
return true;
263+
}
264+
return false;
265+
}
266+
255267
//function that shos the hangman image given the current guesses remaining and the difficulty level
256268
function show_img(hangman_var)
257269
{
@@ -286,7 +298,7 @@ function show_img(hangman_var)
286298
{
287299
var oImg=document.createElement("img");
288300
oImg.setAttribute('src', 'img/'+img_to_show);
289-
oImg.setAttribute('alt', ('Remaining chances: '+rem_chances));
301+
oImg.setAttribute('alt', ('Image for remaining chances: '+rem_chances));
290302
oImg.setAttribute('height', '150');
291303
oImg.setAttribute('width', '75');
292304
var imgID = document.getElementById("cur_img");
@@ -345,47 +357,50 @@ function start_new_game()
345357
//function that guesses a letter
346358
function guess_letter(lett)
347359
{
348-
if (HANGMAN['curgame']['rem_chances'] > 0)
360+
if (! is_alredy_guessed_letter(lett, HANGMAN))
349361
{
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)
362+
if (HANGMAN['curgame']['rem_chances'] > 0)
364363
{
365-
//check if all the word has been revealed
366-
if (is_word_complete(HANGMAN))
364+
//I update the status of the game if necessary
365+
if (!HANGMAN['curgame']['game_started'])
366+
HANGMAN['curgame']['game_started'] = true;
367+
368+
//In any case I add the leter to the global variable
369+
HANGMAN['curgame']['letter_guessed'][lett] = true;
370+
371+
//I get the word to find
372+
var word = HANGMAN['curgame']['word'];
373+
//I check if there are some instances of the current letter in the word
374+
var positions = letter_in_word(lett, word);
375+
//If I have at least one position, then I found a letter in the word
376+
//and I have to check if all the word has been revealed
377+
if (positions.length >0)
367378
{
368-
//I update the stats
369-
HANGMAN['played'] = HANGMAN['played'] + 1;
370-
HANGMAN['won'] = HANGMAN['won'] + 1;
371-
HANGMAN['curgame']['game_status'] = 'won';
379+
//check if all the word has been revealed
380+
if (is_word_complete(HANGMAN))
381+
{
382+
//I update the stats
383+
HANGMAN['played'] = HANGMAN['played'] + 1;
384+
HANGMAN['won'] = HANGMAN['won'] + 1;
385+
HANGMAN['curgame']['game_status'] = 'won';
386+
}
372387
}
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)
388+
//Otherwise I have to reduce the future changes and check if this was the last one!
389+
else
379390
{
380-
//I update the stats
381-
HANGMAN['played'] += 1;
382-
HANGMAN['curgame']['game_status'] = 'lost';
391+
HANGMAN['curgame']['rem_chances'] = HANGMAN['curgame']['rem_chances'] -1;
392+
if (HANGMAN['curgame']['rem_chances'] == 0)
393+
{
394+
//I update the stats
395+
HANGMAN['played'] += 1;
396+
HANGMAN['curgame']['game_status'] = 'lost';
397+
}
383398
}
399+
//I save the new variable in the localStorage
400+
save_to_localstorage('HANGMAN', HANGMAN);
401+
//and I show the current situation
402+
show_cur_game(HANGMAN);
384403
}
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);
389404
}
390405
}
391406

New/hangman_desktop.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
</div>
2727
<div id="setup_scores">
2828
<ul class="letters">
29+
<li class="newgame" onclick="start_new_game();">New game</li>
2930
<li id="level_easy" onclick="change_level('easy');">Easy</li>
3031
<li id="level_medium" onclick="change_level('medium');">Medium</li>
3132
<li id="level_hard" onclick="change_level('hard');">Hard</li>
32-
<li class="newgame" onclick="start_new_game();">New game</li>
3333
</ul>
3434
<ul class="scores">
3535
<li>Games Played: <span id="game_played">0</span></li>
@@ -77,7 +77,6 @@
7777
</ul>
7878
</div>
7979
</div>
80-
<!-- <script type="text/javascript" src="jquery-1.6.4.min.js"></script> -->
8180
<script type="text/javascript" src="hangman.js"></script>
8281
</body>
8382
</html>

New/hangman_mobile.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
<h1>Hangman</h1>
2020
<div id="container">
2121
<div id="game">
22-
<div id="hangman_img"></div>
22+
<div id="hangman_img">
23+
<div id="cur_img"></div>
24+
<div id="rem_chances"></div>
25+
</div>
2326
<div id="setup_scores">
2427
<ul class="letters">
25-
<li class="newgame">New game</li>
28+
<li class="newgame" onclick="start_new_game();">New game</li>
2629
<li id="level_easy" onclick="change_level('easy');">Easy</li>
2730
<li id="level_medium" onclick="change_level('medium');">Medium</li>
2831
<li id="level_hard" onclick="change_level('hard');">Hard</li>
@@ -41,23 +44,34 @@
4144
</div>
4245
<div id="keyboard">
4346
<ul class="letters">
44-
<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>
45-
<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>
47+
<li id="let_A" onclick="guess_letter('A');">A</li><li id="let_B" onclick="guess_letter('B');">B</li>
48+
<li id="let_C" onclick="guess_letter('C');">C</li><li id="let_D" onclick="guess_letter('D');">D</li>
49+
<li id="let_E" onclick="guess_letter('E');">E</li><li id="let_F" onclick="guess_letter('F');">F</li>
50+
<li id="let_G" onclick="guess_letter('G');">G</li><li id="let_H" onclick="guess_letter('H');">H</li>
51+
<li id="let_I" onclick="guess_letter('I');">I</li><li id="let_J" onclick="guess_letter('J');">J</li>
52+
<li id="let_K" onclick="guess_letter('K');">K</li><li id="let_L" onclick="guess_letter('L');">L</li>
53+
<li id="let_M" onclick="guess_letter('M');">M</li><li id="let_N" onclick="guess_letter('N');">N</li>
54+
<li id="let_O" onclick="guess_letter('O');">O</li><li id="let_P" onclick="guess_letter('P');">P</li>
55+
<li id="let_Q" onclick="guess_letter('Q');">Q</li><li id="let_R" onclick="guess_letter('R');">R</li>
56+
<li id="let_S" onclick="guess_letter('S');">S</li><li id="let_T" onclick="guess_letter('T');">T</li>
57+
<li id="let_U" onclick="guess_letter('U');">U</li><li id="let_V" onclick="guess_letter('V');">V</li>
58+
<li id="let_W" onclick="guess_letter('W');">W</li><li id="let_X" onclick="guess_letter('X');">X</li>
59+
<li id="let_Y" onclick="guess_letter('Y');">Y</li><li id="let_Z" onclick="guess_letter('Z');">Z</li>
4660
</ul>
4761
</div>
4862
</div>
4963
<div id="help">
5064
<h2>How to play</h2>
5165
<ul>
66+
<li>If you want select the difficulty level. This sets the number of wrong attempts before hanging to 3, 6, or 12</li>
5267
<li>Press New Game</li>
53-
<li>Select the difficulty level. This sets the number of wrong attempts before hanging to 3, 6, or 12</li>
54-
<li>The game begins when you select your first character</li>
68+
<li>The game begins when you select your first character.</li>
5569
<li>You cannot change the difficulty level once the game has started until the &quot;New Game&quot; button is pressed</li>
5670
</ul>
5771
</div>
5872
<div id="footer">
5973
<ul>
60-
<li><a href="<?php echo $path_parts['dirname']?>/hangman.php?vpa=m">View mobile version of this website</a></li>
74+
<li><a href="<?php echo $path_parts['dirname']?>/hangman.php?vpa=d">View desktop version of this website</a></li>
6175
<li><a href="http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../games/hangman/hangman1.htm">Visit the website that inspired this page</a></li>
6276
</ul>
6377
</div>

New/style.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@
108108
}
109109

110110
ul.letters li.hideletter {
111-
display:none;
111+
background-color:grey;
112+
cursor:auto;
112113
}
113114

114115
#help ul {
@@ -120,9 +121,9 @@
120121
width:auto;
121122
}
122123
#setup_scores ul.letters li.newgame {
124+
float:none;
123125
clear: both;
124-
padding-left:2em;
125-
padding-right:2em;
126+
width: 7em;
126127
}
127128

128129
ul.scores {

0 commit comments

Comments
 (0)