MarkovChain_PercentagePick

AuthorScienceMentorsTest
Submission date2019-07-25 06:18:05.932631
Rating5854
Matches played221
Win rate55.2

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

Source code:

import random
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():
    percentageTable = {"R": 0, "P": 0, "S": 0}

    editDict = transitionTable[moveHistory[-1]]

    pickList = []

    for k, v in editDict.items():
        if v == 0:
            return choices[random.randint(0, 2)]
        else:
            percentageTable[k] = int(Decimal((v / (playCount[moveHistory[-1]]- 1))*100))
            for i in range(percentageTable[k]):
                pickList.append(k)

    pick = pickList[random.randint(0, len(pickList) - 1)]

    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()