Author | Emmanuel Harish Menon |
Submission date | 2019-05-10 23:04:49.232761 |
Rating | 4824 |
Matches played | 238 |
Win rate | 46.22 |
Use rpsrunner.py to play unranked matches on your computer.
'''
Program Name: MarkovChainLearner v3 [SubmissionCode]
Program by: Emmanuel Harish Menon
Last Updated: 9:00 AM 11/5/19
Explanation:
This program uses Markov chains to respond to the probability that the user will pick rock, paper or scissors. It was made for the contest at rpscontest.com . This version is based on the maximum value in the transition table
'''
#import modules
import random
import operator
from decimal import Decimal
#transition table of nested dictionaries
transitionTable = {"R": {"S": 0, "P": 0, "R": 0}, "S": {"S": 0, "P": 0, "R": 0}, "P": {"S": 0, "P": 0, "R": 0}}
#stores number of plays
playCount = {"R": 0, "P": 0, "S": 0}
#used to determine a random pick
choices = ["R", "P", "S"]
#stores all move history
moveHistory = []
#this function updates values in transition table
def updater(moveHistory, transitionTable):
#increases the appropriate value in the play count dict
playCount[moveHistory[-1]] += 1
#this will run as long as move history is larger than 1
if len(moveHistory) > 1:
#stores last two plays into individual vars
slice1 = moveHistory[-2]
slice2 = moveHistory[-1]
#stores a portion of the nested dict into editDict
editDict = transitionTable[slice1]
#updates the relevant variable
editDict[slice2] += 1
def picker():
#stores a the relevant portion of the nested dict
editDict = transitionTable[moveHistory[-1]]
#empty var
pick = ""
#gets largest valued key in dict
largest = max(editDict.items(), key=operator.itemgetter(1))[0]
#if it is 0, return a random pick
if editDict[largest] == 0:
return choices[random.randint(0,2)]
#pick the largest value in the dictionary
else:
pick = largest
#returns a value based on the predicted user's pick
if pick is "R":
return "P"
elif pick is "P":
return "S"
elif pick is "S":
return "R"
#as long as input is not empty
if input is not "":
#add the prev pick to the list
moveHistory.append(input)
#update transition table
updater(moveHistory, transitionTable)
#pick randomly for the first game
if len(moveHistory) == 0:
output = choices[random.randint(0, 2)]
else:
output = picker()