C. Program Description for Developers

Hangman

The hangman program contains the necessary code to play the well-known game of hangman. In accordance with the rules of hangman, one player chooses a random word, and the other player has a given amount of attempts to guess letters contained within the word or to guess the word itself. Usually, the player has 6 guesses, because every time the player guesses wrong, a body part of a “hanged man” is drawn, starting with the head, then the torso, two arms, and finally, two legs. In our program, the user is the player that plays against the computer. The computer chooses the word and keeps track of remaining attempts, while the player must guess the letters of the word. 

The program begins with loading a dictionary file – a file that contains all the words in the English language. Then, from these words, the program selects a random word. This becomes the secret word that the player must guess. 

The program then enters the main game loop. As long as the user has more than 0 attempts remaining, and given that they have not already won the game, the program will print out the secret word, with the correctly guessed letters displayed and the remaining unguessed letters with dashes. Then, the program asks the user to input a letter or a word. The program will continue to prompt for a letter or a word that the user has not entered before. As soon as the user enters a unique input, the program checks to see if it is a letter or a word. If the user entered a letter that is in the word, then the program checks to see if there are any letters left to guess. If not, then the user has won the game. If there are still letters remaining, then the main game loop repeats. If the user entered a word, then the program checks to see if it matches the secret word. If so, the user has also won the game. Alternatively, if the user entered an incorrect letter or incorrect word, then one attempt is subtracted from the remaining attempts, and the subsequent part of the hangman is drawn. 

Once the program exits from the main game loop, if the user won, then a winning message is displayed; if they lost, then a losing message is displayed. The user is prompted if they want to play again, and if so, the program repeats the entire process outlined above, if not, then the program exists. 

Guess

The Guess game is a very simple game that allows 2 players to guess a number between 1 and 10, and the player that chose the number closest to the randomly generated number between 1 and 10 wins.

The program does this by introducing the game with an introduction, and promoting player one to input a number between 1 and 10. Using a while loop, the program will continue to ask for a valid response between 1 and 10 if a number outside of that range is given by the user. Next, another prompt is used to get the second player’s input. The same while loop is applied to the second user to keep the input within the given range. Another while loop is applied to the second user’s input because they are not allowed to guess the same number as the first player or else the game would always end up in a tie! The randomly generated number between 1 and 10 was done using the randi function, to randomly select a number in the given parameters.

To program who would win the game, the absolute value of the difference between the player’s guess, and the randomly selected number was taken. Which ever player had the smaller absolute difference had the closer number to the random number, and therefore won the game. The program would then output to the screen which player was closer, and that they won the game. An exit prompt was used and asked the players if they would like to play again by either typing y or n (yes or no). If y was input, the program used a repeat function to repeat the code back up to the start of the game, just below the introduction so they would not get the same intro prompt every time. If n was input. The game would end and the program would come to completion.