Skip to content

Commit 0961ece

Browse files
committed
basic js
1 parent 06d0942 commit 0961ece

File tree

9 files changed

+816
-18
lines changed

9 files changed

+816
-18
lines changed

New/CHOICES.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
1- Hangman: based on http://www.huntingground.freeserve.co.uk/main/mainfram.htm?../games/hangman/hangman1.htm
22
2- Web application, no widget
3-
3- Use different images,
3+
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+
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
File renamed without changes.
File renamed without changes.
File renamed without changes.

New/hangman.js

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/**
2+
* Functions to play hangman
3+
*/
4+
5+
//First of all I remove the content of the word section (so the user doesn't see the error)
6+
document.getElementById('guessword').innerHTML = '';
7+
8+
//function that returns a random word
9+
function getWord() {
10+
var a = new Array('abate','aberrant','abscond','accolade','acerbic','acumen','adulation','adulterate','aesthetic','aggrandize','alacrity','alchemy','amalgamate','ameliorate','amenable','anachronism','anomaly','approbation','archaic','arduous','ascetic','assuage','astringent','audacious','austere','avarice','aver','axiom','bolster','bombast','bombastic','bucolic','burgeon','cacophony','canon','canonical','capricious','castigation','catalyst','caustic','censure','chary','chicanery','cogent','complaisance','connoisseur','contentious','contrite','convention','convoluted','credulous','culpable','cynicism','dearth','decorum','demur','derision','desiccate','diatribe','didactic','dilettante','disabuse','discordant','discretion','disinterested','disparage','disparate','dissemble','divulge','dogmatic','ebullience','eccentric','eclectic','effrontery','elegy','eloquent','emollient','empirical','endemic','enervate','enigmatic','ennui','ephemeral','equivocate','erudite','esoteric','eulogy','evanescent','exacerbate','exculpate','exigent','exonerate','extemporaneous','facetious','fallacy','fawn','fervent','filibuster','flout','fortuitous','fulminate','furtive','garrulous','germane','glib','grandiloquence','gregarious','hackneyed','halcyon','harangue','hedonism','hegemony','heretical','hubris','hyperbole','iconoclast','idolatrous','imminent','immutable','impassive','impecunious','imperturbable','impetuous','implacable','impunity','inchoate','incipient','indifferent','inert','infelicitous','ingenuous','inimical','innocuous','insipid','intractable','intransigent','intrepid','inured','inveigle','irascible','laconic','laud','loquacious','lucid','luminous','magnanimity','malevolent','malleable','martial','maverick','mendacity','mercurial','meticulous','misanthrope','mitigate','mollify','morose','mundane','nebulous','neologism','neophyte','noxious','obdurate','obfuscate','obsequious','obstinate','obtuse','obviate','occlude','odious','onerous','opaque','opprobrium','oscillation','ostentatious','paean','parody','pedagogy','pedantic','penurious','penury','perennial','perfidy','perfunctory','pernicious','perspicacious','peruse','pervade','pervasive','phlegmatic','pine','pious','pirate','pith','pithy','placate','platitude','plethora','plummet','polemical','pragmatic','prattle','precipitate','precursor','predilection','preen','prescience','presumptuous','prevaricate','pristine','probity','proclivity','prodigal','prodigious','profligate','profuse','proliferate','prolific','propensity','prosaic','pungent','putrefy','quaff','qualm','querulous','query','quiescence','quixotic','quotidian','rancorous','rarefy','recalcitrant','recant','recondite','redoubtable','refulgent','refute','relegate','renege','repudiate','rescind','reticent','reverent','rhetoric','salubrious','sanction','satire','sedulous','shard','solicitous','solvent','soporific','sordid','sparse','specious','spendthrift','sporadic','spurious','squalid','squander','static','stoic','stupefy','stymie','subpoena','subtle','succinct','superfluous','supplant','surfeit','synthesis','tacit','tenacity','terse','tirade','torpid','torque','tortuous','tout','transient','trenchant','truculent','ubiquitous','unfeigned','untenable','urbane','vacillate','variegated','veracity','vexation','vigilant','vilify','virulent','viscous','vituperate','volatile','voracious','waver','zealous');
11+
return a[parseInt(Math.random()* a.length)];
12+
}
13+
14+
15+
//I check if there is JSON otherwise I load it
16+
if (typeof JSON == 'undefined')
17+
{
18+
var headID = document.getElementsByTagName("head")[0];
19+
var newScript = document.createElement('script');
20+
newScript.type = 'text/javascript';
21+
newScript.src='json2.js';
22+
headID.appendChild(newScript);
23+
}
24+
25+
//Global variable that stores the current status of the game
26+
var HANGMAN;
27+
28+
//function that saves to local storage when it is available, otherwise nothing happens
29+
function save_to_localstorage(key, value)
30+
{
31+
if (Modernizr.localstorage)
32+
{
33+
localStorage.setItem(key, JSON.stringify(value));
34+
}
35+
}
36+
37+
//function that initializes the hangman variable for the first time
38+
function initialize_hangman()
39+
{
40+
HANGMAN = new Object();
41+
HANGMAN['played'] = 0;
42+
HANGMAN['won'] = 0;
43+
HANGMAN['level'] = 'medium';
44+
HANGMAN['curgame'] = new Object();
45+
return HANGMAN;
46+
}
47+
//function that initialize a new game
48+
function new_hangman_game(hangman_status_var)
49+
{
50+
hangman_status_var['curgame'] = new Object();
51+
hangman_status_var['curgame']['word'] = getWord();
52+
if (hangman_status_var['level'] == 'easy')
53+
{
54+
hangman_status_var['curgame']['rem_chances'] = 12;
55+
}
56+
else if (hangman_status_var['level'] == 'medium')
57+
{
58+
hangman_status_var['curgame']['rem_chances'] = 6;
59+
}
60+
else if (hangman_status_var['level'] == 'hard')
61+
{
62+
hangman_status_var['curgame']['rem_chances'] = 3;
63+
}
64+
hangman_status_var['curgame']['game_started'] = false;
65+
hangman_status_var['curgame']['letter_guessed'] = new Object();
66+
return hangman_status_var;
67+
}
68+
69+
//function that shows which level I have
70+
function show_set_level(level)
71+
{
72+
if (level == 'easy')
73+
{
74+
//I set the background color with another class
75+
document.getElementById("level_easy").className += " levelset";
76+
//I remove the background color from the others
77+
document.getElementById("level_medium").className =
78+
document.getElementById("level_medium").className.replace
79+
( /(?:^|\s)levelset(?!\S)/ , '' );
80+
document.getElementById("level_hard").className =
81+
document.getElementById("level_hard").className.replace
82+
( /(?:^|\s)levelset(?!\S)/ , '' );
83+
}
84+
else if (level == 'medium')
85+
{
86+
//I set the background color with another class
87+
document.getElementById("level_easy").className =
88+
document.getElementById("level_easy").className.replace
89+
( /(?:^|\s)levelset(?!\S)/ , '' );
90+
document.getElementById("level_medium").className += " levelset";
91+
document.getElementById("level_hard").className =
92+
document.getElementById("level_hard").className.replace
93+
( /(?:^|\s)levelset(?!\S)/ , '' );
94+
}
95+
else if (level == 'hard')
96+
{
97+
//I set the background color with another class
98+
document.getElementById("level_easy").className =
99+
document.getElementById("level_easy").className.replace
100+
( /(?:^|\s)levelset(?!\S)/ , '' );
101+
document.getElementById("level_medium").className =
102+
document.getElementById("level_medium").className.replace
103+
( /(?:^|\s)levelset(?!\S)/ , '' );
104+
document.getElementById("level_hard").className += " levelset";
105+
}
106+
}
107+
108+
//function that changes the level of the game if the game is not started
109+
function change_level(level)
110+
{
111+
if (!HANGMAN['curgame']['game_started'])
112+
{
113+
if (level == 'easy')
114+
{
115+
HANGMAN['level'] = 'easy';
116+
HANGMAN['curgame']['rem_chances'] = 12;
117+
}
118+
else if (level == 'medium')
119+
{
120+
HANGMAN['level'] = 'medium';
121+
HANGMAN['curgame']['rem_chances'] = 6;
122+
}
123+
else if (level == 'hard')
124+
{
125+
HANGMAN['level'] = 'hard';
126+
HANGMAN['curgame']['rem_chances'] = 3;
127+
}
128+
}
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']);
133+
}
134+
//function that shows the statistics
135+
function show_statistics(hangman_var)
136+
{
137+
//I show the number of games played
138+
document.getElementById("game_played").innerHTML = hangman_var['played'];
139+
//and the game won
140+
document.getElementById("game_won").innerHTML = hangman_var['won'];
141+
}
142+
143+
144+
145+
//////////////////
146+
// MAIN
147+
//////////////////
148+
//then I actually initialize the game
149+
if (Modernizr.localstorage)
150+
{
151+
//I try to get previous saved infos
152+
try
153+
{
154+
HANGMAN = JSON.parse(localStorage.getItem('HANGMAN'));
155+
}
156+
catch(err)
157+
{
158+
HANGMAN == null;
159+
}
160+
161+
//if HANGMAN is null then I need to initialaze he variables
162+
if (HANGMAN == null)
163+
{
164+
HANGMAN = initialize_hangman();
165+
HANGMAN = new_hangman_game(HANGMAN);
166+
localStorage.setItem('HANGMAN', JSON.stringify(HANGMAN));
167+
}
168+
}
169+
else
170+
{
171+
// 'Sadly, there is no support for Web Storage or cookies that we could use to emulate it.';
172+
HANGMAN = initialize_hangman();
173+
HANGMAN = new_hangman_game(HANGMAN);
174+
}
175+
176+
//I show the set of the level
177+
show_set_level(HANGMAN['level']);
178+
//I show the statistics
179+
show_statistics(HANGMAN);

New/hangman_desktop.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,32 @@
2020
<h1>Hangman</h1>
2121
<div id="container">
2222
<div id="game">
23-
23+
<div id="hangman_img"></div>
24+
<div id="setup_scores">
25+
<ul class="letters">
26+
<li class="newgame">New game</li>
27+
<li id="level_easy" onclick="change_level('easy');">Easy</li>
28+
<li id="level_medium" onclick="change_level('medium');">Medium</li>
29+
<li id="level_hard" onclick="change_level('hard');">Hard</li>
30+
</ul>
31+
<ul class="scores">
32+
<li>Games Played: <span id="game_played">0</span></li>
33+
<li>Games Won: <span id="game_won">0</span></li>
34+
</ul>
35+
</div>
36+
<div id="words">
37+
<span id="guessword">
38+
If you see this text for more than 10 seconds, your browser doesn't
39+
support javascript and you cannot play :-( <br/>
40+
Please visit this page with a browser that supports javascript.
41+
</span>
42+
</div>
43+
<div id="keyboard">
44+
<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>
47+
</ul>
48+
</div>
2449
</div>
2550
<div id="help">
2651
<h2>How to play</h2>
@@ -33,21 +58,12 @@
3358
</div>
3459
<div id="footer">
3560
<ul>
36-
<li><a href="<?php echo $path_parts['dirname']?>/hangman.php?vpa=m">Visit mobile version of this website</a></li>
61+
<li><a href="<?php echo $path_parts['dirname']?>/hangman.php?vpa=m">View mobile version of this website</a></li>
3762
<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>
3863
</ul>
3964
</div>
4065
</div>
41-
<script type="text/javascript">
42-
var msg;
43-
44-
if (Modernizr.localstorage) {
45-
msg = 'Success! Web Storage is available either through native support or cookies.';
46-
} else {
47-
msg = 'Sadly, there is no native support for Web Storage';
48-
}
49-
50-
document.getElementById('game').innerHTML = msg;
51-
</script>
66+
<!-- <script type="text/javascript" src="jquery-1.6.4.min.js"></script> -->
67+
<script type="text/javascript" src="hangman.js"></script>
5268
</body>
5369
</html>

0 commit comments

Comments
 (0)