paper cut

This program has been disqualified.


Authorben haley
Submission date2012-07-28 19:18:11.218573
Rating6857
Matches played185
Win rate65.41

Source code:

"""
Using compression to predict the opponents most likely next move.

The most likely move is assumed to be the one that with the least
complexity (least randomness) and therefore the one that has the
smallest compression size when added to the string representation
of previous moves.

bmh July 2012 <benjamin.haley@gmail.com>
"""

from zlib import compress
from random import shuffle, choice, random

moves = {
    'rock':'r',
    'paper':'p',
    'scissors':'s'
}

comebacks = {
    ('rock',): ('paper',),
    ('paper',): ('scissors',),
    ('scissors',): ('rock',),
    ('paper', 'rock'): ('paper',),
    ('paper', 'scissors'): ('scissors',),
    ('rock', 'scissors'): ('rock',),
    ('paper', 'rock', 'scissors'): ('paper', 'rock', 'scissors'),
}

def complexity(string):
    """ What is the compressed size of a string?
    (larger is more complex)"""
    return len(compress(string, 9))

def get_move_string(my_moves, opp_moves):
    """translate a list of moves into a compressible string"""
    return ''.join([moves[me] + moves[opp] for me, opp in zip(my_moves, opp_moves)])

def expected(my_moves, opp_moves):
    """The most likely next move of the opponent"""
    history = get_move_string(opp_moves, my_moves)
    exps = [(complexity(history + moves[m]), m) for m in moves.keys()]
    brilliance = 0.91 #complexity(history) / float(complexity(shuffle_(history)))
    patients = 0 if brilliance < 0.90 else \
               1 if brilliance < 0.96 else \
               2
    res = tuple(sorted([e[1] for e in exps if e[0] <= min(exps)[0] + patients]))
    return res

def player(my_moves, opp_moves):
    return choice(comebacks[expected(my_moves, opp_moves)])

def shuffle_(string):
    array = list(string)
    shuffle(array)
    return ''.join(array)

#"""
# to compare with existing code at
# http://www.rpscontest.com/submit
rmoves = dict((v.upper(), k) for k, v in moves.items())
if input == "":
    my_moves = []
    opp_moves = []
else:
    opp_moves += [rmoves[input]]

my_move = player(my_moves, opp_moves)
my_moves.append(my_move)
output = my_move[0].upper()