Making Hangman game in javascript?
-
Hi, I'm trying to make a Hangman game in JavaScript. I want to have the player type the letter in a textbox and click. If the letter is part of the set of letters in the word, a letter will appear in another textbox as part of the word. If it isn't, the image of the gallows will change to show (in each turn) a head, then a torso, then each arm, and then each leg. I have several jpg images for this. I want the picture to change in turn with each wrong answer. At the end, a pop-up box will say Game Over and show the word. I'm having a hard time figuring out how to use the if statement in this to do all this, and I don't know how to make something where the letters can show up in the textboxes without having letters in them already. I tried naming the textboxes so I could use them in the if statement (giving them values) but the letters show up in the boxes when I don't want them to do so. I'm learning how to use JavaScript, but it's hard to apply a one-size fits all, so to speak. Can anyone please help me with this? I'd be grateful for any help at all. Thanks.
-
Answer:
I was like really bored, so I wrote a simple version for you without images, but it works. check the this pastebin link for a properly formatted version of this program. http://pastebin.com/wCMYdBHS <!doctype html> <html> <head> <script type="text/javascript"> var word = "Hangman" //This is the word for the game var obj; var output; var count = 0; //This variable holds the number of mistakes var init = function() { //in this function the word is split into letters and made an object to categorize letters word = word.toLowerCase().split(""); // make word lowercase and split to an array of letters ['h','a','n','g','m','a','n'] obj = oc(word); // create an object {'a':'i','g':'i','m':'i','n':'i'} draw(); //draw the first time _ _ _ _ _ _ _ } function draw() { // this function draws a '_' if the letter is not yet guessed and the letter if the letter is guessed output = ''; // reset the temporary output string for(var i = 0; i < word.length; i++) { // loop through all the letters if(obj[word[i]] == 'i') { // if the letter in the object has an i (i means not guessed yet) word[i] is the letter on the ith (1st, 2nd etc) place obj[letter] returns the state of the given letter output += '_ '; // add _ to the output }else{ output += word[i] + ' '; // add the letter to the output, because it is already guessed } } output += ' ' + count; // optional: add the number of mistakes to the output $('output').innerHTML = output; // print the output in the output container } function Test(letter) { //This function checks if the chosen letter is valid if(letter in obj) { // if the letter is in obj obj[letter] = 'a'; // set the value of the letter to 'a' (not 'i') } else { count++; // wrong, mistake + 1 } draw(); // draw the line var winning = true; // if this variable doesn't change the player won for (var a in obj) { // loop through the object (all the used letters) if(obj[a]=='i') { // this letter is not guessed yet winning = false; // so winning is false break; // break the loop, the user has not won yet } } if (winning) { // if the user won alert('You won!'); } } Object.size = function(obj) { //misc. function for counting the number of letters in the object var size = 0, key; for (key in obj) { if (obj.hasOwnProperty(key)) size++; } return size; }; function oc(a) //misc. function for creating an objec from a function { var o = {}; for(var i=0;i<a.length;i++) { o[a[i]]='i'; } return o; } var $ = function(id){return document.getElementById(id);} //misc. function for making selecting elements easier. </script> </head> <body onload="init()"> <div id="output"></div> <input type="text" id="letter"/> <button onclick="Test($('letter').value);$('lett… </body>
Maggie at Yahoo! Answers Visit the source
Related Q & A:
- Why is Javascript called Javascript, if it has nothing to do with Java?Best solution by Stack Overflow
- Do consumers do more research before making an expensive purchase than before making a cheaper purchase?Best solution by answers.yahoo.com
- Why does game system like Nintendo DS, has worse graphics than other handheld game systems like PSP?Best solution by answers.yahoo.com
- How can I stop a player in chess from freezing my game when I am winning in the middle or at the end of a game?Best solution by chess.com
- What game making program should we use?Best solution by makeuseof.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.