MarkovChain_MaxPick

AuthorScienceMentorsTest
Submission date2019-07-25 06:20:58.456093
Rating4679
Matches played220
Win rate44.55

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

Source code:

import random
import operator
from decimal import Decimal

transitionTable = {"R": {"S": 0, "P": 0, "R": 0}, "S": {"S": 0, "P": 0, "R": 0}, "P": {"S": 0, "P": 0, "R": 0}}

playCount = {"R": 0, "P": 0, "S": 0}

choices = ["R", "P", "S"]

moveHistory = []

def updater(moveHistory, transitionTable):
    playCount[moveHistory[-1]] += 1

    if len(moveHistory) > 1:
        slice1 = moveHistory[-2]
        slice2 = moveHistory[-1] 

        editDict = transitionTable[slice1]

        editDict[slice2] += 1

def picker():
    editDict = transitionTable[moveHistory[-1]]
    pick = ""
    largest = max(editDict.items(), key=operator.itemgetter(1))[0]

    if editDict[largest] == 0:
        return choices[random.randint(0,2)] 
    else:
        pick = largest

    if pick is "R":
        return "P"
    elif pick is "P":
        return "S"
    elif pick is "S":
        return "R"

if input is not "":
    moveHistory.append(input)
    updater(moveHistory, transitionTable)

if len(moveHistory) == 0:
    output = choices[random.randint(0, 2)]
else:
    output = picker()