Rock Paper Scissor
a simple command-line Rock-Paper-Scissor game without using any external game libraries like PyGame.
In this game, the user gets the first chance to pick the option among Rock, paper and scissors. After that computer select from the remaining two choices(randomly), the winner is decided as per the rules.
Winning Rules as follows : Rock vs paper-> paper wins Rock vs scissor-> Rock wins paper vs scissor-> scissor wins.
Source code of The Program
Python
Javascript
Python
import random
# We will now explain the rules of this classic game to the user
print(“Welcome to a Python simulation of the classic game Rock-Paper-Scissors.”)
print(“Winning Rules of the Rock paper scissor game as follows: \n”
+”Rock vs aper –> paper wins \n”
+ “Rock vs scissor->Rock wins \n”
+”paper vs scissor->scissor wins \n”)
while True:
print(“Please enter a choice \n 1. Rock \n 2. Paper \n 3. Scissor \n”)
# Based on these options, we take the user’s choice
choice = int(input(“Please enter the number in accordance with your choice: “))
# looping until user enter invalid input
condition = True # Using a flag to control this loop
while condition:
if choice in (1, 2, 3):
condition = False
else:
choice = int(input(“Please enter a valid input: “))
# We now assign the character in accordance with the number
if choice == 1:
character = ‘Rock’
elif choice == 2:
character = ‘Paper’
else:
character = ‘Scissor’
# We now print this character that the user has chosen
print(“The character you have chosen is: ” + character)
print(“\nNow its the computer’s turn. The computer will randomly choose a character now…….”)
# Using the imported library, the computer will now randomly choose a character based on the number
comp_character = random.randint(1, 3)
# We make sure that the computer doesn’t make the same choice as the user
while comp_character == choice:
comp_character = random.randint(1, 3)
# We initialise the computer character the same way we did for the user
if comp_character == 1:
comp_character_name = ‘Rock’
elif comp_character == 2:
comp_character_name = ‘Paper’
else:
comp_character_name = ‘Scissor’
print(“The computer choice is: ” + comp_character_name)
# We now start the game
print(character + ” Vs ” + comp_character_name)
# We now use the rules for this game
if((choice == 1 and comp_character == 2) or
(choice == 2 and comp_character ==1 )):
print(“Paper wins => “, end = “”)
result = “Paper”
elif((choice == 1 and comp_character == 3) or
(choice == 3 and comp_character == 1)):
print(“Rock wins =>”, end = “”)
result = “Rock”
else:
print(“Scissor wins =>”, end = “”)
result = “Scissor”
# Printing whether the user or computer wins
if result == character:
print(“The user wins!”)
else:
print(“Sorry, the computer wins.”)
# We will give the user the option to continue or not
print(“Do you want to play again? (Y/N)”)
ans = input()
# if user input n or N then condition is True
if ans == ‘n’ or ans == ‘N’:
break
# When the user eventually quits the game, we print this
print(“\nThanks for playing. Hope you enjoyed this game!”)
Javascript
window.alert(“Welcome to a Python simulation of the classic game Rock-Paper-Scissors.”);
window.alert(“Winning Rules of the Rock paper scissor game as follows: \n”
+”Rock vs paper –> paper wins \n”
+ “Rock vs scissor –>Rock wins \n”
+”paper vs scissor –>scissor wins \n”);
var flag= true;
while (flag)
{
window.alert(“Please enter a choice \n 1. Rock \n 2. Paper \n 3. Scissor \n”);
// Based on these options, we take the user’s choice
var choice = window.prompt(“Please enter the number in accordance with your choice: “);
var condition = true;
// looping until user enter invalid input
while (condition)
{
if (choice == 1 || choice == 2 || choice == 3)
{
condition=false;
}
else
{
choice= window.prompt(“Please enter a valid input: “);
}
}
// We now assign the character in accordance with the number
if (choice == 1)
{
var character = ‘Rock’;
}
else if (choice == 2)
{
var character = ‘Paper’;
}
else
{
character= ‘Scissor’;
}
// We now print this character that the user has chosen
window.alert(“The character you have chosen is: ” + character);
window.alert(“\nNow its the computer’s turn. The computer will randomly choose a character now…….”);
// Using the library, the computer will now randomly choose a character based on the number
var comp_character = Math.floor(Math.random()*4+1);
// We make sure that the computer doesn’t make the same choice as the user
while (comp_character == choice)
{
comp_character = Math.floor(Math.random()*4+1);
}
// We initialise the computer character the same way we did for the user
if (comp_character == 1)
{
var comp_character_name = ‘Rock’;
}
else if (comp_character == 2)
{
comp_character_name = ‘Paper’;
}
else
{
comp_character_name = ‘Scissor’;
}
window.alert(“The computer choice is: ” + comp_character_name);
// We now start the game
window.alert(character + ” Vs ” + comp_character_name);
// We now use the rules for this game
if ((choice == 1 && comp_character == 2) || (choice == 2 && comp_character == 1))
{
window.alert(“Paper wins => “);
var result = ‘Paper’;
}
else if ((choice == 1 && comp_character == 3) || (choice == 3 && comp_character == 1))
{
window.alert(“Rock wins => “);
var result = ‘Rock’;
}
else
{
window.alert(“Scissor wins => “);
result= ‘Scissor’;
}
// Printing whether the user or computer wins
if (result == character)
{
window.alert(“The user wins!”);
}
else
{
window.alert(“Sorry, The computer wins”);
}
// We will give the user the option to continue or not
var ans = window.prompt(“Do you want to play again? (Y/N)”);
// if user input n or N then condition is True
if (ans == ‘n’ || ans == ‘N’)
{
flag= false;
}
else
// When the user eventually quits the game, we print this
window.alert(“\n Thanks for playing, Hope you enjoyed this game!”);
}
Sample output
winning Rules of the Rock paper and scissor game as follows:
rock vs paper->paper wins
rock vs scissors->rock wins
paper vs scissors->scissors wins
Enter choice
1. Rock
2. paper
3. scissor
User turn:1
User choice is:Rock
Now its computer turn ……
computer choice is:paper
RockV/spaper
paper wins=>computer wins
do you want to play again?
N