Skip to content

Commit e6a03c8

Browse files
author
gsp
committed
Code for Rock Scissors Paper
1 parent 1fc4706 commit e6a03c8

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import sys
2+
3+
# sys.path.append(".")
4+
from rps import Roll, Player
5+
6+
7+
def print_header():
8+
print('----------------------------------------')
9+
print('--------- Rock, Paper, Scissors --------')
10+
print('----------------------------------------')
11+
12+
13+
def get_player_name():
14+
try:
15+
player = input('>>>> Enter your name: ')
16+
except (EOFError, KeyboardInterrupt):
17+
print(f'Thanks for playing!, Bye')
18+
exit(0)
19+
return player
20+
21+
22+
def get_player_choice():
23+
try:
24+
while True:
25+
choice = input(">>>> Select [R]ock, [P]aper, [S]cissors or [A]bort to exit game: ")
26+
27+
choice_dict = {
28+
'r': 'rock',
29+
'p': 'paper',
30+
's': 'scissors',
31+
'a': 'abort'
32+
}
33+
34+
if choice_dict.get(choice.lower()) == 'abort':
35+
print('Thanks for playing')
36+
exit(0)
37+
38+
elif choice_dict.get(choice.lower()):
39+
return choice_dict.get(choice.lower())
40+
41+
else:
42+
print(f'Wrong entry, please select [R], [P], [S] for playing or [A] to abort and exit: ')
43+
44+
except (EOFError, KeyboardInterrupt):
45+
print(f'Thanks for playing!, Bye')
46+
exit(0)
47+
48+
49+
if __name__ == '__main__':
50+
print_header()
51+
player = get_player_name()
52+
53+
player_a_name = Player(player).name
54+
player_b_name = Player('computer').name
55+
score = {player_a_name: 0, 'computer': 0}
56+
57+
while True:
58+
for game in range(3):
59+
player_a_choice = get_player_choice()
60+
player_b_choice = Roll().name()
61+
if Roll(player_a_choice).win() == player_b_choice:
62+
print(f"{player_a_name} picked: {player_a_choice} which beats {player_b_name}'s {player_b_choice}")
63+
score[player_a_name] += 1
64+
elif Roll(player_b_choice).win() == player_a_choice:
65+
print(f"{player_b_name} picked: {player_b_choice} which beats {player_a_name}'s {player_a_choice}")
66+
score[player_b_name] += 1
67+
else:
68+
print(
69+
f'{player_a_name} picked: {player_a_choice}, {player_b_name} picked: {player_b_choice} you are tied!')
70+
71+
if score[player_a_name] > score['computer']:
72+
print(
73+
f'Winner: {player_a_name}, Score: {player_a_name}: {score[player_a_name]}, {player_b_name}: {score[player_b_name]}')
74+
score = {player_a_name: 0, 'computer': 0}
75+
elif score[player_a_name] < score['computer']:
76+
print(
77+
f'Winner: {player_b_name}, Score: {player_a_name}: {score[player_a_name]}, {player_b_name}: {score[player_b_name]}')
78+
score = {player_a_name: 0, 'computer': 0}
79+
else:
80+
print(f'You are tied, Score: {player_a_name}: {score[player_a_name]}, {player_b_name}: {score[player_b_name]}')
81+
score = {player_a_name: 0, 'computer': 0}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import random
2+
3+
4+
class Roll:
5+
def __init__(self, roll_name=''):
6+
if roll_name == '':
7+
self.roll_name = random.choice(['rock', 'scissors', 'paper'])
8+
else:
9+
self.roll_name = roll_name.lower()
10+
11+
def win(self):
12+
d = dict(rock='scissors', scissors='paper', paper='rock')
13+
return d[self.roll_name]
14+
15+
def name(self):
16+
return str(self.roll_name)
17+
18+
19+
class Player:
20+
def __init__(self, name):
21+
self.name = name
22+
23+
def name(self):
24+
return str(self.name())
25+
26+
def play(self):
27+
return Roll()

0 commit comments

Comments
 (0)