Skip to content

Instantly share code, notes, and snippets.

@TylerLeite
Last active December 24, 2015 06:09
Show Gist options
  • Save TylerLeite/6755434 to your computer and use it in GitHub Desktop.
Save TylerLeite/6755434 to your computer and use it in GitHub Desktop.
Rock paper scissors with simple AI
from random import *
def one_liner():
while raw_input('You %s. ' % choice('win lose draw'.split())).lower() not in 'stop exit quit'.split(): party_time = True
def pro_strats():
wins = dict(zip('rock paper scissors'.split(), 'scissors rock paper'.split())) #what beats what
loses = dict(zip('rock paper scissors'.split(), 'paper scissors rock'.split())) #what loses to what
hist = list()
clever = False
win_streak, win_ct, loss_ct, draw_ct = 0, 0, 0, 0
while True:
# Input
player_move = raw_input('Rock paper or scissors? ').lower()
# Check input
if player_move not in 'rock paper scissors r p s 1 2 3'.split():
if player_move in 'stop exit quit'.split():
break
else:
print 'You must choose a valid move!'
# Do game logic
else:
# Account for shorthand moves
if player_move in 'r p s 1 2 3'.split():
if player_move == 'r' or player_move == '1': player_move = 'rock'
if player_move == 'p' or player_move == '2': player_move = 'paper'
if player_move == 's' or player_move == '3': player_move = 'scissors'
# Make the computer move
if not randint(0, 30):
computer_move = 'rock'
elif len(hist) < 4:
computer_move = choice('rock paper scissors'.split())
elif clever:
if (randint(0, 3)):
computer_move = loses[hist[-1]]
else:
computer_move = choice('rock paper scissors'.split())
elif hist[-2] == hist[-3] and hist[-4] == hist[-3] and loses[hist[-1]] == hist[-2]:
clever = True
computer_move = hist[-1]
elif hist[-1] == hist[-2] and hist[-1] == hist[-3]:
if randint(0, 3):
computer_move = loses[hist[-1]]
else:
computer_move = choice('rock paper scissors'.split())
elif hist[-1] == hist[-2] and hist[-1] != hist[-3]:
if randint(0, 2):
computer_move = wins[hist[-1]]
else:
computer_move = choice('rock paper scissors'.split())
elif win_streak > 3:
if randint(0, 1) == 0:
computer_move = choice('rock paper scissors'.split())
else:
computer_move = loses[hist[-1]]
elif win_streak == 2:
if randint(0, 2) == 0:
computer_move = hist[-1]
else:
computer_move = loses[hist[-1]]
else:
if randint(0, 4) == 0:
computer_move = wins[hist[-1]]
else:
computer_move = choice('rock paper scissors'.split())
# Update player's move history and check if they think they're clever
hist.append(player_move)
if clever and hist[-1] != hist[-2] and wins[hist[-1]] != hist[-2]:
clever = False
# Determine result
result = str()
if player_move == computer_move:
result = 'draw'
draw_ct += 1
elif wins[player_move] == computer_move:
result = 'win'
win_ct += 1
win_streak += 1
else:
result = 'lose'
win_streak = 0
loss_ct += 1
# Output
print 'You throw %s, computer throws %s, you %s!' % (player_move, computer_move, result)
print 'Wins: %i Losses: %i, Draws: %i' % (win_ct, loss_ct, draw_ct)
pro_strats()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment