Author | izzy |
Submission date | 2017-12-07 22:09:07.461710 |
Rating | 6092 |
Matches played | 308 |
Win rate | 63.64 |
Use rpsrunner.py to play unranked matches on your computer.
import random
# comment this out when submitting
#moves = "PPPSSSSRR"
#input = "P"
if input=="":
moves=""
def count_substring(str, sub):
if len(str)==0:
return 0
if str[0:len(sub)]==(sub):
return 1 + count_substring(str[1:], sub)
else:
return 0+count_substring(str[1:], sub)
def predictPatterns():
rockCount=0
paperCount=0
scissorsCount=0
last4=moves[-4:]
last3=moves[-3:]
last2=moves[-2:]
# weight longer patterns more than shorter patterns, we did this by multiplyng by 5
rockCount=count_substring(moves, last4+"R") * 5 + count_substring(moves, last3+"R") * 3 + count_substring(moves, last2+"R")
scissorsCount=count_substring(moves, last4+"S") * 5 + count_substring(moves, last3+"S") * 3 + count_substring(moves, last2+"S")
paperCount=count_substring(moves, last4+"P") * 5 + count_substring(moves, last3+"P") * 3 + count_substring(moves, last2+"P")
# find the most likely outcome in order to counter it
if rockCount > scissorsCount and rockCount > paperCount:
output = "P"
elif scissorsCount > rockCount and scissorsCount > paperCount:
output = "R"
elif paperCount > rockCount and paperCount > scissorsCount:
output = "S"
else:
output = random.choice(["R", "S", "P"])
return output
moves=moves+input
# Pick random moves until we've seen enough to make predictions
if len(moves) > 500:
output = random.choice(["R", "S", "P"])
else:
output = predictPatterns()
# comment this when submitting
#print(output)