# switching13

This program has been disqualified.

 Author pyfex Submission date 2011-06-17 19:40:22.987573 Rating 7565 Matches played 11 Win rate 81.82

## Source code:

``````# See http://overview.cc/RockPaperScissors for more information about rock, paper, scissors
# Add additional strategies to switching 12. The new strategies always make a double shift
# after a loose or a win.

import random

if input == "":
# combinations is not in Python 2.5, so I have to define it myself
def combinations(iterable, r):
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
hist = ""
opp_played = []
beat = {'P': 'S', 'S': 'R', 'R': 'P'}
beat2 = {'PP': 'S', 'SS': 'R', 'RR':'P', 'PS': 'S', 'PR': 'P', 'RS': 'R', 'RP': 'P', 'SP': 'S', 'SR': 'R'}

score = {'RR': 0, 'PP': 0, 'SS': 0, 'PR': 1, 'RS': 1, 'SP': 1,'RP': -1, 'SR': -1, 'PS': -1,}
output = random.choice(["R", "P", "S"])

def shift(n, move):
for i in range(n%2):
move = beat[move]
return move

def unshift(n, move):
for i in range(n%2):
move = beat[beat[move]]
return move

# 525 different strategies ftw ;)
candidates = [output] * 525
performance = [(0,0)] * 525
wins = losses = ties = 0

else:

sc = score[output+input]
if sc == 1:
wins += 1
elif sc == 0:
ties += 1
elif sc == -1:
losses += 1

hist += output.lower()+input
opp_played.append(input)

for i, c in enumerate(candidates):
performance[i] = ({1:performance[i][0]+1, 0: performance[i][0], -1: 0}[score[c+input]],
performance[i][1]+score[c+input])

index = performance.index(max(performance, key=lambda x: x[0]**3+x[1]))

for length in range(min(10, len(hist)-2), 0, -2):
search = hist[-length:]
idx = hist.rfind(search, 0, -2)
if idx != -1:
my = hist[idx+length].upper()
opp = hist[idx+length+1]
candidates[0] = beat[opp]
candidates[1] = beat[beat[my]]
candidates[2] = opp
candidates[3] = my
candidates[4] = beat[beat[opp]]
candidates[5] = beat[my]
for i, (a,b) in enumerate(combinations(candidates[:6],2)):
candidates[6+i] = beat2[a+b]
for i, a in enumerate(candidates[:21]):
for offset in range(3):
candidates[21+24*i+offset*8] = shift(offset+wins, a)
candidates[21+24*i+offset*8+1] = shift(offset+wins+ties, a)
candidates[21+24*i+offset*8+2] = shift(offset+losses+ties, a)
candidates[21+24*i+offset*8+3] = shift(offset+losses, a)
candidates[21+24*i+offset*8+4] = unshift(offset+wins, a)
candidates[21+24*i+offset*8+5] = unshift(offset+wins+ties, a)
candidates[21+24*i+offset*8+6] = unshift(offset+losses+ties, a)
candidates[21+24*i+offset*8+7] = unshift(offset+losses, a)
break
else:
candidates = [random.choice(['R', 'P', 'S'])] * 525

output = candidates[index]``````