switching14

This program has been disqualified.


Authorpyfex
Submission date2011-06-17 19:53:22.128898
Rating7626
Matches played2249
Win rate76.43

Source code:

# See http://overview.cc/RockPaperScissors for more information about rock, paper, scissors
# Switching 13 got disqualified. 525 strategies were probably too much. Now try if 150 work

import random

if input == "":
  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

  # 150 different strategies
  candidates = [output] * 150
  performance = [(0,0)] * 150
  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 in enumerate(candidates[:6]):
        for offset in range(3):
          candidates[6+24*i+offset*8] = shift(offset+wins, a)
          candidates[6+24*i+offset*8+1] = shift(offset+wins+ties, a)
          candidates[6+24*i+offset*8+2] = shift(offset+losses+ties, a)
          candidates[6+24*i+offset*8+3] = shift(offset+losses, a)
          candidates[6+24*i+offset*8+4] = unshift(offset+wins, a)
          candidates[6+24*i+offset*8+5] = unshift(offset+wins+ties, a)
          candidates[6+24*i+offset*8+6] = unshift(offset+losses+ties, a)
          candidates[6+24*i+offset*8+7] = unshift(offset+losses, a)
      break
  else:
      candidates = [random.choice(['R', 'P', 'S'])] * 150
 
  output = candidates[index]