switching18

This program has been disqualified.


Authorpyfex
Submission date2011-06-21 10:08:42.402251
Rating7994
Matches played2125
Win rate77.41

Source code:

# See http://overview.cc/RockPaperScissors for more information about rock, paper, scissors
# Switching with multiple different switching strategies

import random
import operator

if input == "":
  hist = ""
  my_stats = {'R':0, 'P':0, 'S': 0}
  beat = {'P': 'S', 'S': 'R', 'R': 'P'}
  cede = {'P': 'R', 'S': 'P', 'R': 'S'}

  def shift(n, move):
    return {0: move, 1:beat[move], 2:cede[move]}[n%2]
    
  def unshift(n, move):
    return {0: move, 1:cede[move], 2:beat[move]}[n%2]
    
  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"])

  candidates = [output] * 150
  # 1. Switch when lose or draw
  # 2. switch when lose
  # 3. Use best overall strategy
  # 4. Use switching 16
  # 5. Use rfind
  # 6. Always play cede to my most played move (counter rndbeat)
  
  performance = [[(2,0)]*6, [(2,0)]*6, [0]*6, [(2,0)]*150, 0, 0]
  main_performance = [0, 0, 0, 0, 0, 0]
  main_candidates = [output] * 6
  indices = [0, 0, 0, 0] # only the first 4 main strats are switching strats
  main_index = 0
  wins = losses = ties = 0
else:
  my_stats[output] += 1

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

  hist += output.lower()+input

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

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

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

  for i, c in enumerate(main_candidates):
    main_performance[i] += score[c+input]

  main_index = main_performance.index(max(main_performance))

  for length in range(min(14, 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] = cede[my]
      candidates[2] = opp
      candidates[3] = my
      candidates[4] = cede[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)
      main_candidates[4] = beat[opp]
      break
  else:
      output = random.choice(['R', 'P', 'S'])
      candidates = [output] * 150

  for i in range(4):
    main_candidates[i] = candidates[indices[i]]
  
  main_candidates[5] = cede[max([e for e in my_stats.items()], key=operator.itemgetter(1))[0]]
  output = main_candidates[main_index]