Markov2

AuthorDavidO
Submission date2019-04-16 06:26:14.318704
Rating4926
Matches played250
Win rate50.8

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

Source code:

import random
import operator

choices = ["R","P","S"]
win = {"R":"P","P":"S","S":"R"}
transition_matrix = {"RS":0,"RP":0,"RR":0,"PR":0,"PP":0,"PS":0,"SR":0,"SP":0,"SS":0}


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


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