-
Notifications
You must be signed in to change notification settings - Fork 1
/
rock_scissor_paper.py
128 lines (106 loc) · 2.4 KB
/
rock_scissor_paper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Author: Davide
# Date: 01/12/2019
import random as rand
import sys
def computer_move():
a = 1
b = 3
computer_choose = generate_computer_choose(a, b)
print_hand(computer_choose)
return computer_choose
def user_move():
a = 1
b = 3
print("Choose an option between 1 and 3 where:")
print("1- Paper")
print("2 - Rock")
print("3- Scissor")
print("Press any other key to stop the application")
user_choose = int(input("Choose: "))
if 1 <= user_choose <= 3:
print_hand(user_choose)
return user_choose
else:
print("Goodbye")
# Stop the application
sys.exit(0)
def print_hand(choose):
if choose == 1:
print_paper()
elif choose == 2:
print_rock()
else:
print_scissor()
return
def print_paper():
# Paper
print("""
_______
---' ____)____
_______)
________)
________)
---.___________)
""")
return
def print_rock():
# Rock
print("""
_______
---' ___)
(____)
(____)
(___)
---.__(__)
""")
return
def print_scissor():
# Scissors
print("""
_______
---' ___)____
_____)
_________)
(___)
---.__(__)
""")
return
def generate_computer_choose(a, b):
# Create a random integer between 1 and 3, that is going to be the options for the game.
computer_choose = rand.randint(a, b)
return computer_choose
def get_winner(user_choose, computer_choose):
# Get the winner of the game.
if user_choose == computer_choose:
return "nobody"
if user_choose == 1:
if computer_choose == 2:
return "user"
else:
return "computer"
elif user_choose == 2:
if computer_choose == 1:
return "computer"
else:
return "user"
else:
if computer_choose == 1:
return "user"
else:
return "computer"
def print_winner(wi):
if wi == "nobody":
print("Nobody won")
return
elif wi == "user":
print("User won")
return
else:
print("Computer won")
return
# Core of the program
while 1:
user_option = user_move()
computer_option = computer_move()
winner = get_winner(user_option, computer_option)
print_winner(winner)