Preview only show first 10 pages with watermark. For full document please download

Assignment 3 Question 3 Specification

   EMBED


Share

Transcript

CS247 Spring 2017 Assignment 3 Question 3 Specification Q3 [60 marks] Generic Programming You will use C++ STL containers and algorithms to implement a Hangman program. Hangman is a game in which the player tries to guess what word has been selected from a pool of words. The player guesses letters that he or she thinks might be in the word, and the game displays where the guessed letters appear in the word. If the player correctly guesses the entire word before making five incorrect guesses about the word's letters, the player wins. Objective Your solution must use STL containers and algorithms to perform much of its functionality. Specifically, it must use an STL container of your choice to store the pool of game words, and it must use STL container operations or STL algorithms (possibly with predicates and function objects, though lambdas are strongly preferred) to: • • • • • • Read words from the input file into the STL container. Remove unsuitable words from the container. Output the modified contents of the container to the file named gamewords. Find guessed letters in the game word. Test whether the guessed letter completes the game word. Print the game word with unguessed letters replaced by hyphens. As a result, your code should be very short, and you likely won't need classes. We'll thus encapsulate everything in a single file. Provided files We have provided a sample executable, sampleHangman, that you can use to compare your output to ours. Your program will be tested only on valid input commands, except for the error cases explicitly mentioned. We have also provided a sample file of words and the file of game words that was produced from it. Execution There are four main stages to the game. Initializing the game with a dictionary of words through the command line, setting up the pool of game words, starting the game, and playing the game. 0. Command line parameter To start the program, the user provides the name of a file of words from which a game word is selected; the user can also provide an optional integer argument, like this: ./hangman words 42 where words is a file of words. The integer argument is used to seed the pseudo-random selection of game words. You may assume it is a valid integer. Use std::mt19937 to declare a static random number generator, seeded with the value provided in the command line. If no seed is provided in the command line, assume that it is 0. The seed must only be set once. Games that are started with the same argument value have the same sequences of selected game words. If no command-line arguments are provided, print the error message to standard output: \nERROR: No input file specified.\n If the program either cannot find the file specified by the command-line argument , or cannot open it for reading, print the error message to standard output: \nERROR: Could not open file "".\n CS247 S17 A3Q3 Specification 2 1. Game word pool The first command-line argument is a file of words where each word is separated by whitespace. Read the words into an STL container of your choice. The input file might include words that are unsuitable for the game. Remove from the container any word that: o contains a non-alphabetic character o has fewer than 6 characters The relative order of the words that remain in the container should be preserved. Output the contents of the modified container, one word per line, to a file named gamewords. The last word in the file should be followed by a newline character. The pool is set up only once, no matter how many games are played. If the pool of game words is empty (either initially or after removing all unsuitable words), print the error message to standard output: \nERROR: Pool of game words is empty.\n 2. Game start The first game starts after the pool of game words has been processed and output to a file. Generate a random number, modulo the number of words in the pool of game words, to randomly select the first game word. Print the following four lines: Word:\n Letters used:\n You have 5 lives left.\n Next guess: is the game word, but with all of its letters replaced by hyphens. Thus, if the game word is 7 characters long, 7 hyphens are printed. is a single blank space character. The player starts with 5 lives. 3. Game play Whenever it is the player's turn, the player can either guess a letter that he or she thinks is in the game word, or can try to guess the whole word. a) Guess a letter If the guessed letter has not previously been guessed during this game, then print an update to the state of the game: Word:\n Letters used:\n You have lives left.\n Next guess: The differences between the above four lines and the lines on the previous page are in bold font. is the game word except that each letter not yet guessed is replaced by a hyphen. are the letters that have been guessed within this game, including the letter guessed in this turn. The letters guessed are printed in lowercase, separated by a single space, and printed in the order in which they have been guessed. If a character other than an alphabetic letter is guessed, it is treated as an incorrectly guessed letter. is the number of lives that the player has left. The player loses a life every time he or she guesses a letter that is not in the game word. (If the player has 1 life left, then "1 life" is printed rather than " lives".) CS247 S17 A3Q3 Specification 3 If the guessed letter is the last unguessed letter that completes the game word, then print: You WIN!The word was "".\n Do you want to play again? [Y/N] where is the game word with all of its letters revealed. If the player's response starts with "y" or "Y", then start a new game with a new randomly selected game word. Otherwise, quit. Note that the win message is different if the user wins by guessing the entire word. Guesses of letters are NOT case sensitive, so if the letter 'A' or 'a' is guessed, it matches all instances of 'A' or 'a' in the game word. When a guess is printed, it is converted to lower-case if it was previously in upper-case. As an example, if the game word is 'African' and 'a', 'r', and 'e' have been guessed in that order, then the updated state of the game is: Word:A-r--a-\n Letters used:are\n You have 4 lives left.\n Next guess: If the guessed letter has previously been guessed, then print an error message, followed by the (unchanged) state of the game: You already guessed letter "a".\n Word:A-r--a-\n Letters used:are\n You have 4 lives left.\n Next guess: b) Guess a word If the guess is longer than a single character, it is treated as a guess of the entire game word. If the guessed word exactly matches the game word, where this guess IS case sensitive, then print: You WIN!\n Do you want to play again? [Y/N] Otherwise, print You LOSE!The word was "".\n Do you want to play again? [Y/N] where is the game word with all of its letters revealed. If the player's response starts with "y" or "Y", then start a new game with a new randomly selected game word. Otherwise, quit. Hints 1. If there is a loop in your program other than the main loop that encodes a player's turn, there is a good chance that it should be replaced with an STL algorithm. 2. A good general rule is to think about what you want to do and figure out if an STL algorithm exists that is related to what you need to do.