Skip to content

Commit f0eeab8

Browse files
committed
Rock Paper Scissor completed
1 parent 47a9443 commit f0eeab8

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

days/13-15-text-games/RockPaperScissors/RockPaperScissors.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,47 @@ def print_header():
2727
print()
2828

2929
def build_the_three_rolls():
30-
rock = Roll('Rock')
31-
paper = Roll('Paper')
32-
scissors = Roll('Scissors')
33-
valid_rolls = ['Rock','Paper','Scissors']
30+
rock = Roll('rock')
31+
paper = Roll('paper')
32+
scissors = Roll('scissors')
33+
rolls = [rock, paper, scissors]
34+
return rolls
35+
36+
def get_player_roll():
37+
valid_choice = False
38+
while not valid_choice:
39+
choice = input("Choose Rock, Paper, or Scissors: ").lower()
40+
if choice == 'rock':
41+
return Roll('rock')
42+
elif choice == 'paper':
43+
return Roll('paper')
44+
elif choice == 'scissors':
45+
return Roll('scissors')
46+
47+
3448

3549
def game_loop(player1, player2, rolls):
36-
round1 = Roll()
3750
count = 1
38-
while count < 3:
39-
p2_roll = roll.
40-
p1_roll = input("{}, roll: 1..2..3..".format(player1.name))
51+
while player1.score < 3 and player2.score < 3:
52+
p2_roll = random.choice(rolls)
53+
p1_roll = get_player_roll()
54+
win = p1_roll.can_defeat(p2_roll)
55+
if win:
56+
player1.score += 1
57+
print("{} {} {}. {} wins!".format(p1_roll.name, p1_roll.action, p2_roll.name, player1.name))
58+
elif p2_roll.name == p1_roll.name:
59+
print("it's a tie. Try again")
60+
else:
61+
player2.score += 1
62+
print("{} {} {}. Computer wins.".format(p2_roll.name, p2_roll.action, p1_roll.name))
4163

42-
outcome = p1_roll.can_defeat(p2_roll)
43-
print(outcome)
64+
print("{}: {} {}: {}".format(player1.name, player1.score, player2.name, player2.score))
4465

45-
count += 1
66+
if player1.score == 3:
67+
print("Congrats, {}. You are the champion!".format(player1.name))
68+
else:
69+
print(" ")
70+
print("Sorry. Computer wins the match.")
4671

4772
# Compute who won
4873

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
class Player():
22
def __init__(self, name):
33
self.name = name
4+
self.score = 0

days/13-15-text-games/RockPaperScissors/roll.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,14 @@
22

33
class Roll():
44

5-
def __init__(self, name, defeats):
5+
def __init__(self, name):
66
self.name = name
7-
self.defeats = defeats
8-
self.valid_rolls = ["Rock", "Paper", "Scissors"]
9-
10-
def get_random(self):
11-
return random.choices("Rock", "Paper", "Scissors")
12-
13-
def choose(self):
14-
valid_choice = False
15-
while not valid_choice:
16-
choice = input("Choose a roll (Rock, Paper, Scissors): ")
17-
if choice in self.valid_rolls:
18-
valid_choice = True
19-
return choice
7+
action = {'rock': 'smashes','paper':'covers','scissors':'cuts'}
8+
self.action = action[name]
209

2110
def can_defeat(self,roll):
22-
if (roll == self.defeats):
11+
win = {'rock': 'scissors','paper':'rock','scissors':'paper'}
12+
if (roll.name == win[self.name]):
2313
return True
2414
return False
2515

0 commit comments

Comments
 (0)