A Pen by Dashon Hawkins on CodePen.
Created
February 12, 2017 07:52
-
-
Save Dashon-Hawkins/27edf2a7a919d612380a3fc2320eeb53 to your computer and use it in GitHub Desktop.
Hangman Game in HTML/JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Hangman!</title> | |
| </head> | |
| <body> | |
| <h1 id="main-heading"><center>Hangman!</center></h1> | |
| <script text="hangman.js" src="https://code.jquery.com/jquery-2.1.0.js"> | |
| <script src></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Create an array of words | |
| var words = [ | |
| "javascript", | |
| "monkey", | |
| "amazing", | |
| "pancake", | |
| "galvainze", | |
| "cohort", | |
| "concatenate", | |
| "iteration", | |
| "index", | |
| "code", | |
| "angular", | |
| "react", | |
| "python" | |
| ]; | |
| // Pick a random word | |
| var word = words[Math.floor(Math.random() * words.length)]; | |
| // Set up the amswer answerArrayvar answerArray = []; | |
| for (var i = 0; i < word.length; i++) { | |
| answerArray[i] = "_"; | |
| } | |
| var remainingLetters = word.length; | |
| // The game loop | |
| while (remainingLetters > 0) { | |
| // Show the player their progress alert(answerArray.join(" ")); | |
| // Get a guess from the player | |
| var guess = prompt("Guess a letter, or click Cancel to stop playing."); | |
| if (guess === null) { | |
| // Exit the game loop | |
| break; | |
| } else if (guess.length !== 1) { | |
| alert("Please enter a single letter."); | |
| } else { | |
| // Update the game state with the guess | |
| for (var j = 0; j < word.length; j++) { | |
| if (word[j] === guess) { | |
| answerArray[j] = guess; remainingLetters--; | |
| } | |
| } | |
| } | |
| // The end of the game loop | |
| } | |
| // Show the answer and congratulate the player | |
| alert(answerArray.join(" ")); | |
| alert("Good job! The answer was " + word); | |
| // Write your functions here | |
| var word = pickWord(); | |
| var answerArray = setupAnswerArray(word); | |
| var remainingLetters = word.length; | |
| var pickWord = function () { | |
| // Return a random word | |
| }; | |
| var setupAnswerArray = function (word) { | |
| // Return the answer array | |
| }; | |
| var showPlayerProgress = functin (answerArray) | |
| { | |
| //Use alert to show the player their progress | |
| }; | |
| var getGuess = function () { | |
| // Use prompt to get a guess | |
| }; | |
| var updateGameState = function (guess, word, answerArray) { | |
| // Update answerArray and return a number showing how many times the guess appears in the word so remainingLetters can be updated | |
| }; | |
| var showAnswerAndCongratulatePlayer = function (answerArray) { | |
| // Use alert to show the answer and congratulate the player | |
| }; | |
| while (remainingLetters > 0) { | |
| showPlayerProgress(answerArray); | |
| var guess = getGuess(); | |
| if (guess === null) { | |
| break; | |
| } else if (guess.length !== 1) { | |
| alert("Please enter a single letter."); | |
| } else { | |
| var correctGuesses = updateGameState(guess, word, answerArray); | |
| remainingLetters -= correctGuesses; | |
| } | |
| } | |
| showAnswerAndCongratulatePlayer(answerArray); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm working on the same project and I managed to write all the functions except for the last one "update game state" can you show me how you wrote it? Thanks in advance