Javascript Hangman
I am supposed to be building a hangman game, a simple basic hangman game
and for some reason I cannot figure out what to do or where to go. Any
direction will be appreciated!
I know how hangman works, but I am stuck with the most basic stuff :(
/*
* Using an array for underscores so substitution
* can happen much more easily. No string functions
* to replace positions where as you can do it with
* an array ie.) underscore[2] = 'r';
*/
underscore = [];
secretWord = "chrome";
splitWord = secretWord.split("");
for (var i = 0; i < splitWord.length; i++){
underscore.push("_");
}
// print _'s for secret word
output = '';
for (x=0;x<underscore.length;x++){
output += underscore[x] + " ";
}
element = document.getElementById("placeholder");
element.innerHTML = output;
// Store the wrong guesses to determine when to end the game
wrongGuesses = [];
// Store all of the guesses to disallow duplicates
guesses = [];
// Run through this code continuously asking the user
// to enter letters until we break the loop manually
// because they've won, or they guessed too many times
// incorrectly and will lose
while (1==1) {
userGuess = prompt("What letter would you like to guess?");
// Verify that they have not yet guessed this letter incorrectly
while (guesses.indexOf(userGuess.toLowerCase()) >= 0){
alert("Already used that letter, please try again");
userGuess = prompt("What letter would you like to guess?");
}
// Convert their letter to lowercase
userGuess = userGuess.toLowerCase();
// Add their guess to guesses
// Check to see if the letter exists in the word
// If it does exist, replace the proper _ position
// With the guessed letter
// Test to see if they've guessed all the letters in the word
// If it does not exist, add their guess to wrong guesses
// Display the hangman based on how many wrong guesses they've had
// If they've guessed too many times, tell them they lost
No comments:
Post a Comment