|
| 1 | +import sys |
| 2 | +import random |
| 3 | +from enum import Enum |
| 4 | + |
| 5 | + |
| 6 | +def rps(name='PlayerOne'): |
| 7 | + game_count = 0 |
| 8 | + player_wins = 0 |
| 9 | + python_wins = 0 |
| 10 | + |
| 11 | + def play_rps(): |
| 12 | + nonlocal name |
| 13 | + nonlocal player_wins |
| 14 | + nonlocal python_wins |
| 15 | + |
| 16 | + class RPS(Enum): |
| 17 | + ROCK = 1 |
| 18 | + PAPER = 2 |
| 19 | + SCISSORS = 3 |
| 20 | + |
| 21 | + playerchoice = input( |
| 22 | + f"\n{name}, please enter... \n1 for Rock,\n2 for Paper, or \n3 for Scissors:\n\n") |
| 23 | + |
| 24 | + if playerchoice not in ["1", "2", "3"]: |
| 25 | + print(f"{name}, please enter 1, 2, or 3.") |
| 26 | + return play_rps() |
| 27 | + |
| 28 | + player = int(playerchoice) |
| 29 | + |
| 30 | + computerchoice = random.choice("123") |
| 31 | + |
| 32 | + computer = int(computerchoice) |
| 33 | + |
| 34 | + print(f"\n{name}, you chose {str(RPS(player)).replace('RPS.', '').title()}.") |
| 35 | + print( |
| 36 | + f"Python chose {str(RPS(computer)).replace('RPS.', '').title()}.\n" |
| 37 | + ) |
| 38 | + |
| 39 | + def decide_winner(player, computer): |
| 40 | + nonlocal name |
| 41 | + nonlocal player_wins |
| 42 | + nonlocal python_wins |
| 43 | + if player == 1 and computer == 3: |
| 44 | + player_wins += 1 |
| 45 | + return f"π {name}, you win!" |
| 46 | + elif player == 2 and computer == 1: |
| 47 | + player_wins += 1 |
| 48 | + return f"π {name}, you win!" |
| 49 | + elif player == 3 and computer == 2: |
| 50 | + player_wins += 1 |
| 51 | + return f"π {name}, you win!" |
| 52 | + elif player == computer: |
| 53 | + return "π² Tie game!" |
| 54 | + else: |
| 55 | + python_wins += 1 |
| 56 | + return f"π Python wins!\nSorry, {name}..π’" |
| 57 | + |
| 58 | + game_result = decide_winner(player, computer) |
| 59 | + |
| 60 | + print(game_result) |
| 61 | + |
| 62 | + nonlocal game_count |
| 63 | + game_count += 1 |
| 64 | + |
| 65 | + print(f"\nGame count: {game_count}") |
| 66 | + print(f"\n{name}'s wins: {player_wins}") |
| 67 | + print(f"\nPython wins: {python_wins}") |
| 68 | + |
| 69 | + print(f"\nPlay again, {name}?") |
| 70 | + |
| 71 | + while True: |
| 72 | + playagain = input("\nY for Yes or \nQ to Quit\n") |
| 73 | + if playagain.lower() not in ["y", "q"]: |
| 74 | + continue |
| 75 | + else: |
| 76 | + break |
| 77 | + |
| 78 | + if playagain.lower() == "y": |
| 79 | + return play_rps() |
| 80 | + else: |
| 81 | + print("\nππππ") |
| 82 | + print("Thank you for playing!\n") |
| 83 | + sys.exit(f"Bye {name}! π") |
| 84 | + |
| 85 | + return play_rps |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + import argparse |
| 92 | + |
| 93 | + parser = argparse.ArgumentParser( |
| 94 | + description="Provides a personalized game experience." |
| 95 | + ) |
| 96 | + |
| 97 | + parser.add_argument( |
| 98 | + "-n", "--name", metavar="name", |
| 99 | + required=True, help="The name of the person playing the game." |
| 100 | + ) |
| 101 | + |
| 102 | + args = parser.parse_args() |
| 103 | + |
| 104 | + rock_paper_scissors = rps(args.name) |
| 105 | + rock_paper_scissors() |
0 commit comments