473,411 Members | 2,184 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,411 developers and data experts.

Hang man

iam_clint
1,208 Expert 1GB
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, 431 views)
Nov 2 '07 #1
4 5561
Ferris
101 100+
hi

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

Regards.
Nov 3 '07 #2
acoder
16,027 Expert Mod 8TB
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.
Nov 3 '07 #3
iam_clint
1,208 Expert 1GB
should work in firefox now.
Nov 5 '07 #4
RamananKalirajan
608 512MB
It was amazing

Regards
Ramanan Kalirajan
Sep 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Ido Zur | last post by:
The problem: We have ASP application that uses WSC as data access tier to SQL Server, and it runs well on windows 2000 server IIS 5.0. When we install the application on XP with IIS 5.1 the...
0
by: dyw55a | last post by:
Hi, I have a web service which have a web method to validate the user and password. I am not sure why it slow down after several person start to run it. I let it return interger. But when user...
3
by: Ember | last post by:
Doozie of a problem here - may open a ticket with support, but wanted to see what others had to say first... We have an AIX 5.2 server with DB2 V8 FP5 and 32-bit instance. ~45GB db. Reorgs and...
0
by: szehau | last post by:
Hi all, I have a program written in C with embeded SQL. Following are the configuration: DB2/LINUX 8.1.5 Thread model: posix gcc version 3.2 20020903 (Red Hat Linux 8.0 3.2-7) My problems...
2
by: Serge | last post by:
Hi, I am having a thread hang problem in my c# code. The example on the website: http://csharp.web1000.com/ is a simplified version of my problem. You will see in the form that a...
4
by: Madhu Gopinathan | last post by:
Hi All, I am faced with a horrible hang problem. I have a COM exe server that executes some tasks. The task execution manager is a thread that manages the pool of threads, which is 4 per processor....
1
by: caesarchen | last post by:
Dear all: The following is the situation,can any one share me idea to solve it? Windows 2000 + IIS6.0 + .NET Framework 1.1 6 projects, about 800 webforms and 300 .asmx programs Project cowork...
0
by: Rosny | last post by:
Hi, Is any body experiencing the same problem as below scenario? Environment : Windows 20003 Server ASP.NET Beta 2 STEP to reproduce: 1. Create a link on aspx page to any zip file on the...
0
by: Ben | last post by:
Hi there, We've asp.net application running on a Win2000 server + .net framework 1.1.4322 . After running for a few weeks, it got hang. Even calling up a simple page such as the home page took...
0
by: Jeff | last post by:
Banging my head against the wall trying to figure this out. I have a 'sometimes' hang while disposing a form which contains an ActiveX control (Flash.ocx version 7). The form's Dispose() method...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.