Homework 1

Homework 2

« hide

DUE: Thursday 7/30

Please email your C++ source file(s) to me before 11:59pm on the due date.

Write an interactive hangman game in C++ with the rules and specifications described below. Follow the instructions carefully and test your code thoroughly.

  1. Your hangman program begins by reading a file called word-list.txt. This input file contains individual words for your hangman game.
  2. Write a function called read_file() that reads all words from the file and stores them in an array of strings. Your function should return the number of words read. Display this value to the user in main():
    Welcome to HANGMAN!
    (successfully read in 26 words....)
    
  3. Further, convert all letters of each word to lowercase. You may assume that no more than 100 puzzles will be in the input file.
  4. Start the game by randomly selecting a puzzle from the array. Show the puzzle by creating a new string variable in which each letter is replaced with an underscore character, leaving all other characters (e.g. punctuation) unchanged, as in:
    
    Here's the puzzle:  _ _ _ _ ' _
    
    
  5. Allow users to repeatedly guess letters. Write a decision-making function called puzzle_contains_letter() that determines whether the given letter is in the puzzle.
  6. In main(), if the puzzle_contains_letter() function returns true, fill in all occurrences and show the puzzle again. Otherwise, the player is that much closer to being hanged! To replace all occurrences, write a function called replace_all().
  7. Here's an example of how your program should operate (with sample user input shown in bold):
    
    Here's the puzzle:  _ _ _ _ ' _
    
    Guess a letter: t
    Good guess!
    
    Here's the puzzle:  _ _ _ _ ' t
    guessed letters: t
    
    Guess a letter: k
    Sorry, no 'k'
    
    Here's the puzzle:  _ _ _ _ ' t
    guessed letters: k t
    
    Guess a letter: d
    Good guess!
    
    Here's the puzzle:  d _ d _ ' t
    guessed letters: d k t
    
    ...
    
    
  8. Use an array of 26 bool elements to keep track of the individual letters that have been guessed. Initially, this array should contain 26 false values. As shown above, display the guessed letters (regardless of whether they're in the puzzle or not) in alphabetical order. To do so, write a function called display_guessed_letters().
  9. When a user guesses six incorrect letters, the player is hanged and the game is over! Implement this logic.
  10. If a player guesses all the correct letters without being hanged, the player wins. Implement this logic.
  11. Extra credit: display each piece of the hangman below as players guess incorrect letters.
        -------
        |     |
        |     O         H
        |   --|--         E
        |     |             L
        |    / \              P
        |                       !
       =|===========
      / |           \
     /               \
     =================