This program has been disqualified.
Author | JFreegman |
Submission date | 2012-07-28 00:06:59.639698 |
Rating | 5000 |
Matches played | 0 |
Win rate | 0 |
# Author: JFreegman
# Contact: JFreegman@gmail.com
# Date: July 27, 2012
# v2.1
# All code is written from scratch. The general idea is based off Iocaine Powder
# by Dan Egnor (http://ofb.net/~egnor/iocaine.html).
import random
def get_history_match(hist, n=200):
start = len(hist) - min(len(hist) / 2, n)
end = len(hist)
for i in xrange(start, end):
partition = hist[i:end]
match = hist[:-1].find(partition)
if match != -1:
return hist[match+len(partition)]
return random_weapon()
def get_probs(total_moves, n):
last = get_move_freq(total_moves[-n:])
probs = {}
probs['R'] = float(last['R']) / last['total']
probs['S'] = float(last['S']) / last['total']
probs['P'] = float(last['P']) / last['total']
return probs
def get_move_freq(moves):
mov_freq = {'R': 0, 'P': 0, 'S': 0}
count = 0
for move in moves:
if move == 'R':
mov_freq['R'] += 1
elif move == 'P':
mov_freq['P'] += 1
elif move == 'S':
mov_freq['S'] += 1
else:
raise ValueError, 'Invalid move'
count += 1
mov_freq['total'] = count
return mov_freq
def random_weapon():
return random.choice(['R', 'P', 'S'])
if not input:
last_strats = {}
res_history = []
winning_move = {'R': 'P', 'P': 'S', 'S': 'R'}
losing_move = {'R': 'S', 'P': 'R', 'S': 'P'}
opp_moves = ""
strat_success = {'freq20': 0,'hist': 0, 'random': 0, 'freq100': 0,
'freqtot': 0, 'freq5': 0, 'hist5': 0,'hist20': 0,
'my_hist': 0, 'c_my_move_my_hist': 0, 'c1_my_move_my_hist': 0,}
output = random_weapon()
my_moves = output
else:
# update strategy success rates based on last round results
opp_moves += input
last_opp_move = input
beat_opp = winning_move[last_opp_move]
lose_opp = losing_move[last_opp_move]
for s in last_strats:
if last_strats[s] == beat_opp:
strat_success[s] += 1
elif last_strats[s] == lose_opp:
strat_success[s] -= 1
# get opponent's most probable move based on frequency and history
# pattern matches
opp_freq5 = get_probs(opp_moves, 5)
opp_prob_f_5 = max(opp_freq5, key=opp_freq5.get)
opp_freq20 = get_probs(opp_moves, 20)
opp_prob_f_20 = max(opp_freq20, key=opp_freq20.get)
opp_freq100 = get_probs(opp_moves, 100)
opp_prob_f_100 = max(opp_freq100, key=opp_freq100.get)
opp_freqtot = get_probs(opp_moves, len(opp_moves))
opp_prob_f_tot = max(opp_freqtot, key=opp_freqtot.get)
opp_prob_h = get_history_match(opp_moves)
opp_prob_h20 = get_history_match(opp_moves, 20)
opp_prob_h5 = get_history_match(opp_moves, 5)
# get my most probable move based on history
my_prob_h = get_history_match(my_moves, 150)
# naive moves for each strategy
my_move_freq5 = winning_move[opp_prob_f_5]
my_move_freq20 = winning_move[opp_prob_f_20]
my_move_freq100 = winning_move[opp_prob_f_100]
my_move_freqtot = winning_move[opp_prob_f_tot]
my_move_hist = winning_move[opp_prob_h]
my_move_hist20 = winning_move[opp_prob_h20]
my_move_hist5 = winning_move[opp_prob_h5]
my_move_my_hist = losing_move[my_prob_h]
c_my_move_my_hist = losing_move[my_move_my_hist]
c1_my_move_my_hist = losing_move[c_my_move_my_hist]
random_move = random_weapon()
# dict of all available strategies and their move
strats = {'freq20': my_move_freq20, 'freq100': my_move_freq100,
'hist': my_move_hist, 'random': random_move,
'freqtot': my_move_freqtot,'freq5': my_move_freq5,
'hist5': my_move_hist5, 'hist20': my_move_hist20,
'my_hist': my_move_my_hist, 'c_my_move_my_hist': c_my_move_my_hist,
'c1_my_move_my_hist': c1_my_move_my_hist }
# Pick the strategy with the highest current success rate
strat = max(strats, key=lambda x: strat_success[x])
output = strats[strat]
my_moves += output
last_strats = strats