Rock-Paper-Scissors is a Python project where player selects one of three options: Rock, Paper, or Scissors. The computer simultaneously makes a random selection. The program compares both choices using the game rules and declares the winner. The game can be played repeatedly until the user decides to quit.
Example:
1. User Choice: Rock
Computer Choice: Scissors
Result: User Wins
2. User Choice: Paper
Computer Choice: Paper
Result: Draw
Winning Rules
- Rock defeats Scissors.
- Scissors defeats Paper.
- Paper defeats Rock.
- If both players choose the same option, the round ends in a draw.
In this game, randint() built-in function is used for generating random integer values within the given range.
Algorithm
- Display the rules of the game.
- Accept the user's choice.
- Validate the input and ensure it is within the allowed options.
- Generate the computer's choice randomly.
- Compare both choices and determine the winner.
- Display the result of the round.
- Ask the user whether they want to play again.
- Repeat the game until the user chooses to exit.
Implementation
Output:
Winning rules of the game ROCK PAPER SCISSORS are:
Rock vs Paper -> Paper wins
Rock vs Scissors -> Rock wins
Paper vs Scissors -> Scissors wins
Enter your choice
1 - Rock
2 - Paper
3 - Scissors
Enter your choice:
1 - Rock
2 - Paper
3 - Scissors
Enter your choice: 1
User choice is: Rock
Computer choice is: Scissors
<== User wins! ==>
Do you want to play again? (Y/N)
N
Thanks for playing!
Explanation:
- Store Available Choices: A list is used to store the three possible options: Rock, Paper, and Scissors.
- Validate User Input: The program uses try-except and conditional checks to ensure the user enters a valid choice.
- Generate Random Selection: The random.randint() function generates a random choice for the computer.
- Map Numbers to Choices: User and computer selections are converted from numeric values to their corresponding string representations.
- Determine the Winner: Conditional statements compare the selected options and identify the winner or a draw.
- Control Game Execution: A while loop allows multiple rounds to be played until the user chooses to exit the game.