Riddler
In this lab we will remind ourserlves of classes and object-oriented programming with a riddle guessing game. It’s up to you to use the Riddle class to create working game.
[0] Setup
Due to an issue with our website, you will clone this lab manually.
๐ป
Start by going into your unit0.0_review folder.
cd ~/desktop/making_with_code/cs10/unit0.0_review/
git clone https://github.com/the-isf-academy/lab-riddler.git
cd lab-riddler
๐ป Get the necessary packages Run the following command in your terminal.
poetry update
๐ป Enter the Poetry Shell. We will run this command at the start of each lab, but only when we are inside a lab folder.
poetry shell
๐พ ๐ฌ Exiting the poetry shell
When you want to exit the shell, you can typeexitor^D
This repository has two files:
riddle.py: This file had theRiddleclass and a list ofRiddleobjectsgame.py: When run, this file should play the riddle guessing game
[1] Riddle Object
Let’s start by exploring the Riddle object. 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 inside the interactive Python shell. As a reminder, the Python shell is a great way to run small tests.
python -i riddle.py
๐ป
Create an instance of a Riddle.
r = Riddle('What gets wet when drying?','a towel')
๐ป
Print the prompt and the answer to check you correctly created a Riddle.
r.prompt
r.answer
๐ป
Try to guess the riddle by using the check_guess(guess) method.
r.check_guess('a towel')
๐พ ๐ฌ Exiting the python shell
When you want to exit the shell, you can typeexit()or^D
๐ป
Now that you understand how to create a Riddle, open up riddle.py
This file contains a list of Riddle objects called riddles. Currently, there are only 2 riddles.
riddles = [
Riddle(
'What has to be broken before you can use it?',
'an egg'),
Riddle(
'What month of the year has 28 days?',
'all of them'
)
]
๐ป Add at least 3 more riddles to the 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
Riddlein theriddleslist - 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
[3] Deliverables
โกโจ Congrats on completing the Riddler!
Once you’ve successfully completed the Riddler be sure to fill out this Google form.
[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.
๐ป 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
[Add Color]
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!
It is already installed, but you will need to add the import statements to the top of
game.py.
[Difficulty Setting]
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 may require you to change the Riddle object and the game play flow in game.py.