Rock, Paper, Scissor Game in Python
Hey Guy's,
Welcome to the blog, In this blog you are going to create a Simple Rock, Paper, Scissor Game in python. This project is for Absolute beginners, So "be Ready to Put your hands dirty in Coding".
Here is the Full Code With the comment's written for every thing if you want you can go through it:
https://github.com/AbdulRasheedGameDeveloper/Rock-Paper-Scissor-Game
Step 1:
Import the modules required for the project, here the game.
we are going to use only 1 module named "random". This is used in the computer AI to choose any choice from "Rock Paper Scissor".
Code:
#import modules
import random
Step 2:
create a function and get the user Input. Here we will ask the user/Player to pick any thing from "Rock Paper Scissor".
Code:
def Play():
user = input("your choice? 'r' for 'Rock', 's' for 'Scissor', 'p' for ''Paper' : ")
And the we create a computer which will choose any choice randomly.
Here we will use random.choice and give a List of 3 values as [r, s, p]
Code:
computer_AI = random.choice(['r', 's', 'p'])
Step 3:
Check whether user and computer has grabbed same value if so we'll print that his match is on Tie (or) Something You like.
code:
if user == computer_AI:
#Or else print it
return "It's a Tie Match!!!"
Step 4:
In this step we will check the conditions for if our player has won. if the condition get's true then Player get the message as You've won.
Conditions:
#Player = r, computer = s: Player Wins
#Player = s, computer = p: Player Wins
#Player = p, computer = r: Player Wins
let's create a function and give parameter's as player, opponent. Then write a if statement to check the conditions. Here in the parameters the player refers to the user and the opponent refers to the Computer_AI. With thos code we can check the Player Win condition.
Code:
def is_win(player, opponent):
if((player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r')):
return True
#when the if stament is true the funtion will be true and Player will won the match
this will work when the player condition is true.
Step 5:
we will check if the is_win Funtion is true. (this means if the player condition is true).
if player condition is tru we will print that "You've Won", if not we will print "You've Lost"
we can acheive this with the help of this code:
if is_win(user, computer_AI):
#This will happen when is_win is true
return "You've Won!!!"
#this below return funtion will be called when the computer_AI wins
return "You've Lost, Better Luck Next Time!!!"
Step 6:(Last Step)
Print the Play function to see the result in console.
Code:
#Print the Funtion To Play
print(Play())
That's it you have created a game with the help of python. You can further develop it by adding GUI, best AI etc.... But So far you have created a simple functional Rock Paper Scissor Game in Python. Feel Free To Comment Down Below if you have any doubts Regarding This.
Thank You.
Full Code:
#import modules
import random
def Play():
user = input("your choice? 'r' for 'Rock', 's' for 'Scissor', 'p' for ''Paper' : ")
computer_AI = random.choice(['r', 's', 'p'])
if user == computer_AI:
#Or else print it
return "It's a Tie Match!!!"
if is_win(user, computer_AI):
#This will happen when is_win is true
return "You've Won!!!"
#this below return funtion will be called when the computer_AI wins
return "You've Lost, Better Luck Next Time!!!"
def is_win(player, opponent):
#Player = r, computer = s: Player Wins
#Player = s, computer = p: Player Wins
#Player = p, computer = r: Player Wins
if((player == 'r' and opponent == 's') or (player == 's' and opponent == 'p') or (player == 'p' and opponent == 'r')):
return True
#when the if stament is true the funtion will be true and Player will won the match
#Print the Funtion To Play
print(Play())