Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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)

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 guessesvar 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 ;DAMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

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

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;

![]() |
![]() |
![]() |

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