Featured Post

BUILD A TODO LIST IN PYTHON

Image
Build a TODO List in Python Overview The objective is to build a todo list to which we can add items, remove the added items, edit the items and even if required, delete out the entire list itself. We will learn here step by step, how to build the todo list with the above  CRUD (Create, Remove, Update/edit, and Delete ) features.  This is a CRUD list  app  that we are building as we can Create, Remove, Update items in the list and Delete the entire list itself. We will be using Python3 to build this Project. Importing os and time We import time to let the screen pause for a while before clearing  and we import os to clear the screen . Initializing the list We initialize the todo list with an empty list as there are not items added yet to the list and this is represented in the code aby using an empty square braces   toDoList = [] Defining the print list  The def keyword is used to define a function in python. In the below code, we define the function called printList(). When we define

Build Rock Paper Scissors Game in Python

Build  Rock Paper Scissors Game in Python


                                                       


INTRODUCTION 

The Rock, Paper, Scissors game is a very game to build in Python for beginners to understand the basic concepts of python in a working method and also exciting to learn as this is a very fun game to play. I will be guiding you by writing step-by-step explanation and code with detailed explanations to make you actually live through the game of Rock, Paper, Scissors Game in Python. I will be explaining not only the technical aspects of the code, but also, the practical steps involved in the game, using logical analysis and insights of the players' moves, based on the concepts of the game. By doing code and analyzing code and building this fun project in Python, you will be able to understand this programming language in an easier and efficient way, such that you will fall in love with Python, I promise you !



CORE CONCEPTS OF THE GAME 

Players playing this game are told to take action on their moves, whether it is Rock, Paper or Scissors and each time the players take their moves, they are scored based on their moves and it is further explained in the paragraphs below along with the respective code. In general, for this game, it is understood that Rock can smash the Scissors but Rock could be smothered by Paper. Also, Scissors can cut Paper. So, if a player selects Rock, then that player can win if the other player has taken Scissors but if the other player has selected a move of Paper, then the Rock will be smothered by Paper. Likewise, if a player has selected Paper, he can be  ripped and cut by the player who has selected Scissors , but if the other player has selected Rock, then the first player can smother the second player's Rock. 



Code for the main.py file

import getpass from getpass import getpass as input import emoji print(emoji.emojize("Welcome to the game of :rock: and :paper: and :scissors:")) print(emoji.emojize("R stands for Rock :rock:")) print(emoji.emojize("P stands for Paper :paper:")) print(emoji.emojize("S stands for Scissors :scissors:")) print() possible_actions = ["R", "P", "S"] print("Choose your actions",possible_actions) print() score_player1 = 0 score_player2 = 0 while True: player1_action = input("Player1 > ") print() player2_action = input("Player2 > ") print() if(player1_action== "R"): if(player2_action == "R"): print("You both chose Rock, it is a tie!") elif(player2_action == "S"): print("Player1's Rock hits Player2's Scissors! Player1 wins!") score_player1 += 1 elif(player2_action == "P"): print("Player2's Paper covers Player1's Rock! Player2 wins!") score_player2 += 1 elif(player1_action == "P"): if(player2_action == "R"): print("Player1's Paper covers Player2's Rock! Player1 wins!") score_player1 += 1 elif(player2_action == "S"): print("Player2's Scissors cuts Player1's Paper! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Both chose Paper, it is a tie!") elif(player1_action == "S"): if(player2_action == "R"): print("Player2's Rock hits Player1's Scissors! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Player1's Scissors cuts Player2's Paper, Player1 wins! ") score_player1 += 1 elif(player2_action == "S"): print("Both chose Scissors! It is a tie!") print("Player1 has", score_player1, "wins!") print("Player2 has", score_player2, "wins!" ) if score_player1 == 3 and score_player2 == 3: print("Thanks for playing a fantastic game!") exit() else: continue




 





GETTING STARTED WITH IMPORTS

You need to import getpass and the reason you are importing getpass is in order to retain the privacy of the game between the different players. For example, if Player one has selected a move, then, that move will not be visible to the other Players , so that they can not manipulate the game. I am also importing emoji as I want to show some emotions while declaring the score of the Players.

In the following line of code, we create the emojis for this welcome message to be printed out for the game introduction: We then show the lines of code to emojise the different images for rock, paper , scissors so that the user gets a visual of the game .


print(emoji.emojize("Welcome to the game of :rock: and :paper: and :scissors:")) print(emoji.emojize("R stands for Rock :rock:")) print(emoji.emojize("P stands for Paper :paper:")) print(emoji.emojize("S stands for Scissors :scissors:"))




PRINTING OUT THE WELCOME MESSAGES

An essential point that you need to notice is that the code syntax for the in-built print function is mentioned as print() , that is, print function with an empty parenthesis having no parameter inside the parenthesis. This print() function is written in the code block to implement the printing of the above lines of the code in which we wrote the welcome message and also emojized the visuals for the rock, paper and scissors used for the game .


VARIABLES TO CREATE FOR POSSIBLE ACTIONS/MOVES

We need to create a variable for storing all the possible actions or moves or options whatever you can term it as, in order to presume the players' move selections, because in order to get started, we need to have the first player take a move and then the second player need to make a move accordingly and so on and so forth. So, we create a variable by the name of possible_actions  and this is the first variable that we create to allocate the values, R,P,S that is Rock, Paper, Scissors. We show the three values, namely, "R", "P" and "S" respectively inside a list. As you know, a list in python can be shown with string characters in this way : ["R", "P", "S"]



CHOOSING  AND PRINTING THE POSSIBLE OPTIONS/ACTIONS


possible_actions = ["R", "P", "S"]

print("Choose your actions",possible_actions) print()

Now we store the possible options, whether the user chooses Rock, Paper or Scissors and we denote these as "R", "P", and "S" in the list as shown above and assign this list to a variable by the name of possible_actions. Then, we write the print command whereby we write within the parenthesis, "Choose your actions", followed by variable possible_actions. We always write the instructions as a string followed by a comma and then we write the name of the relevant variable and as you can see the variable is not within open and closed inverte commas since a variable is not a string and needs to be written after the comma accompanying a string. When we write the variable inside the above print function, what happens is , in the outcome, we find the values of the variable possible_actions printed out without having to write out the values all the time, we just write the variable name. Below this print function, we write the print() function with a blank parenthesis . The idea of this blank print() function or command is to print out the specified message on the screen in the output.



WRITING  CODE FOR THE INITIAL SCORE OF PLAYERS

score_player1 = 0 score_player2 = 0

Since both the players have not yet started playing , we assign a value of zero to both the players.


CREATING THE WHILE LOOP

while True: player1_action = input("Player1 > ") print() player2_action = input("Player2 > ") print()


In the above while loop as we can see, we have created a while loop by writing while True followed by a colon. This loop signifies the tasks to be performed if the condition is true. True is a Python built-in constant of bool type. So, when you say, while True, it means, when True is True, that is when the while condition is true and usually, this leads to an infinite loop until you break the loop. In this game, the players can go on playing until they exit the game with the exit() function that will help in terminating the loop. We create variables player1_action and player2_action in order to start the game with each player given the chance to take an action to choose the rock,paper,scissors move. You may name the variables as per your preference, such as player1_move or anything else, as long as it is readable to the person who is reading your code. Since these actions being taken are those based on users' generated actions, we assign values to these variables with the help of input() function. Whenever we want values for a variable, based on the value keyed in by the user, we always use the input() function. Hence, we write the code, player1_action = input("Player1 > "), so th e value of the player1_action here depends on what the user enters the value
in the computer screen before him. Note the usage of a closed arrowhead right after "Player1 " and you may also write a : such as "Player1: " these are various ways to show a space for the
user to type in the choice to input. After repeating this step for the second player, we write a print() function with a blank parenthesis right below the player 1 and player 2 action , as shown above. This blank print function will execute the print command for the input keyword, that is, it will print out whatever move or action that is entered or keyed in by the user.


CREATING THE IF-ELIF LOOP

if(player1_action== "R"): if(player2_action == "R"): print("You both chose Rock, it is a tie!")


In this game there are two if loops that we need to create, the first if loop is when you consider the condition when the Player 1 will choose the option "Rock", "Paper" or "Scissors" and the second if - loop within this main if-loop, to determine the various moves of the Player2.
I will explain this in more detail as we go forward in this lesson. Consider this game with a logical mind and imagine the first if loop scenario, which is the main if-elif outer loop. In the above code snippet, we write the if condition when Player 1 chooses the move "Rock" and in that case, if the Player also chooses the move "Rock", (since the two players can not see each others' moves), what should you proclaim as the the result? Yes, we must declare the result as a Tie, right? A tie is what happens when both the players at a given time, makes the same move or option! In the above scenario, when the Player 1 chooses "Rock", then, if the Player 2 also chooses "Rock", then, we declare the result as a tie. Accordingly, we print the statement that both the players have chosen the same move "Rock" and that the result is a tie!



NESTED IF-ELIF-ELSE LOOP CREATED AND EXPLAINED

if(player1_action== "R"):
if(player2_action == "R"): print("You both chose Rock, it is a tie!") elif(player2_action == "S"): print("Player1's Rock hits Player2's Scissors! Player1 wins!") score_player1 += 1 elif(player2_action == "P"): print("Player2's Paper covers Player1's Rock! Player2 wins!") score_player2 += 1 elif(player1_action == "P"): if(player2_action == "R"): print("Player1's Paper covers Player2's Rock! Player1 wins!") score_player1 += 1 elif(player2_action == "S"): print("Player2's Scissors cuts Player1's Paper! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Both chose Paper, it is a tie!") elif(player1_action == "S"): if(player2_action == "R"): print("Player2's Rock hits Player1's Scissors! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Player1's Scissors cuts Player2's Paper, Player1 wins! ") score_player1 += 1 elif(player2_action == "S"): print("Both chose Scissors! It is a tie!")


When the Player1 selected the move "Rock", first we considered the option whereby Player 2 chooses the move "Rock", which we declared as a Tie. Then , we consider the option whereby Player 2 chooses the move, "Scissors"  and in this case, we need to consider who is the winner. Now, we know that Rock is stronger than a pair of Scissors, so, considering the fact that Rock can hit and destroy the Scissors, we print the winner to be Player 1 who chose Rock in the first place. After writing the print() command for declaring the Player 1 as winner whose Rock smashes the Player 2's Scissors, we need to evaluate and write down the score of the players here. We had already initiated the value of both the players' score as zero before the game begins and we also had created variables for determining their scores as score_player1 and score_player2 respectively. In this move, since Player 1 wins, we write the score for Player 1 by writing the code score_player1 += 1 As you already know, this is interpreted officially or logically as score_player1 = score_player1 +1 , that is, this code is showing the addition of scores to the initial score. So, now the Player 1 has scored 0+1 or 1.
Now, the Player 2 has only one more move to make for this session, he has to choose Paper, now in the move where Player 2 chooses Paper, then, the Paper is able to smother the Player 1's Rock and therefore, the winner is going to be Player 2. We declare the winner by writing the print() statement with the mention of the name of the winner. Then, we write out the score of the winner, in this case, the Player 2. So, score_player2 += 1

So, in the first session, we had a game where, Player 1 made a move for Rock, and we  went through the three different moves of the Player 2 in this session, thereby analysing who could be the winner in the different options/moves that the Player 2 makes in the situation when Player 1 chooses the move of Rock. Let us go through this session again, to understand the game in this session and the declaration of the winner and score thereofff. When the Player 1 makes a move for Rock, when the Player 2 makes the move for Rock as well, the result is a Tie as both the players choose the same move. No winners declared! Then, when the Player 2 makes a move for the Scissors, we declare the Player 1 as a winner since his move Rock is able to smash the Player 2's Scissors! So, in this case, Player 1 is a clear winner and we declare his score! After this, we consider the case when Player 2 makes the move for the remaining move, that is, Paper, and in this case, the winner is Player 2, since  his Paper can smother the Player 1's Rock! We declare Player 2 as the winner and also record the score for the winner Player 2.

A very important point to remember here is that we must first evaluate the game moves and the logic behind the game just as if we are playing the game and then only, we must write out the code and after writing out the code, we must analyse it in the light of the game logic and not in the light of code syntaxes, because, in this case, you are bound to make severe mistakes along the way, if you do not understand the basic logic behind the game. Once you understand the logic the way I have explained above, it is so easy to write the code!

Next, we evaluate the move when Player 1 makes a move for Paper


elif(player1_action == "P"): if(player2_action == "R"): print("Player1's Paper covers Player2's Rock! Player1 wins!") score_player1 += 1 elif(player2_action == "S"): print("Player2's Scissors cuts Player1's Paper! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Both chose Paper, it is a tie!")

When Player 1 makes a move for Paper, there are 3 different moves for Player 2 as expected and shown above :

Player 2- If Player 2 chooses the move orP option of Rock, in which case Player 1's Paper will smother Player 2's Rock and in this round, Player 1 is declared a clear winner and the score of player 1 is recorded as shown in the code above, whereby the score of Player 1 again increases by 1. 

However, if Player 2 chooses Scissors , then , Player 2's scissors cuts the Paper of Player 1, therefore in this round, Player 2 is the winner and his score is recorded and increases by 1.

Again, if  Player 2 chooses a move for Paper, then, it is a tie, since Player 1 had already chosen Paper. So, no winner in this situation!

In the next round, we analyse the moves of Player 2 when the Player 1 makes a move for  Scissor s :

elif(player1_action == "S"): if(player2_action == "R"): print("Player2's Rock hits Player1's Scissors! Player2 wins!") score_player2 += 1 elif(player2_action == "P"): print("Player1's Scissors cuts Player2's Paper, Player1 wins! ") score_player1 += 1 elif(player2_action == "S"): print("Both chose Scissors! It is a tie!")


In this situation, when the Player 2 makes a move for Rock, then, the Rock of Player 2 will smash the Scissors of Player 1 , and Player 2 wins in this case and his score is recorded as shown above . Next, the Player 2 makes a move for Paper, in which case Player 1's Scissors could cut through the Paper of Player 2 and thus the winner of this round would be Player 1 and his score is  recorded. Another situation is when Player 2 chooses Scissors , whereby it is a tie , since Player 1 has already chosen Scissors!



PRINTING OUT THE SCORE RESULTS FOR THE PLAYERS


We print out the results by writing the string with scores inside the print() function. Make a note here, that when we write strings in the print function within the parenthesis, the string is the sentence that we want to be printed out on the screen before us and we must write a comma after the string followed by the variable that we want to connect with the string to show the value in the output. For example as shown above in the code, we want to show the score of the player Player 1 so we write, print("Player 1 has ", score_player1, "wins")
The string "Player 1 has " and "wins" will show as is in the screen and the variable score_player mentioned here will not show on the screen as score_player 1, but the value of the score_player1 and you need to remember, NOT to write the variable name within open and close quotes, please refer to the code below.

print("Player1 has", score_player1, "wins!")
print("Player2 has", score_player2, "wins!" )



CREATING A CONDITION TO QUIT/EXIT THE GAME

Next we write a condition for exiting or quitting the game and that is when the score of playaer 1 is equal to the score of player 2 and that equals to 3. In this scenario, the players exit or quit the game . In this conditional if-else loop, we mention in the else condition, that the players will continue to play the game if they do not score a value each of 3 in the game. Until they reach the value of 3, both the players will continue to play until one player wins or both the players reaches the score of 3. Please refer to the code below.

if score_player1 == 3 and score_player2 == 3: print("Thanks for playing a fantastic game!") exit() else: continue


CONCLUSION

We were able to write code to play a fun game of Rock, Paper , Scissors between two players and we also were able to presume and analyze how both the players would choose or make different moves/options between Rock, Paper, Scissors and based on that we were able to code the different moves for the players accordingly as explained above. We were also able to record scores of the players and finally cound which player has more score or if it is a tie between them! We put a condition such that they were not able to play when both of them reaches the score of 3 so that the game does not carry out till a range of infinity! We wrote code to quit or exit the game and also code for a situation when we want to continue the game as explained above.

The source code for the complete code can also be found in my code editor where you can also play the game: 

If you want to fork my code from my GitHub Repository, here is my source code in GitHub












Comments

Popular Posts

Build A Random Quote Machine in React

A Simple Guide to Promises in JavaScript ES6

Welcome to my first Blog Post

How To Fight Programmer's Imposter Syndrome

Top Free Online Websites to Learn Coding

Build A Calculator in React

Guide to Learn Coding Efficiently and Effectively