Rock, Paper, and Scissors: Series 8 — #LearnPythonThroughProjects
One of the oldest hand games, dated far back in history, centuries and centuries ago, passed down from generation to generation. It is not known for sure when it first started, however, its first known mention was in the book “Wuzazu” — written by Xie Zhaozhi, in the then Chinese Han-dynasty around 1600, where it was called ‘Shouling’. The game later found its way into the Japanese history, where a variation was created and named ‘Janken’, around 1700.
This hand-game is not just a pass time for most, it is considered as an official game in most parts of the world and finds a spot in professional tournaments(find more details here). It is also used in situations where decisions have to be made or a dispute, or disagreement has to be settled.
What makes it so interesting is that there is no room for cheating as a player cannot predict what the other player would do, it is a random choice. There are official rules and guidelines around it, however, there are also certain tricks people have come with over time to help them win the game. It is a game of calculation, intuition, smartness, and fun, all in one-pack.
OFFICIAL RULES OF THE GAME
- Rock beats scissors.
- Scissors beats paper.
- Paper beats rock.
So, how we can create a simple version of this to test our coding skills? To translate the sequence of the game into a programming language, we need to take note of the rules and what happens at each stage.
Another thing we will learn today is the importance of code efficiency.
It’s not always just about how many lines of code, or how well you can code? It’s about how efficient and readable the codes are. Where possible, aim at using as fewer lines of code as possible. Similarly: “Simpler codes are not always efficient codes”. It all depends on the aim of the code and task at hand.
Tricky right? “The Key here is Efficiency”. Aim at efficiency. Where possible use fewer lines if they serve the purpose well.
Let’s get into building a simple Python version!
PSEUDOCODE(rps_game.py)
- import the ‘random module’.
2. Initiate a welcome message
3. Create a ‘name’ variable for the player/user.
4. Print a greeting message for the user/player with game instruction.
5. Create a choice variable using the Tuple data structure.
6. Initiate game sequence using the Flow control statements for decision making.
i. Start with a while loop
ii. Create a player variable, for player’s choice
iii. Check for correct entry
iv. Print Player’s choice
v. Initiate computer’s choice.
vi. Print Computer’s choice
vii. Create game decisions using conditional statements.
Great! The Program is complete.
Let’s test it!
Save and run the program
Awesome right!
So what are the new concepts used?
The only newly introduced concept is ‘ a module:’
Let’s explain that.
MODULES IN PYTHON: What they are and how to use them.
What is a Module?
What’s the first definition that comes to your mind when you hear this word? “A pack, file or group of things that consists of smaller or minute items, things which can otherwise stand alone serving a single purpose, but as a module serves several multiple functions”. This is just my way of explaining it.
Dictionary definition: “each of a set of standardized parts or independent units that can be used to construct a more complex structure, such as an item of furniture or a building.”
It is like the saying “Together we can do greater things”.
So in programming, a module can consist of functions, classes, and variables, saved with a default file extension, which can be used repeatedly. In Python, it is a file saved with “.py” extension, containing function, classes, and variables. Modules are used to break complex programs into smaller bits.
Generally, Modular programming breaks programming tasks into smaller, manageable, simpler, reusable tasks/subtasks.
Imagine a Wardrobe with two drawers, hanging space and four sub-folding partitions. Each of these sessions serves different purposes and can be used independently of the others, however, they collectively serve a bigger purpose — “the Wardrobe”, making it easier to store, organize and access items accordingly. Now imagine if these sessions were not embedded in one and had to be kept separately, more room space, less accessibility, and efficiency. Not nice right?
This is exactly the intuition behind “Modules”.
How to use modules:
In python, there are two types of modules.
- User-defined
- Built-in
- User-defined modules: Just as in the name, these are modules created outside the default python modules. Since Python is an open-source programming language, it provides options for users to add in functionalities or tweak and turn codes to preference, either to improve a process or create a unique one. Any Python program can be a module.
- Built-in modules: These are modules contained in the standard Python library.
The focus of this article is the “Built-in” modules.
Modules, whether built-in or user-defined can be used by importing, using the module’s name, then calling the preferred function/class or variable.
The syntax is:
import <module name>
Example:
import math
As seen earlier, the ‘random’ module was imported, then a function from the module(random.choice()) was called. This function simply generated random options/choices, most often used to generate random items from a data structure. So in the program, it was used to generate random options for the computer as the game proceeds. Notice how each time, a random choice gets generated.
Generally, Here’s how the entire program flows:
- A welcome message was created
- A user/player name was created and a personalized greeting printing. We all feel more attracted to a personalized program.
- The user makes a choice from either ‘Rock, paper, or scissors’.
- A random choice was generated for the computer and compared to the user’s choice.
- According to the rules of the game( Rock beats Scissors, Scissors beats Paper, and Paper beats Rock). So multiple conditional statements were created to validate these, using the logical operators(and, or) to map two conditions together.
- A verdict is passed for either a tie, a win, or a loss.
We could make this more exciting by adding score points for wins or losses.
To do this, simply create two variables for both the player and the computer. See the image below:
Then in the conditional statements block, for each win and losses, include an instance for a score increment, using this simple python variable trick. See image below:
Can you think of an extra feature to add to improve the functionality?
Remember the previous series on ‘Chocolate Store’, where an option to exit or continue the game was included? Do you think you can add that here? Give it a try! Explore, experiment, get bugs, and find a way to debug them.
HAVE FUN CODING!
Thank you for reading!
Thank you for tuning in once again. If this is the first time coming across this series, find the links to the previous series here:
Find the link to the Github repository(‘rps_game.py, rps_game_modified.py’) and other useful resources below.
CONNECT ON SOCIAL MEDIA
LinkedIn: https://www/linkedin.com/in/aminah-mardiyyah-rufa-i/
Twitter: @diyyah92