Markov5

Authorasd101
Submission date2019-04-18 02:51:15.754089
Rating5281
Matches played256
Win rate50.78

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

Source code:

import random
import operator
import copy

choices = ["R","P","S"]
win = {"R":"P","P":"S","S":"R"}
if "history" not in globals():
    history = ""

history += input

transition_matrix = {"RS":0,"RP":0,"RR":0,"PR":0,"PP":0,"PS":0,"SR":0,"SP":0,"SS":0}


def getSubset(startKey):
    rtn = {}
    dictionary = copy.deepcopy(transition_matrix)
    for item, value in dictionary.items():
        if item[0] == startKey:
            rtn[item] = value
    return rtn


if len(history) < 2 :
    output = choices[random.randint(0,2)]
else:
    for (first, second) in zip(history, history[1:]):
        transition_matrix[first + second] += 1
    prediction = max((getSubset(history[-1])).iteritems(), key=operator.itemgetter(1))[0][1]
    output = win[prediction]