Computing.Net > Forums > Programming > javascript hangman game help!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

javascript hangman game help!

Reply to Message Icon

Name: ellis1884
Date: December 9, 2007 at 06:03:40 Pacific
OS: XP & SP2
CPU/Ram: Quad Core G0 @ 2.4Ghz / 2
Product: Custom
Comment:

Hi i am supposed to make a javascript program to play a simple game of hangman
I have got several lines of code which works, but i stuck as to what do next...
could somebody point me in the right direction please?
below is what i have already
I am getting prompt to enter a word, then player2 takes over and enters a charcter, i need to save this as an index of all anwsers and also correctly guessed but have no idea how to go about it.
-----------

main_word=prompt("please enter hangman word:");
var casechanged=main_word.toUpperCase();
var lives=7
var main_word_array = [];
var answer_so_far = [];
var letters_guessed = [];


for(var i=0;i<main_word.length;i++) {
main_word_array[i]=main_word.charAt(i);
answer_so_far[i] = " __ ";
}
prompt ('Please enter a character')
alert (answer_so_far)
alert (main_word_array)



Sponsored Link
Ads by Google

Response Number 1
Name: Deimos
Date: December 9, 2007 at 08:30:53 Pacific
Reply:

Hello there,

I am currently reading a book were they taught me how to make a hangman game(big coincidence) I just did it yesterday, I only know C++, so i can't help you with the code itself, but from what i've seen, java isnt much different.

So lets start,
First you create a new variable called wrong (or something like that) that stores the number of uncorrect guesses

var wrong = 0;

then you need to enter the main game loop:

while((wrong < lifes) && (guessed_so_far != main_word){

var guess = prompt("Enter a guess:")
if(main_word.find(guess) != string::npos) {
for(var i = 0; i < main_word.lenght; ++i){
if(main_word[i] == guess){
answers_so_far[i] = guess;
}//end nested if
}//end for
}else{
[send a message to the player saying that his guess is NOT in the word and increment the wrong variable by one]
}//end main if [word.find(guess) != string::npos]

Let me explain the "code":
you start by creating a main loop that executes if the word hasn't been found yet AND the 'wrong' is bigger than 'lifes'.

You ask the user for a guess, if the guess is in the word you cycle trough all the chars in the word and replace the "_" for its corresponding letter. if not, inform the player that the guess isnt in the word and ask him to guess again.

Ending the game:

when the main loop stops executing it's because the player ran out of lifes or he discovered the word, so you make a simple if statement: if(word_so_far == main_word){[congragtulate the player}else{inform what word it was afterall}

I hope this helped, im kinda noob at helping people, and good luck on your game.

PSI: dont forget the uppercase thingy so you can compare both words.
PSII: Please repply saying if my answer helped ;D

AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;


0

Response Number 2
Name: ellis1884
Date: December 10, 2007 at 02:55:45 Pacific
Reply:

thanks, alot it helped loads,
im not worried bout the code but more about the structure you have to apply to get correct answer

cheers!

Ellis UK


0

Response Number 3
Name: Deimos
Date: December 10, 2007 at 11:34:01 Pacific
Reply:

Glad to help =D

(The first person I helped YAY!)

AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: javascript hangman game help!

Javascript or ASP help www.computing.net/answers/programming/javascript-or-asp-help/12772.html

C++ game help www.computing.net/answers/programming/c-game-help/1913.html

2D Pool Game - Help Needed www.computing.net/answers/programming/2d-pool-game-help-needed/8888.html