Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import operator, random
- from sys import exit
- defeat = {'rock' : ['paper', 'spock'],
- 'paper' : ['scissors', 'lizard'],
- 'scissors' : ['spock', 'rock'],
- 'spock' : ['lizard', 'paper'],
- 'lizard' : ['rock', 'scissors']}
- verbs = {'scissorspaper' : 'cut',
- 'paperrock' : 'covers',
- 'rocklizard' : 'crushes',
- 'lizardspock' : 'poisons',
- 'spockscissors' : 'smashes',
- 'scissorslizard' : 'decapitate',
- 'lizardpaper' : 'eats',
- 'paperspock' : 'disproves',
- 'spockrock' : 'vaporizes',
- 'rockscissors' : 'crushes'}
- pwt = [0, 0, 0]
- moves = {'rock' : 0,
- 'paper' : 0,
- 'scissors' : 0,
- 'spock' : 0,
- 'lizard' : 0}
- def menu():
- while True:
- print("Menu:")
- print("1: Play Game")
- print("2: Exit")
- choice = input()
- if choice == '1': play_game()
- if choice == '2': end_game()
- def judge(player1, player2):
- if player1 == player2: return 0
- if player1 in defeat[player2]: return 1
- else: return 2
- def ai(moves):
- sorted_moves = sorted(moves.items(), key = operator.itemgetter(1))
- sorted_moves.reverse()
- top_plays = [sorted_moves[0][0]]
- for i in range(1, 5):
- if sorted_moves[i][1] == sorted_moves[i-1][1]:
- top_plays.append(sorted_moves[i][0])
- else: break
- choices = []
- for item in top_plays:
- if defeat[item][0] not in choices and defeat[item][0] not in top_plays:
- choices.append(defeat[item][0])
- if defeat[item][1] not in choices and defeat[item][1] not in top_plays:
- choices.append(defeat[item][1])
- if choices == []:
- player2 = random.choice(top_plays)
- else:
- player2 = random.choice(choices)
- return player2
- def play_game():
- player2 = ai(moves)
- while True:
- player1 = input("Player picks: ").lower()
- if player1 in moves: break
- moves[player1] += 1
- print("Computer picks: %s" % player2)
- outcome = judge(player1, player2)
- if outcome == 0:
- print("It's a tie!")
- pwt[0] += 1
- pwt[2] += 1
- if outcome == 1:
- print("%s %s %s. Player wins!" % (player1.title(), verbs[player1+player2], player2.title()))
- pwt[0] += 1
- pwt[1] += 1
- if outcome == 2:
- print("%s %s %s. Computer wins!" % (player2.title(), verbs[player2+player1], player1.title()))
- pwt[0] += 1
- def end_game():
- plays = pwt[0]
- wins = pwt[1]
- ties = pwt[2]
- losses = plays - (wins + ties)
- print("Plays: %i" % plays)
- print("Wins: %i (%i%%)" % (wins, int(wins*100/plays)))
- print("Losses: %i (%i%%)" % (losses, int(losses*100/plays)))
- print("Ties: %i (%i%%)" % (ties, int(ties*100/plays)))
- exit()
- menu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement