Making with Code

Riddler #

In this lab we will continue to review classes by making a Trivia game.

[0] Setup #

๐Ÿ’ป Let's start by cloning the repository in your year2_review folder. Be sure to change yourgithubusername to your actual Github username.

cd ~/desktop/making_with_code/year2_review
git clone https://github.com/the-isf-academy/lab_riddler_yourgithubusername
cd lab_riddler_yourgithubusername
๐Ÿ’ป Get the necessary packages:
poetry install
๐Ÿ’ป Enter the Poetry shell
poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D

๐Ÿ“„ This repository has two files:

  • riddle.py: This file has the Riddle class
  • riddle_list.py: This file has a list of Riddle instances
  • game.py: When run, this file should play the riddle guessing game

[1] Riddle Class #

Let’s start by exploring the Riddle class. It has two properties and one method.

class Riddle:
    def __init__(self,prompt,answer):
        self.prompt = prompt
        self.answer = answer


    def check_guess(self,guess):
        #Checks whether a guess is correct.
        #Uses the fuzzywuzzy library to accept guesses which 
        #are close to the answer.

        min_fuzz_ratio = 80
        similarity = fuzz.ratio(guess.lower(), self.answer.lower())

        if similarity >= min_fuzz_ratio:
            return True
        else:
            return False

To learn more about how check_guess() accepts guesses that are very close to the answer, you can visit the fuzzywuzzy documentation

๐Ÿ’ป Open riddle.py and look at the bottom of the file. We can use this section of the file to run tests on our Riddle to ensure it works as expected.

if __name__ == "__main__":
    r = Riddle(
        prompt = 'What has to be broken before you can use it?', 
        answer = 'an egg')
    
    print(r.prompt)

    # TODO: print the answer
  


    # TODO: call check_guess() and print the return value

๐Ÿ’ป Run riddle.py to see the output.

What has to be broken before you can use it?

๐Ÿ’ป Complete the TODO items. Print answer and use check_guess(). Example output should like this this:

What has to be broken before you can use it?
an egg
False
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the python shell

When you want to exit the shell, you can type exit() or ^D

๐Ÿ’ป Now that you understand how to create a Riddle, open up riddle_list.py

This file contains the list list_of_riddles with only one Riddle().

list_of_riddles = [
    Riddle(
        prompt = 'What has to be broken before you can use it?',
        answer = 'an egg'),    
]

๐Ÿ’ป Add 5 instances of Riddle() to riddle_list. Here is a list of riddles, but feel free to write your own!


[2] Creating the Game #

Now that you understand how the Riddle is structured, it’s up to you use it and create a guessing game.

-----------------------------------
---- Welcome to the Riddler ----
-----------------------------------

๐Ÿ’ป Start by opening up game.py

๐Ÿ’ป It is up to you to finish the code in game.py to create a riddle guessing game! The game should:

  • loop each Riddle in the list_of_riddles list
  • ask the user to guess the answer
  • tell the user if their guess was correct or incorrect

๐Ÿ•น๏ธ Once you’ve completed your game logic, play test your game! python game.py

-----------------------------------
---- Welcome to the Riddler ----
-----------------------------------

Riddle 1: What is black and white and read all over?
Guess: newspaper
Correct :)

Riddle 2: What has to be broken before you can use it?
Guess: A chicken
Incorrect :(

[3] Deliverables #

โšกโœจ Congrats on completing the lab!

Once you’ve successfully completed the lab, fill out this Google form.

๐Ÿ’ป Push your work to Github:

  • git status
  • git add -A
  • git status
  • git commit -m “describe your code and your process here”

    be sure to customize this message, do not copy and paste this line

  • git push


[4] Extension: Additional Features #

Now that we’ve got a basic riddle guessing game, let’s improve it!

Simple Improvements #

Let’s start with a few simple improvements.

  • tracking user guesses
  • randomizing order of riddles

๐Ÿ’ป Keep track of the user's guesses and provide a score at the end. For example once the user guessed all of the riddles, it may print:

Score: 3/5

๐Ÿ’ป Randomize the order of the riddles. Use the random library to change the order the riddles are given each time the user plays the game.

You may want to look at the Random library documentation for a hint


Difficulty Settings #

Currently, there are no difficulty settings for the game. Some riddles are easy, some riddles are difficult, and some riddles are in-between.

๐Ÿ’ป Implement a difficulty setting in your game to make it more accesisble. Your game should allow the user between easy, medium, or hard riddles. This will require you to change the Riddle class and the game play flow in game.py.

Hint: what property could you add to store the difficulty of each riddle?


Add Terminal Menu System #

Although the Riddler is functional, it is not the most exciting looking game. Let’s spice it up with colors!

๐Ÿ’ป Explore the package colorama to add color to your game!

The package is already installed, but you will need to add the import statements to the top of game.py.