Author | Eppie |
Submission date | 2020-02-02 02:49:18.659756 |
Rating | 7223 |
Matches played | 203 |
Win rate | 69.46 |
Use rpsrunner.py to play unranked matches on your computer.
import random
from collections import defaultdict
if not input:
history = []
moves = ['R', 'P', 'S']
counters = {'R': 'P', 'P': 'S', 'S': 'R'}
max_context = 40
table = defaultdict(lambda: {'R': 0, 'P': 0, 'S': 0})
output = random.choice(moves)
else:
max_context_len = min(len(history), max_context)
for context_len in range(max_context_len, 0, -1):
current_context = tuple(history[-context_len:])
table[current_context][input] += 1
history.append(input)
max_context_len = min(len(history), max_context)
for context_len in range(max_context_len, 0, -1):
current_context = tuple(history[-context_len:])
counts = table[current_context]
if counts['R'] != 0 or counts['P'] != 0 or counts['S'] != 0:
break
v, k = max((v, k) for k, v in counts.items())
output = counters[k]