Advertisement
Guest User

RPSLS.py r/dailyprogrammer

a guest
Apr 23rd, 2014
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Rock, Paper, Scissors, Lizard, Spock
  4. #
  5.  
  6. from random import randint
  7.  
  8. ai_counterattack = { 'Rock': ('Spock', 'Paper'),
  9.                     'Paper': ('Scissors', 'Lizard'),
  10.                     'Scissors': ('Spock', 'Rock'),
  11.                     'Lizard': ('Rock', 'Scissors'),
  12.                     'Spock': ('Lizard', 'Paper') }
  13.  
  14. rules_map = { ('Scissors', 'Paper'): 'Scissors cut paper.',
  15.               ('Paper', 'Rock'): 'Paper covers rock.',
  16.               ('Rock', 'Lizard'): 'Rock crushes lizard.',
  17.               ('Lizard', 'Spock'): 'Lizard poisons spock.',
  18.               ('Spock', 'Scissors'): 'Spock smashes scissors.',
  19.               ('Scissors', 'Lizard'): 'Scissors decapitates lizard.',
  20.               ('Lizard', 'Paper'): 'Lizard eats paper.',
  21.               ('Paper', 'Spock'): 'Paper disproves spock.',
  22.               ('Spock', 'Rock'): 'Spock vaporizes rock.',
  23.               ('Rock', 'Scissors'): 'Rock crushes scissors.' }
  24.  
  25. def result(mode, phand, chand):
  26.   """ Return a result tuple (result, h, c, t) where the last three can be 0 or 1
  27.      according to who won round """
  28.   if phand == chand:
  29.     return ("It's a Tie!", 0, 0, 1)
  30.   res = rules_map.get((phand,chand))
  31.   if res == None:
  32.     res = rules_map.get((chand,phand))
  33.     if mode == 'cc':
  34.       return (res + ' Computer 2 wins!', 0, 1, 0)
  35.     else:
  36.       return (res + ' Computer wins!', 0, 1, 0)
  37.   if mode == 'cc':
  38.     return (res + ' Computer 1 wins!', 1, 0, 0)
  39.   else:
  40.     return (res + ' Player wins!', 1, 0, 0)
  41.  
  42. def smart_ai(phand, history):
  43.   """ Sort the history of human moves, extract those with the same count,
  44.      and choose a counterattack trying to avoid a tie considering the actual player hand """
  45.   sorted_his = sorted(history, key=history.get)
  46.   top_his = [hand for hand in sorted_his if history[hand] == history[sorted_his[-1]]]
  47.   if len(top_his) == 1:
  48.     ai_ca = set(ai_counterattack[top_his[0]]) - set([phand]) # avoid ties
  49.     return ai_ca.pop()
  50.   else:
  51.     ai_ca = set()
  52.     for t in top_his:
  53.       ai_ca |= set(ai_counterattack[t])
  54.     ai_ca -= set([phand]) # avoid ties
  55.     r = randint(0, len(ai_ca)-1)
  56.     while(r > 0):
  57.       ai_ca.pop()
  58.       r-=1
  59.     return ai_ca.pop()
  60.  
  61. def dumb_ai(*args, **kwargs):
  62.   ai_ca = set(ai_counterattack)
  63.   r = randint(0,3)
  64.   while(r >= 0):
  65.     ai_ca.pop()
  66.     r-=1
  67.   return ai_ca.pop()
  68.  
  69. def exit_stats(mode, games, hwins, cwins, ties):
  70.   if games == 0:
  71.     print("\nNo games played!")
  72.     return
  73.   if mode == 'cc':  
  74.     print("\nTotal games played: %d\n"
  75.           "Computer 1 wins: %d %.2f%%\n"
  76.           "Computer 2 wins: %d %.2f%%\n"
  77.           "Ties %d %.2f%%" % (games, cwins, 100*cwins/games, hwins, 100*hwins/games, ties, 100*ties/games))      
  78.   else:
  79.     print("\nTotal games played: %d\n"
  80.         "Computer wins: %d %.2f%%\n"
  81.         "Human wins: %d %.2f%%\n"
  82.         "Ties %d %.2f%%" % (games, cwins, 100*cwins/games, hwins, 100*hwins/games, ties, 100*ties/games))  
  83.  
  84. def main():
  85.   hwins, cwins, ties, total = (0,0,0,0)
  86.   history = { 'Rock': 0, 'Paper':0, 'Scissors': 0, 'Lizard': 0, 'Spock': 0}
  87.   phand = None
  88.  
  89.   print("----------------------------------\n"
  90.         "[ROCK-PAPER-SCISSORS-LIZARD-SPOCK]\n"
  91.         "----------------------------------\n")
  92.  
  93.   purerandom = input("Do you want AI to be pure random? (y/N): ").lower()
  94.   if purerandom == 'y':
  95.     ai_comp_pick = dumb_ai
  96.   else:
  97.     ai_comp_pick = smart_ai
  98.  
  99.   gamemode = input("Choose between human x computer or computer x computer mode (hc/cc): ")
  100.   if gamemode == "cc":
  101.     c1wins = c2wins = 0
  102.     c1hand = c2hand = None
  103.     total = int(input("Enter the number of rounds: "))
  104.     c1history = c2history = { 'Rock': 0, 'Paper':0, 'Scissors': 0, 'Lizard': 0, 'Spock': 0}
  105.     for c in range(total):
  106.       c1hand = ai_comp_pick(c2hand, c2history)
  107.       print("Computer 1 picks: %s" % c1hand)
  108.       c2hand = ai_comp_pick(c1hand, c1history)
  109.       print("Computer 2 picks: %s" % c2hand)
  110.       c1history[c1hand]+=1
  111.       c2history[c2hand]+=1
  112.       res = result(gamemode, c1hand, c2hand)
  113.       print(res[0])
  114.       c1wins+=res[1]
  115.       c2wins+=res[2]
  116.       ties+=res[3]
  117.     exit_stats(gamemode, total, c1wins, c2wins, ties)
  118.     return
  119.  
  120.   print("\nPick Rock, Paper, Scissors, Lizard or Spock!\n"
  121.         "Press q to quit the game\n")
  122.   try:
  123.     while(True):
  124.       phand = input('Player Picks: ').title()
  125.       if phand == 'Q':
  126.         break
  127.       chand = ai_comp_pick(phand, history)
  128.       print("Computer Picks: %s" % chand)
  129.       # to simulate simultaneity of hands the computer only remembers after the game was played
  130.       # otherwise, it would be like if he could se the human hand before showing his own
  131.       history[phand]+=1
  132.       res = result(gamemode, phand, chand)
  133.       print(res[0])
  134.       total+=1
  135.       hwins+=res[1]
  136.       cwins+=res[2]
  137.       ties+=res[3]
  138.   except (KeyboardInterrupt, SystemExit):
  139.     print("\n")
  140.     exit_stats(gamemode, total, hwins, cwins, ties)
  141.   else:
  142.     exit_stats(gamemode, total, hwins, cwins, ties)    
  143.  
  144. if __name__ == '__main__':
  145.   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement