Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #
- # Rock, Paper, Scissors, Lizard, Spock
- #
- from random import randint
- ai_counterattack = { 'Rock': ('Spock', 'Paper'),
- 'Paper': ('Scissors', 'Lizard'),
- 'Scissors': ('Spock', 'Rock'),
- 'Lizard': ('Rock', 'Scissors'),
- 'Spock': ('Lizard', 'Paper') }
- rules_map = { ('Scissors', 'Paper'): 'Scissors cut paper.',
- ('Paper', 'Rock'): 'Paper covers rock.',
- ('Rock', 'Lizard'): 'Rock crushes lizard.',
- ('Lizard', 'Spock'): 'Lizard poisons spock.',
- ('Spock', 'Scissors'): 'Spock smashes scissors.',
- ('Scissors', 'Lizard'): 'Scissors decapitates lizard.',
- ('Lizard', 'Paper'): 'Lizard eats paper.',
- ('Paper', 'Spock'): 'Paper disproves spock.',
- ('Spock', 'Rock'): 'Spock vaporizes rock.',
- ('Rock', 'Scissors'): 'Rock crushes scissors.' }
- def result(mode, phand, chand):
- """ Return a result tuple (result, h, c, t) where the last three can be 0 or 1
- according to who won round """
- if phand == chand:
- return ("It's a Tie!", 0, 0, 1)
- res = rules_map.get((phand,chand))
- if res == None:
- res = rules_map.get((chand,phand))
- if mode == 'cc':
- return (res + ' Computer 2 wins!', 0, 1, 0)
- else:
- return (res + ' Computer wins!', 0, 1, 0)
- if mode == 'cc':
- return (res + ' Computer 1 wins!', 1, 0, 0)
- else:
- return (res + ' Player wins!', 1, 0, 0)
- def smart_ai(phand, history):
- """ Sort the history of human moves, extract those with the same count,
- and choose a counterattack trying to avoid a tie considering the actual player hand """
- sorted_his = sorted(history, key=history.get)
- top_his = [hand for hand in sorted_his if history[hand] == history[sorted_his[-1]]]
- if len(top_his) == 1:
- ai_ca = set(ai_counterattack[top_his[0]]) - set([phand]) # avoid ties
- return ai_ca.pop()
- else:
- ai_ca = set()
- for t in top_his:
- ai_ca |= set(ai_counterattack[t])
- ai_ca -= set([phand]) # avoid ties
- r = randint(0, len(ai_ca)-1)
- while(r > 0):
- ai_ca.pop()
- r-=1
- return ai_ca.pop()
- def dumb_ai(*args, **kwargs):
- ai_ca = set(ai_counterattack)
- r = randint(0,3)
- while(r >= 0):
- ai_ca.pop()
- r-=1
- return ai_ca.pop()
- def exit_stats(mode, games, hwins, cwins, ties):
- if games == 0:
- print("\nNo games played!")
- return
- if mode == 'cc':
- print("\nTotal games played: %d\n"
- "Computer 1 wins: %d %.2f%%\n"
- "Computer 2 wins: %d %.2f%%\n"
- "Ties %d %.2f%%" % (games, cwins, 100*cwins/games, hwins, 100*hwins/games, ties, 100*ties/games))
- else:
- print("\nTotal games played: %d\n"
- "Computer wins: %d %.2f%%\n"
- "Human wins: %d %.2f%%\n"
- "Ties %d %.2f%%" % (games, cwins, 100*cwins/games, hwins, 100*hwins/games, ties, 100*ties/games))
- def main():
- hwins, cwins, ties, total = (0,0,0,0)
- history = { 'Rock': 0, 'Paper':0, 'Scissors': 0, 'Lizard': 0, 'Spock': 0}
- phand = None
- print("----------------------------------\n"
- "[ROCK-PAPER-SCISSORS-LIZARD-SPOCK]\n"
- "----------------------------------\n")
- purerandom = input("Do you want AI to be pure random? (y/N): ").lower()
- if purerandom == 'y':
- ai_comp_pick = dumb_ai
- else:
- ai_comp_pick = smart_ai
- gamemode = input("Choose between human x computer or computer x computer mode (hc/cc): ")
- if gamemode == "cc":
- c1wins = c2wins = 0
- c1hand = c2hand = None
- total = int(input("Enter the number of rounds: "))
- c1history = c2history = { 'Rock': 0, 'Paper':0, 'Scissors': 0, 'Lizard': 0, 'Spock': 0}
- for c in range(total):
- c1hand = ai_comp_pick(c2hand, c2history)
- print("Computer 1 picks: %s" % c1hand)
- c2hand = ai_comp_pick(c1hand, c1history)
- print("Computer 2 picks: %s" % c2hand)
- c1history[c1hand]+=1
- c2history[c2hand]+=1
- res = result(gamemode, c1hand, c2hand)
- print(res[0])
- c1wins+=res[1]
- c2wins+=res[2]
- ties+=res[3]
- exit_stats(gamemode, total, c1wins, c2wins, ties)
- return
- print("\nPick Rock, Paper, Scissors, Lizard or Spock!\n"
- "Press q to quit the game\n")
- try:
- while(True):
- phand = input('Player Picks: ').title()
- if phand == 'Q':
- break
- chand = ai_comp_pick(phand, history)
- print("Computer Picks: %s" % chand)
- # to simulate simultaneity of hands the computer only remembers after the game was played
- # otherwise, it would be like if he could se the human hand before showing his own
- history[phand]+=1
- res = result(gamemode, phand, chand)
- print(res[0])
- total+=1
- hwins+=res[1]
- cwins+=res[2]
- ties+=res[3]
- except (KeyboardInterrupt, SystemExit):
- print("\n")
- exit_stats(gamemode, total, hwins, cwins, ties)
- else:
- exit_stats(gamemode, total, hwins, cwins, ties)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement