|
| 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} |
0 commit comments