Advertisement
Trekky0623

Untitled

Apr 23rd, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import operator, random
  2. from sys import exit
  3.  
  4. defeat = {'rock' : ['paper', 'spock'],
  5. 'paper' : ['scissors', 'lizard'],
  6. 'scissors' : ['spock', 'rock'],
  7. 'spock' : ['lizard', 'paper'],
  8. 'lizard' : ['rock', 'scissors']}
  9.  
  10. verbs = {'scissorspaper' : 'cut',
  11. 'paperrock' : 'covers',
  12. 'rocklizard' : 'crushes',
  13. 'lizardspock' : 'poisons',
  14. 'spockscissors' : 'smashes',
  15. 'scissorslizard' : 'decapitate',
  16. 'lizardpaper' : 'eats',
  17. 'paperspock' : 'disproves',
  18. 'spockrock' : 'vaporizes',
  19. 'rockscissors' : 'crushes'}
  20.  
  21. pwt = [0, 0, 0]
  22.  
  23. moves = {'rock' : 0,
  24. 'paper' : 0,
  25. 'scissors' : 0,
  26. 'spock' : 0,
  27. 'lizard' : 0}
  28.  
  29.  
  30. def menu():
  31. while True:
  32. print("Menu:")
  33. print("1: Play Game")
  34. print("2: Exit")
  35. choice = input()
  36. if choice == '1': play_game()
  37. if choice == '2': end_game()
  38.  
  39.  
  40. def judge(player1, player2):
  41. if player1 == player2: return 0
  42. if player1 in defeat[player2]: return 1
  43. else: return 2
  44.  
  45.  
  46. def ai(moves):
  47. sorted_moves = sorted(moves.items(), key = operator.itemgetter(1))
  48. sorted_moves.reverse()
  49. top_plays = [sorted_moves[0][0]]
  50. for i in range(1, 5):
  51. if sorted_moves[i][1] == sorted_moves[i-1][1]:
  52. top_plays.append(sorted_moves[i][0])
  53. else: break
  54. choices = []
  55. for item in top_plays:
  56. if defeat[item][0] not in choices and defeat[item][0] not in top_plays:
  57. choices.append(defeat[item][0])
  58. if defeat[item][1] not in choices and defeat[item][1] not in top_plays:
  59. choices.append(defeat[item][1])
  60. if choices == []:
  61. player2 = random.choice(top_plays)
  62. else:
  63. player2 = random.choice(choices)
  64. return player2
  65.  
  66.  
  67. def play_game():
  68. player2 = ai(moves)
  69. while True:
  70. player1 = input("Player picks: ").lower()
  71. if player1 in moves: break
  72. moves[player1] += 1
  73. print("Computer picks: %s" % player2)
  74. outcome = judge(player1, player2)
  75. if outcome == 0:
  76. print("It's a tie!")
  77. pwt[0] += 1
  78. pwt[2] += 1
  79. if outcome == 1:
  80. print("%s %s %s. Player wins!" % (player1.title(), verbs[player1+player2], player2.title()))
  81. pwt[0] += 1
  82. pwt[1] += 1
  83. if outcome == 2:
  84. print("%s %s %s. Computer wins!" % (player2.title(), verbs[player2+player1], player1.title()))
  85. pwt[0] += 1
  86.  
  87.  
  88. def end_game():
  89. plays = pwt[0]
  90. wins = pwt[1]
  91. ties = pwt[2]
  92. losses = plays - (wins + ties)
  93. print("Plays: %i" % plays)
  94. print("Wins: %i (%i%%)" % (wins, int(wins*100/plays)))
  95. print("Losses: %i (%i%%)" % (losses, int(losses*100/plays)))
  96. print("Ties: %i (%i%%)" % (ties, int(ties*100/plays)))
  97. exit()
  98.  
  99. menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement