Connecting Tech Pros Worldwide Help | Site Map

Hang man

  #1  
Old November 2nd, 2007, 09:11 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <style>
  3. .hangman {
  4.     font-face: tahoma;
  5.     font-size: 15pt;
  6.     color: white;  
  7.     TEXT-DECORATION: underline;
  8. }
  9. .letters {
  10.     font-face: tahoma;
  11.     font-size: 10pt;
  12.     color: white;  
  13. }
  14. .subject {
  15.     font-face: tahoma;
  16.     font-size: 12pt;
  17.     color: gold;  
  18. }
  19. .spacer {
  20.     font-face: tahome;
  21.     font-size: 14pt;
  22. }
  23. </style>
  24. <body bgcolor="black">
  25. <script>
  26. var Phrases = ["The quick brown fox jumps over the lazy dog", "Chuck", "Mc donalds", "Oragel", "Javascript"]; //set the answers
  27. var Subjects = ["Names", "Names", "Places", "Medicine", "Scripting Language"]; //set the subjects
  28. var misses = 0; //start off with 0 misses
  29. window.onload = doload; //load everything
  30.  
  31. function doload() {
  32.     reset(); //reset all default values
  33.     setLetters(); //set the letters back to normal
  34.     setPhrase(); //put the phrase and subject up on screen
  35. }
  36.  
  37. function setPhrase() {
  38.     var ph = random(0, Phrases.length-1); //get a random phrase
  39.     var phrase = Phrases[ph];
  40.     document.getElementById("subject").innerHTML = "Subject: " + Subjects[ph];
  41.     var tbody = document.getElementById("hangman");
  42.     var tr = document.createElement("TR"); //create the phrase on screen in cells
  43.     for (i=0; i<phrase.length; i++) {
  44.         if (document.all) { //Make internet explorer hack (it doesn't like dom elements created and using getelementbyname)
  45.             var td = document.createElement("<TD name=\"phrase\" id=\"phrase\">"); //IE's hacked way of creating elements so you can grab them via .getElementsByTagName
  46.         } else {
  47.             var td = document.createElement("TD");
  48.             td.setAttribute("name", "phrase"); //set the element name
  49.             td.id="phrase"+random(0, 100000); //set the element id hopefully unique.
  50.         }
  51.         if (phrase.substring(i, i+1)==" ") { tdClass = "spacer"; } else { tdClass = "hangman"; } //set the classname to be used
  52.         td.className=tdClass; //actually give the element the class
  53.         td.setAttribute("letter", phrase.substring(i, i+1)); //custom attribute (some people don't like custom attributes) I use them all the time without any problems.
  54.         td.innerHTML =  "&nbsp;&nbsp;&nbsp;"; //Give it some spaces so the underline is decent sized
  55.         tr.appendChild(td); //append the cell to the row
  56.     }
  57.     tbody.appendChild(tr); //append the row to the table body
  58. }
  59.  
  60. function random(min, max) {
  61.     //Just a function for making random numbers more easily.
  62.     random_num = Math.round((Math.random()*max)+min)
  63.     return random_num
  64. }
  65.  
  66. function reset() {
  67.     misses = 0; //reset misses to 0
  68.     setImage(); //reset the hang man image
  69.     document.getElementById("used_letters").innerHTML = ""; //reset the used letters
  70.     document.getElementById("letters").innerHTML = document.getElementById("main").innerHTML; //reset the letters available
  71.     var tbody = document.getElementById("hangman");
  72.     while (tbody.childNodes[0]) { //remove last phrase
  73.         tbody.removeChild(tbody.childNodes[0]); //This removes all the children of tbody so its a clean slate again.
  74.     }
  75. }
  76.  
  77. function setLetters() {
  78.     var val=""; //set a blank val
  79.     var letters = document.getElementById("letters").innerHTML; //get the available letters
  80.     letters = letters.split(" "); //split it up by space
  81.     for (i=0; i<letters.length; i++) {
  82.             val+="<span onclick=\"tryLetter('"+letters[i]+"');\" style=\"cursor: pointer;\">"+letters[i]+"</span>&nbsp;"; //this is just creating a span of each letter so you can click the letters instead of typing.
  83.     }
  84.     document.getElementById("avail_letters").innerHTML = val; //sets the inner html of the avail letters to the new.
  85. }
  86.  
  87. function tryLetter(letter) {
  88.     var letters = document.getElementById("letters").innerHTML;
  89.     var phrase = document.getElementsByName("phrase");
  90.     var cLetters = "";
  91.     var correct = false;
  92.     letters = letters.split(" ");
  93.     for (i=0; i<letters.length; i++) {
  94.         if (letters[i].toLowerCase()!=letter.toLowerCase()) { cLetters+=letters[i]+" "; } //simple compairson of letters if the letter does not match then it adds it back to the letters avail area.
  95.     }
  96.     document.getElementById("letters").innerHTML = cLetters;
  97.     if (document.getElementById("used_letters").innerHTML.indexOf(letter)>=0) { alert("this letter was already used!"); return false; }
  98.     setLetters();
  99.     count = 0;
  100.     for (i=0; i<phrase.length; i++) {
  101.         if (phrase[i].getAttribute("letter").toLowerCase()==letter.toLowerCase()) { phrase[i].innerHTML = letter; correct = true; }
  102.         if (phrase[i].innerHTML!="&nbsp;&nbsp;&nbsp;" || phrase[i].className=="spacer") { count++; }
  103.     }
  104.     if (count==phrase.length) { alert("Yay you won!"); doload(); return false; } //check for win
  105.     if (!correct) { misses++; var fail = setImage(); }
  106.     if (fail) { return false; } //return false if it fails
  107.     var used = document.getElementById("used_letters").innerHTML + " " + letter
  108.     document.getElementById("used_letters").innerHTML = used;
  109. }
  110.  
  111. function setImage() {
  112.     document.getElementById("game").innerHTML = "<img src=\"images/"+Math.floor(misses+1)+".gif\">";
  113.     if (misses==6) { alert("You Lost!!!!"); doload(); return false; } else { return true; }
  114. }
  115.  
  116. </script>
  117. <table border="0" width="100%">
  118.     <tr><td width="500" id="game"><img src="images/1.gif"></td>
  119.     <td>
  120.     <table>
  121.         <tbody id="hangman">
  122.             <tr><td class="hangman">&nbsp;&nbsp;&nbsp;</td><td class="hangman">&nbsp;&nbsp;&nbsp;</td><td class="hangman">&nbsp;&nbsp;&nbsp;</td></tr>
  123.         </tbody>
  124.     </table>
  125.                 <tr><td>&nbsp;</td><td class="subject" align="left" id="subject"></td></tr>
  126.     <tr><td class="letters">Used letters: <span id="used_letters"></span></td><td class="letters">Available letters: <span id="avail_letters"></span></td></tr>
  127. <tr><td>&nbsp;<td><input type="text" onkeyup="if (this.value!='') { tryLetter(this.value); this.value=''; }" value="Type Letters Here" onclick="this.value='';" maxlength="1">
  128. </table>
  129. <div id="letters">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</div>
  130. <div id="main">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</div>
  131. </body>
  132. </html>
  133.  
Alright heres my example of the game hangman in javascript. The uploaded zip has the images and such for it. Hope this helps someone.

----- I have modified to the code to work in firefox now ----
Attached Files
File Type: zip hangman.zip (12.3 KB, 153 views)



  #2  
Old November 3rd, 2007, 05:00 AM
Ferris's Avatar
Member
 
Join Date: Oct 2007
Location: Shanghai
Posts: 102

re: Hang man


hi

It's good.I like it,but it doesn't work in Firefox.

Regards.
  #3  
Old November 3rd, 2007, 10:41 AM
acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,521

re: Hang man


It doesn't work in Opera or Safari either.

The problem is that the phrases array length is 0, so when the count matches you win straight away.
  #4  
Old November 5th, 2007, 03:41 PM
iam_clint's Avatar
Forum Leader
 
Join Date: Jul 2006
Location: Oklahoma
Posts: 1,077

re: Hang man


should work in firefox now.
  #5  
Old September 2nd, 2008, 02:03 PM
RamananKalirajan's Avatar
Needs Regular Fix
 
Join Date: Mar 2008
Location: Chennai - India
Posts: 341

re: Hang man


It was amazing

Regards
Ramanan Kalirajan
Reply


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to java; String variable as a literal argument. slimewizard answers 2 August 18th, 2009 08:45 PM
Is Access my man? Luvin lunch answers 14 March 13th, 2008 01:05 PM
Process.Start is causes application to hang Jesse Cates via DotNetMonster.com answers 4 November 19th, 2005 04:29 AM
Microsoft's Martin Taylor: A Desperate Man Ministry Of Jute answers 24 July 21st, 2005 03:45 PM