Background Information

This assignment tests your understanding of and ability to apply the programming concepts we have covered in the unit so far, including the usage of variables, input/output, data types, selection, iteration, functions and data structures. Above all else, it tests your ability to design and then implement a solution to a problem using these concepts.

Assignment Overview

You are required to design and implement a Word Game program in which the user must identify a randomly selected password from a list of 8 words. The 8 words are selected at random from the list of 100 words in the starter file (word_game.py) provided to you with this assignment brief. Please use the starter file as the basis of your assignment code.

The user has 4 attempts in which to guess the password. Whenever they guess incorrectly, they are told how many of the letters are the same between the word they guessed and the password. For example, if the password is COMEDY and they guessed MOULDY, they would be told that 3/6 letters were correct due to the O, D and Y being the same. Note that a letter must be in the correct position to be correct. e.g. in the example above, both words contain the letter M but it is in a different position in the word so it is not counted as a correct letter.

Using this information, the user can make increasingly knowledgeable guesses and win the game by selecting the password within four guesses. If the user fails to select the correct word within 4 guesses, they lose the game.

This game is similar in some ways to the game of mastermind, and is based upon the terminal hacking minigame from the Fallout video game franchise. You do not need to be familiar with either of these games in order to complete this assignment contact your tutor if you do not understand the game.

Gameplay / Program Output Example

To help illustrate the program, here is an annotated screenshot of the game being played: See image.

Pseudocode

As emphasised by the case study of Module 5, it is important to take the time to properly design a solution before starting to write code. Hence, this assignment requires you to write and submit pseudocode of your program design as well as the code for the program. Furthermore, while your tutors are happy to provide help and feedback on your assignment work throughout the semester, they will expect you to be able to show your pseudocode and explain the design of your code.

You will gain a lot more benefit from pseudocode if you actually attempt it before trying to code your program even if you just start with a rough draft to establish the overall program structure, and then revise and refine it as you work on the code. This back and forth cycle of designing and coding is completely normal and expected, particularly when you are new to programming. The requirements detailed on the following pages should give you a good idea of the structure of the program, allowing you to make a start on designing your solution in pseudocode.

See Reading 3.3 for further information and tips regarding writing good pseudocode.

Write a separate section of pseudocode for each function you define in your program so that the pseudocode for the main part of your program is not cluttered with function definitions. Ensure that the pseudocode for each of your functions clearly describes the parameters that the function receives and what the function returns back to the program.

It may help to think of the pseudocode of your program as the content of a book, and the pseudocode of functions as its appendices: It should be possible to read and understand a book without necessarily reading the appendices, however they are there for further reference if needed.

Only one function is required in this assignment (detailed later in the assignment brief).

Program Requirements

The requirements begin where the starter file ends after defining the candidateWords list. In the following information, numbered points describe a requirement of the program, and bullet points (in italics) are additional details, notes and hints regarding the requirement. Ask your tutor if you do not understand the requirements or would like further information.

1. Set up the game by creating a list of 8 randomly selected words from candidateWords and then randomly select one of those words to be the password. Also create a boolean variable to keep track of whether or not the game has been won (set it to False) and an integer variable to keep track of the number of guesses remaining (set it to 4).

  • This assignment brief will assume variable names of wordList, password, won and guessesRemaining for the variables mentioned above.
  • The random.sample() function can be used to randomly select a number of items from a list, and random.choice() can be used to randomly select just one item from a list.
  • The won variable will be set to True if the password is guessed, and will help to control the loop (see Requirement 2) and decide which message to print at the end of the game (see Requirement 3).

2. Print a message to welcome the user, and then enter a loop that will repeat while guessesRemaining is greater than 0 and won is False. The body of this loop must

2.1. Print all of the words in wordList along with their corresponding index number, and then print the number of guesses remaining.

  • The most efficient way to print all of the words in wordList is to use a for loop and the enumerate() function. See Lecture 3, Slide 38 for an example of this.

2.2. Prompt the user to choose one of the words by entering its index number (0-7), and reduce guessesRemaining by 1.

  • Note that the user inputs an index number, not the word itself. To get the corresponding word, refer to that item of the wordList, e.g. wordList[guessNum].
  • Remember that the input() function always returns the users input as a string you will need to convert it to an integer before you can use the value as an index number of wordList.

2.3. Print the selected word, and then check if it is the same as password. If the words are the same, the user has guessed correctly: The program should print a password correct message and set the won variable to True.

If the words are not the same, the user has guessed incorrectly: Print a password incorrect message, then call the compareWords function to determine the number of matching letters before printing a message to tell the user how many letters are correct.

3. The last thing the program should do (after the body of the loop described in Requirement 2) is print a you win message if the won variable is True, or a you lose message if it is False.

  • By the time the program reaches this point, either the loop ended because the user ran out of guesses (and hence the won variable is still set to False), or because the user guessed correctly (and hence the won variable was set to True as per Requirement 2.3).

The compareWords function

When the user selects a word that is not the password, the program needs to determine how many matching letters there are between the password and the selected word (see Requirement 2.3).

Your program must define a function named compareWords that receives two words (the password and the selected word) as parameters. It must determine the number of matching letters between the words, and return this number back to the program as an integer. To do this, set a counter variable to 0 and then then loop through each letter of the words, adding one to the counter if they match.

The definition of the function should be at the start of the program (as it is in the starter file provided), and it should be called where needed in the program. Revise Module 4 if you are uncertain about defining and using functions.

Additional Requirements for CSP5110 Students

If you are in CSP5110, the following additional requirements apply. If you are in CSP1150, you do not need to do implement these requirements (but you are encouraged to do so if you want). Ask your tutor if you do not understand any of the requirements or would like further information.

1. Add 1 to the index number of words whenever you show the list of words (Requirement 2.1), so that the games are numbered from 1-8 instead of 0-7. Remember to subtract 1 from the users input when they guess a word, so that you reference the appropriate index in the wordList.

2. To make it easier for the user to keep track of previous guesses, show the number of matching letters next to the words that have been guessed whenever you show the list of words.

  • Keep an eye on the Blackboard discussion boards if you cannot figure out how to implement this requirement, or ask your tutor for help.

Optional Additions and Enhancements

Below are some suggestions for minor additions and enhancements that you can make to the program to further test and demonstrate your programming ability. They are not required and you can earn full marks in the assignment without implementing them.

  • Put everything after the creation of candidateWords into a loop so that the game can be played repeatedly without having to re-run the program each time. At the end of the game, ask the user whether they wish to play again and exit the loop if they dont.
  • Ensure that your program does not crash if the user enters something that is not an integer when prompted for a guess. Instead, your program should show an invalid input message and prompt the user again until they enter something valid. This is best done using exception handling (Module 6), however what you need to know is covered in Workshop 4.
  • Before starting the game, prompt the user to select Easy, Medium or Hard difficulty, and start guessesRemaining at 5, 4 or 3 depending on their choice. You could even change the number of words in wordList based upon the difficulty.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.