Advertisement
Guest User

Untitled

a guest
Jun 25th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. #! /usr/bin/env python3                                                                                                
  2.  
  3. '''                                                                                                                    
  4. scissors cuts paper                                                                                                    
  5. paper covers rock                                                                                                      
  6. rock crushes lizard                                                                                                    
  7. lizard poisons Spock                                                                                                  
  8. Spock smashes scissors                                                                                                
  9. scissors decapitates lizard                                                                                            
  10. lizard eats paper                                                                                                      
  11. paper disproves Spock                                                                                                  
  12. Spock vaporizes rock                                                                                                  
  13. rock crushes scissors                                                                                                  
  14. '''
  15.  
  16. from random import choice
  17. import readline
  18.  
  19. RULES = list(map(str.split, __doc__.lower().strip().split('\n')))
  20.  
  21. OPTIONS = ({winner for winner, verb, loser in RULES}
  22.            | {loser for winner, verb, loser in RULES})
  23.  
  24. PROMPT = "Please select: " + ', '.join(sorted(OPTIONS)) + '\n'
  25.  
  26. ################################################################################                                      
  27.  
  28. def test(a, b, rules=RULES):
  29.     a, b = a.lower(), b.lower()
  30.     for rule in rules:
  31.         winner, verb, loser = rule
  32.         if (a, b) == (winner, loser): return a, ' '.join(rule)
  33.         if (b, a) == (winner, loser): return b, ' '.join(rule)
  34.  
  35. ################################################################################                                      
  36.  
  37. if __name__ == '__main__':
  38.     print(__doc__)
  39.     print()
  40.     try:
  41.         while True:
  42.             user = None
  43.             while user not in OPTIONS:
  44.                 user = input(PROMPT).lower()
  45.             machine = choice(list(OPTIONS))
  46.             try:
  47.                 winner, rule = test(user, machine)
  48.                 result = 'you win' if user == winner else 'you lose'
  49.                 print(user, 'v.', machine, '->', result, '\t(%s)' % rule)
  50.             except: print('tie')
  51.             print()
  52.     except EOFError: pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement