Rock Paper Scissors
In this version of the two-player hand game also known as Rochambeau or jan-ken-pon, the player faces off against the computer. You can pick either rock, paper, or scissors. Rock beats scissors, scissors beats paper, and paper beats rock. This program adds some brief pauses for suspense.
For a variation of this game, see Project 60, “Rock Paper Scissors (Always-Win Version).”
The Program in Action
When you run rockpaperscissors.py, the output will look like this:
Rock, Paper, Scissors, by Al Sweigart [email protected]
- Rock beats scissors.
- Paper beats rocks.
- Scissors beats paper.
0 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
> r
ROCK versus...
1...
2...
3...
SCISSORS
You win!
1 Wins, 0 Losses, 0 Ties
Enter your move: (R)ock (P)aper (S)cissors or (Q)uit
--snip--
How It Works
The game logic for Rock Paper Scissors is fairly straightforward, and we implement it here with if-elif statements. To add a bit of suspense, lines 45 to 51 count down before revealing the opponent’s move, with brief pauses between counts. This gives the player a period in which their excitement builds about the results of the game. Without this pause, the results would appear as soon as the player entered their move—a bit anticlimactic. It doesn’t take a lot of code to improve the user experience for the player.
1. """Rock, Paper, Scissors, by Al Sweigart [email protected]
2. The classic hand game of luck.
3. View this code at https://nostarch.com/big-book-small-python-projects
4. Tags: short, game"""
5.
6. import random, time, sys
7.
8. print('''Rock, Paper, Scissors, by Al Sweigart [email protected]
9. - Rock beats scissors.
10. - Paper beats rocks.
11. - Scissors beats paper.
12. ''')
13.
14. # These variables keep track of the number of wins, losses, and ties.
15. wins = 0
16. losses = 0
17. ties = 0
18.
19. while True: # Main game loop.
20. while True: # Keep asking until player enters R, P, S, or Q.
21. print('{} Wins, {} Losses, {} Ties'.format(wins, losses, ties))
22. print('Enter your move: (R)ock (P)aper (S)cissors or (Q)uit')
23. playerMove = input('> ').upper()
24. if playerMove == 'Q':
25. print('Thanks for playing!')
26. sys.exit()
27.
28. if playerMove == 'R' or playerMove == 'P' or playerMove == 'S':
29. break
30. else:
31. print('Type one of R, P, S, or Q.')
32.
33. # Display what the player chose:
34. if playerMove == 'R':
35. print('ROCK versus...')
36. playerMove = 'ROCK'
37. elif playerMove == 'P':
38. print('PAPER versus...')
39. playerMove = 'PAPER'
40. elif playerMove == 'S':
41. print('SCISSORS versus...')
42. playerMove = 'SCISSORS'
43.
44. # Count to three with dramatic pauses:
45. time.sleep(0.5)
46. print('1...')
47. time.sleep(0.25)
48. print('2...')
49. time.sleep(0.25)
50. print('3...')
51. time.sleep(0.25)
52.
53. # Display what the computer chose:
54. randomNumber = random.randint(1, 3)
55. if randomNumber == 1:
56. computerMove = 'ROCK'
57. elif randomNumber == 2:
58. computerMove = 'PAPER'
59. elif randomNumber == 3:
60. computerMove = 'SCISSORS'
61. print(computerMove)
62. time.sleep(0.5)
63.
64. # Display and record the win/loss/tie:
65. if playerMove == computerMove:
66. print('It\'s a tie!')
67. ties = ties + 1
68. elif playerMove == 'ROCK' and computerMove == 'SCISSORS':
69. print('You win!')
70. wins = wins + 1
71. elif playerMove == 'PAPER' and computerMove == 'ROCK':
72. print('You win!')
73. wins = wins + 1
74. elif playerMove == 'SCISSORS' and computerMove == 'PAPER':
75. print('You win!')
76. wins = wins + 1
77. elif playerMove == 'ROCK' and computerMove == 'PAPER':
78. print('You lose!')
79. losses = losses + 1
80. elif playerMove == 'PAPER' and computerMove == 'SCISSORS':
81. print('You lose!')
82. losses = losses + 1
83. elif playerMove == 'SCISSORS' and computerMove == 'ROCK':
84. print('You lose!')
85. losses = losses + 1
After entering the source code and running it a few times, try making experimental changes to it. On your own, you can also try to figure out how to do the following:
- Add “Lizard” and “Spock” moves to the game. Lizard poisons Spock and eats paper, but is crushed by rock and decapitated by scissors. Spock breaks scissors and vaporizes rock, but is poisoned by lizard and disproved by paper.
- Allow the player to win a point for each victory and lose a point for each defeat. Upon winning, the player can also take “double or nothing” risks to possibly win 2, 4, 8, 16, and an increasing number of points in subsequent rounds.
Exploring the Program
Try to find the answers to the following questions. Experiment with some modifications to the code and rerun the program to see what effect the changes have.
- What error do you get if you change
random.randint(1, 3)on line 54 torandom.randint(1, 300)? - What happens if you change
playerMove == computerMoveon line 65 toTrue?