BUTT DESTROYER V2

This program has been disqualified.


AuthorJFreegman
Submission date2012-07-27 22:37:25.891673
Rating6484
Matches played40
Win rate67.5

Source code:

# Author: JFreegman
# Contact: JFreegman@gmail.com
# Date: July 27, 2012
# v2.0

# 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():
    freqs = tot_move_freq
    probs = {}
    probs['R'] = float(freqs ['R']) / freqs ['total']
    probs['S'] = float(freqs ['S']) / freqs ['total']
    probs['P'] = float(freqs ['P']) / freqs ['total']
    return probs

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'}
    tot_move_freq = {'R': 0, 'P': 0, 'S': 0, 'total': 0}
    opp_moves = ""
    strat_success = {'hist': 0, 'random': 0, 'freqtot': 0, 'my_hist': 0, 'my_freq': 0,}
    output = random_weapon()
    my_moves = output
else:
    # update strategy success rates based on last round results
    opp_moves += input
    last_opp_move = input
    tot_move_freq[last_opp_move] += 1
    tot_move_freq['total'] += 1
    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

    # opponent's most probable move based on frequency and historic patterns
    opp_freqtot = get_probs()
    opp_prob_f_tot = max(opp_freqtot, key=opp_freqtot.get)
    opp_prob_h = get_history_match(opp_moves)

    # my most probable move based on history pattern matches
    my_prob_h = get_history_match(my_moves, 150)

    # naive moves for each strategy
    my_move_freqtot = winning_move[opp_prob_f_tot]
    my_move_hist = winning_move[opp_prob_h]
    my_move_my_hist = losing_move[my_prob_h]
    random_move = random_weapon()

    # dict of all strategies and their move
    strats = {'hist': my_move_hist, 'random': random_move,
              'freqtot': my_move_freqtot,'my_hist': 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