Freinet

AuthorVincent Osinga
Submission date2020-02-12 08:47:56.193075
Rating7312
Matches played200
Win rate72.5

Use rpsrunner.py to play unranked matches on your computer.

Source code:

from collections import defaultdict
import random
import operator

kept_turns = 2
kept_letters = kept_turns * 2

def random_choice():
  return random.choice(['R', 'P', 'S'])

def update_last_letters(last_letters, last_output, input, kept_letters):
  last_letters += last_output
  last_letters += input
  if len(last_letters) > kept_letters:
    last_letters = last_letters[-kept_letters:]
  return last_letters

def winner(input):
  if input == 'R':
    return 'P'
  elif input == 'P':
    return 'S'
  else:
    return 'R'

if input == '': # initialize variables for the first round
  history = defaultdict(lambda: '')
  output = random_choice()
  last_letters = ''
  last_output = ''

if len(last_letters) == kept_letters:
  history[last_letters] = winner(input)
  last_letters = update_last_letters(last_letters, last_output, input, kept_letters)
  output = history[last_letters]
  if not output:
    output = random_choice()
else:
  last_letters = update_last_letters(last_letters, last_output, input, kept_letters)
  output = random_choice()

last_output = output