Advertisement
gruntfutuk

RockPaperScissorsSpockLizard

Jun 26th, 2018 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Scissors cuts Paper
  3. Paper covers Rock
  4. Rock crushes Lizard
  5. Lizard poisons Spock
  6. Spock smashes Scissors
  7. Scissors decapitates Lizard
  8. Lizard eats Paper
  9. Paper disproves Spock
  10. Spock vaporizes Rock
  11. Rock crushes Scissors
  12. '''
  13.  
  14. from random import choice
  15.  
  16. def check(alpha, beta):
  17.     for rule in RULES:
  18.         winner, verb, loser = rule
  19.         if (alpha, beta) == (winner, loser):
  20.             return alpha, ' '.join(rule)
  21.         if (beta, alpha) == (winner, loser):
  22.             return beta, ' '.join(rule)
  23.     return "", ""  # no winner
  24.  
  25.  
  26. RULES = [rule.split() for rule in __doc__.strip().split('\n')]
  27. OPTIONS = list({winner for winner, verb, loser in RULES}
  28.            | {loser for winner, verb, loser in RULES})
  29. PROMPT = f"{', '.join(sorted(OPTIONS))} (return to quit): "
  30. widest = max(len(option) for option in OPTIONS)
  31.  
  32. print('Rules of the game:')
  33. print(__doc__)
  34. print()
  35. print('Games keep going until you decide to quit by just entering return without a choice')
  36. print('Running totals for wins, loses, and ties are printed each round.')
  37. print()
  38.  
  39. wins = loses = ties = 0
  40.  
  41. while True:
  42.     user = 'to choose'
  43.     while user and user not in OPTIONS:
  44.         user = input(PROMPT).strip().capitalize()
  45.     if not user:
  46.         break
  47.     computer = choice(OPTIONS)
  48.     winner, rule = check(user, computer)
  49.     if not winner:
  50.         result = 'tied'
  51.         ties += 1
  52.     elif user == winner:
  53.         result = 'win'
  54.         wins += 1
  55.     else:  # computer == winner
  56.         result = 'lose'
  57.         loses += 1
  58.     print(f"{user:^{widest}} v. {computer:^{widest}} - {result:4}"
  59.           f" [{wins:3} | {loses:3} | {ties:2}]"
  60.           f" {rule}\n"
  61.           )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement