<h1>Hangman Game</h1>
<p>Hangman is a popular guessing game where one player thinks of a word and the other player tries to guess the word by suggesting letters, with a limited number of guesses. In this version of the game, you will be playing against the computer.</p>
<h2>How to Play</h2>
<ol>
<li>The computer chooses a word at random from a list of words.</li>
<li>The word is displayed as a series of dashes, one for each letter in the word.</li>
<li>You guess a letter by clicking on the corresponding button on the screen.</li>
<li>If the letter is in the word, it will be revealed in the correct position(s). If the letter is not in the word, a part of the hangman is drawn.</li>
<li>You continue to guess letters until you have correctly guessed the word or the hangman is fully drawn.</li>
</ol>
<h2>Rules</h2>
<ol>
<li>You have 6 guesses to correctly guess the word.</li>
<li>Each wrong guess will result in a part of the hangman being drawn.</li>
<li>If the hangman is fully drawn before you guess the word, you lose.</li>
</ol>
<h2>Ready to Play?</h2>
<p>Click the button below to start the game!</p>
<p><button onclick='startGame()'>Start Game</button></p>
<script>
// Array of words to choose from
const words = ['javascript', 'hangman', 'computer', 'programming', 'developer', 'web'];

// Choose a random word from the array
let word = words[Math.floor(Math.random() * words.length)];

// Create an array to store the correctly guessed letters
let guessedLetters = [];

// Create a variable to store the number of incorrect guesses
let incorrectGuesses = 0;

// Create a function to start the game
function startGame() {
  // Reset variables
guessedLetters = [];
incorrectGuesses = 0;
  
  // Choose a new random word
  word = words[Math.floor(Math.random() * words.length)];
  
  // Display the word as dashes
  let wordDisplay = '';
  for (let i = 0; i < word.length; i++) {
    wordDisplay += '-';
  }
  document.getElementById('word-display').innerHTML = wordDisplay;
  
  // Reset the hangman
  document.getElementById('hangman-image').src = 'hangman-0.png';
  
  // Enable the letter buttons
  let letterButtons = document.getElementsByClassName('letter-button');
  for (let i = 0; i < letterButtons.length; i++) {
    letterButtons[i].disabled = false;
  }
}

// Create a function to handle a letter guess
function guessLetter(letter) {
  // Disable the button so it cannot be clicked again
  document.getElementById(letter).disabled = true;
  
  // Check if the letter is in the word
  if (word.indexOf(letter) !== -1) {
    // Update the word display with the correctly guessed letter
    let wordDisplay = '';
    for (let i = 0; i < word.length; i++) {
      if (guessedLetters.indexOf(word[i]) !== -1) {
        wordDisplay += word[i];
      } else {
        wordDisplay += '-';
      }
    }
    document.getElementById('word-display').innerHTML = wordDisplay;
    
    // Check if the word has been fully guessed
    if (wordDisplay.indexOf('-') === -1) {
      alert('Congratulations! You guessed the word!');
      startGame();
    }
  } else {
    // Update the hangman image
incorrectGuesses++;
    document.getElementById('hangman-image').src = 'hangman-' + incorrectGuesses + '.png';
    
    // Check if the game is over
    if (incorrectGuesses === 6) {
      alert('Sorry, you lost. The word was ' + word + '.');
      startGame();
    }
  }
  
  // Add the guessed letter to the list of guessed letters
guessedLetters.push(letter);
}
</script>
Play Hangman Game Online: Guess the Word - Free Game

原文地址: https://www.cveoy.top/t/topic/lnQV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录