|
1 | 1 | import random |
2 | 2 |
|
3 | | -print("---------------------------") |
4 | | -print(" Rock Paper Scissors v1") |
5 | | -print("---------------------------") |
6 | | - |
7 | | -player_1 = "You" |
8 | | -player_2 = "Computer" |
9 | | - |
10 | | -rolls = ['rock', 'paper', 'scissors'] |
11 | | - |
12 | | -roll1 = input(f"{player_1}, what is your roll? [rock, paper, scissors]: ") |
13 | | -roll1 = roll1.lower().strip() |
14 | | -if roll1 not in rolls: |
15 | | - print(f"Sorry {player_1}, {roll1} is not a valid play!") |
16 | | - |
17 | | -roll2 = random.choice(rolls) |
18 | | - |
19 | | -print(f"{player_1} roll {roll1}") |
20 | | -print(f"{player_2} rolls {roll2}") |
21 | | - |
22 | | -# Test for a winner |
23 | | -# Rock |
24 | | -# Rock -> tie |
25 | | -# Paper -> lose |
26 | | -# Scissors -> win |
27 | | -# Paper |
28 | | -# Rock -> win |
29 | | -# Paper -> tie |
30 | | -# Scissors -> lose |
31 | | -# Scissors |
32 | | -# Rock -> lose |
33 | | -# Paper -> win |
34 | | -# Scissors -> tie |
35 | | - |
36 | | -winner = None |
37 | | - |
38 | | -if roll1 == roll2: |
39 | | - print("The play was tied!") |
40 | | -elif roll1 == 'rock': |
41 | | - if roll2 == 'paper': |
42 | | - winner = player_2 |
43 | | - elif roll2 == 'scissors': |
44 | | - winner = player_1 |
45 | | -elif roll1 == 'paper': |
46 | | - if roll2 == 'scissors': |
47 | | - winner = player_2 |
48 | | - elif roll2 == 'rock': |
49 | | - winner = player_1 |
50 | | -elif roll1 == 'scissors': |
51 | | - if roll2 == 'rock': |
52 | | - winner = player_2 |
53 | | - elif roll2 == 'paper': |
54 | | - winner = player_1 |
55 | | - |
56 | | -print("The game is over!") |
57 | | -if winner is None: |
58 | | - print("It was a tie!") |
59 | | -else: |
60 | | - print(f'{winner} takes the game!') |
61 | 3 |
|
| 4 | +def main(): |
| 5 | + show_header() |
62 | 6 |
|
| 7 | + player = "You" |
| 8 | + ai = "Computer" |
| 9 | + |
| 10 | + play_game(player, ai) |
| 11 | + |
| 12 | + |
| 13 | +def show_header(): |
| 14 | + print("---------------------------") |
| 15 | + print(" Rock Paper Scissors v1") |
| 16 | + print("---------------------------") |
| 17 | + |
| 18 | + |
| 19 | +def play_game(player_1, player_2): |
| 20 | + rolls = ['rock', 'paper', 'scissors'] |
| 21 | + |
| 22 | + roll1 = get_roll(player_1, rolls) |
| 23 | + roll2 = random.choice(rolls) |
| 24 | + |
| 25 | + if not roll1: |
| 26 | + print("Can't play that, exiting") |
| 27 | + return |
| 28 | + |
| 29 | + print(f"{player_1} roll {roll1}") |
| 30 | + print(f"{player_2} rolls {roll2}") |
| 31 | + |
| 32 | + # Test for a winner |
| 33 | + # Rock |
| 34 | + # Rock -> tie |
| 35 | + # Paper -> lose |
| 36 | + # Scissors -> win |
| 37 | + # Paper |
| 38 | + # Rock -> win |
| 39 | + # Paper -> tie |
| 40 | + # Scissors -> lose |
| 41 | + # Scissors |
| 42 | + # Rock -> lose |
| 43 | + # Paper -> win |
| 44 | + # Scissors -> tie |
| 45 | + |
| 46 | + winner = None |
| 47 | + |
| 48 | + if roll1 == roll2: |
| 49 | + print("The play was tied!") |
| 50 | + elif roll1 == 'rock': |
| 51 | + if roll2 == 'paper': |
| 52 | + winner = player_2 |
| 53 | + elif roll2 == 'scissors': |
| 54 | + winner = player_1 |
| 55 | + elif roll1 == 'paper': |
| 56 | + if roll2 == 'scissors': |
| 57 | + winner = player_2 |
| 58 | + elif roll2 == 'rock': |
| 59 | + winner = player_1 |
| 60 | + elif roll1 == 'scissors': |
| 61 | + if roll2 == 'rock': |
| 62 | + winner = player_2 |
| 63 | + elif roll2 == 'paper': |
| 64 | + winner = player_1 |
| 65 | + |
| 66 | + print("The game is over!") |
| 67 | + if winner is None: |
| 68 | + print("It was a tie!") |
| 69 | + else: |
| 70 | + print(f'{winner} takes the game!') |
| 71 | + |
| 72 | + |
| 73 | +def get_roll(player_name, rolls): |
| 74 | + roll = input(f"{player_name}, what is your roll? [rock, paper, scissors]: ") |
| 75 | + roll = roll.lower().strip() |
| 76 | + if roll not in rolls: |
| 77 | + print(f"Sorry {player_name}, {roll} is not a valid play!") |
| 78 | + return None |
| 79 | + |
| 80 | + return roll |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + main() |
0 commit comments