rock-paper-scissors (also known as scissors-rock-paper or other variants) is a hand game usually played between two people, in which each player simultaneously forms one of three shapes with an outstretched hand.
These shapes are "rock" (a closed fist), "paper" (a flat hand), and "scissors" (a fist with the index finger and middle finger extended, forming a V).
"Scissors" is identical to the two-fingered V sign (also indicating "victory" or "peace") except that it is pointed horizontally instead of being held upright in the air.
A simultaneous, zero-sum game, it has only two possible outcomes: a draw, or a win for one player and a loss for the other.
A player who decides to play rock will beat another player who has chosen scissors ("rock crushes scissors" or sometimes "blunts scissors"), but will lose to one who has played paper ("paper covers rock").
A play of paper will lose to a play of scissors ("scissors cuts paper").
If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.
In the program, the user gets the first chance to pick the option among rock, paper and scissor. After that the computer selects from the remaining two choices (randomly), then 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
In this game, the randint() inbuilt function is used for generating random integer value within the given range.
# import random module
import random
# Print multiline instruction perform string concatenation of string
print(
"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"
)
def main():
while True:
print("Enter choice \n 1. rock \n 2. paper \n 3. scissor \n")
# take the input from user
choice = int(input("User turn: "))
# OR is the short-circuit operator
# if any one of the condition is true
# then it return True value
# looping until user enter invalid input
while choice > 3 or choice < 1:
choice = int(input("Enter valid input: "))
# initialize value of choice_name variable
# corresponding to the choice value
if choice == 1:
choice_name = "rock"
elif choice == 2:
choice_name = "paper"
else:
choice_name = "scissor"
# print user choice
print(f"User choice is: {choice_name}")
print("\nNow it is Computer's turn.......")
# Computer chooses randomly any number among 1 , 2 and 3. Using randint method of random module
comp_choice = random.randint(1, 3)
# looping until comp_choice value is equal to the choice value
while comp_choice == choice:
comp_choice = random.randint(1, 3)
# initialize value of comp_choice_name variable corresponding to the choice value
if comp_choice == 1:
comp_choice_name = "rock"
elif comp_choice == 2:
comp_choice_name = "paper"
else:
comp_choice_name = "scissor"
print(f"Computer choice is: {comp_choice_name}")
print(f"{choice_name} V/s {comp_choice_name}")
# condition for winning
if (choice == 1 and comp_choice == 2) or (choice == 2 and comp_choice == 1):
print("paper wins => ", end="")
result = "paper"
elif (choice == 1 and comp_choice == 3) or (choice == 3 and comp_choice == 1):
print("rock wins =>", end="")
result = "rock"
else:
print("scissor wins =>", end="")
result = "scissor"
# Print as either user or computer wins
if result == choice_name:
print("<== So User wins ==>")
else:
print("<== So Computer wins ==>")
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
# After coming out of the while loop we print thanks for playing
print("\nThanks for playing")
if __name__ == "__main__":
main()
Winning Rules of the Rock paper scissor game as follows: rock vs paper -> paper wins rock vs scissor -> rock wins paper vs scissor -> scissor wins Enter choice 1. rock 2. paper 3. scissor User turn: 1 User choice is: rock Now it is Computer's turn....... Computer choice is: paper rock V/s paper paper wins => <== So Computer wins ==> Do you want to play again? (Y/N) y Enter choice 1. rock 2. paper 3. scissor User turn: 2 User choice is: paper Now it is Computer's turn....... Computer choice is: scissor paper V/s scissor scissor wins =><== So Computer wins ==> Do you want to play again? (Y/N) y Enter choice 1. rock 2. paper 3. scissor User turn: 3 User choice is: scissor Now it is Computer's turn....... Computer choice is: rock scissor V/s rock rock wins =><== So Computer wins ==> Do you want to play again? (Y/N) n Thanks for playing