This program has been disqualified.
Author | ben haley |
Submission date | 2012-07-29 15:24:12.703805 |
Rating | 5312 |
Matches played | 188 |
Win rate | 55.32 |
"""
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 = ['R', 'P', 'S']
comebacks = {
('R',): ('P',),
('P',): ('S',),
('S',): ('R',),
('P', 'R'): ('P',),
('P', 'S'): ('S',),
('R', 'S'): ('R',),
('P', 'R', 'S'): ('P', 'R', 'S'),
}
def complexity(string):
""" What is the compressed size of a string?
(larger is more complex)"""
return len(compress(string, 9))
def expected(history):
"""The most likely next move of the opponent"""
history = history[-500:]
exps = [(complexity(history + m), m) for m in moves]
brilliance = 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(history):
return choice(comebacks[expected(history)])
def shuffle_(string):
array = list(string)
shuffle(array)
return ''.join(array)
#"""
# to compare with existing code at
# http://www.rpscontest.com/submit
if input == "":
history = ''
else:
history += input
output = player(history)
history += output