Table of contents
No headings in the article.
Hello World !
We all love to play games. Games are escape from the world and it boost confidence through winning. So, I thought to tweak the programming concepts with games.
In the previous code, we have already started with Treasure Island Game, now its time to learn about my favourite game : Rock, Paper and Scissors.
What is Rock, Paper Scissors ?
It is also commonly abbreviated as RPS and is a common and well-known game that is played by everyone all around the World no matter their gender, age, athletic ability or other qualities, everyone has a chance to Win! It is used to settle disputes between two or more individuals with contradicted views and resolve such issues as who gets the last slice of pizza or who will wash the dishes tonight.
Rules of Game :
Rock wins against scissors.
Paper wins against rock.
Scissors wins against paper.
If you are not familiar with the concept of this game, I urge you to visit :Rock-Paper-Scissor . We are going to play with computer. Are you ready !
Lets start with coding
#Import random module to generate random value for computer
import random
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
#Store ASCII art as list
list1=[rock,paper,scissors]
#Greet the user
print("Welcome to Rock, Paper and Scissors game !")
#Ask the user for his choice
user_choice=int(input("What do you choose? Type 0 for Rock, 1 for Paper and 2 for Scissors.\n"))
#print the userchoice Ascii art using list indexing
print(list1[user_choice])
#Generate random choice for computer using randint module:
#Here we are generating random integers between 0 to 2
computer_choice=random.randint(0,2)
print("Computer choice is : \n")
#print the computerchoice Ascii art using list indexing
print(list1[computer_choice])
#CHECK FOR CONDITIONS
if user_choice>3 or user_choice<0:
print("Invalid input")
elif user_choice==0 and computer_choice==1:
print("You Loose")
elif user_choice==1 and computer_choice==0:
print("You win")
elif user_choice>computer_choice:
print("You win")
elif computer_choice >user_choice:
print("You loose")
else:
print("Its a draw")
STEP 1 :
(a). Import Random Module : Random module is to generate random numbers in python.
#Import random module to generate random value for computer
import random
STEP 2:
(b). Assign multiline ASCII art to rock, paper an scissor for making it user friendly.
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
STEP 3: Store ASCII art as list To get your customise art. Check : ASCII art
list1=[rock,paper,scissors]
STEP 4: Greet the user and ask for his choice. Also print its ASCII Value with list indexing
print("Welcome to Rock, Paper and Scissors game !")
user_choice=int(input("What do you choose? Type 0 for Rock, 1 for Paper and 2 for Scissors.\n"))
print(list1[user_choice])
STEP 5: Generate random-integer number between 0-2 with random.randint and assign it as computer choice
computer_choice=random.randint(0,2)
print("Computer choice is : \n")
print(list1[computer_choice])
STEP 6: Check for conditions with if-else statement as defined in game rules:
Rules of Game :
Rock wins against scissors.
Paper wins against rock.
Scissors wins against paper.**
if user_choice>3 or user_choice<0:
print("Invalid input")
elif user_choice==0 and computer_choice==1:
print("You Loose")
elif user_choice==1 and computer_choice==0:
print("You win")
elif user_choice>computer_choice:
print("You win")
elif computer_choice >user_choice:
print("You loose")
else:
print("Its a draw")