Author | __NR__ |
Submission date | 2018-05-19 17:51:50.989800 |
Rating | 5971 |
Matches played | 280 |
Win rate | 56.79 |
Use rpsrunner.py to play unranked matches on your computer.
#!/usr/bin/env python
import sys
import time
import random
import math
'''
Author: __NR__
Concept: history match then frequency counting with non-seeded randomness
[Reference] EbTech's "Boltzmann Counter," a variation on frequency counting, is
used when history fails. See http://www.rpscontest.com/entry/13015
[Reference] dllu, https://daniel.lawrence.lu/programming/rps/
'''
random_dict = {1:"R", 2:"P", 3:"S"}
random_pick = random_dict[random.randint(1,3)]
beat_list = {"R":"P", "P":"S", "S":"R"}
def second_defense(rockCount, paperCount, scissorsCount, random_pick, another_random):
'''
modified frequency counting
[Reference] EbTech's "Boltzmann Counter"
'''
if rockCount == paperCount == scissorsCount:
return random_pick
else:
# print rockCount, paperCount, scissorsCount
if another_random < math.exp(rockCount):
newVariable = "R"
elif another_random < math.exp(rockCount) + math.exp(paperCount):
newVariable = "P"
else:
newVariable = "S"
return newVariable
if input == "":
history = ''
masterCount = 0
newVariable = random_pick
rockCount = paperCount = scissorsCount = 0
# start predicting next value after 100 rounds (opponents vary often)
elif masterCount < 100:
history += input
newVariable = random_pick
masterCount += 1
rockCount *= 0.95
scissorsCount *= 0.95
paperCount *= 0.95
if input == "R":
paperCount += 0.1
scissorsCount -= 0.1
if input == "P":
scissorsCount += 0.1
rockCount -= 0.1
if input == "S":
rockCount += 0.1
paperCount -= 0.1
else:
# tick counters
rockCount *= 0.95
scissorsCount *= 0.95
paperCount *= 0.95
if input == "R":
paperCount += 0.1
scissorsCount -= 0.1
if input == "P":
scissorsCount += 0.1
rockCount -= 0.1
if input == "S":
rockCount += 0.1
paperCount -= 0.1
another_random = random.random()*(math.exp(rockCount) + \
math.exp(scissorsCount)+math.exp(paperCount))
# print "new variable: ", input
# print "history length", len(history)
# original match is the full length of all known history plus new value
match = history + input
for idx, val in enumerate(history):
# the match here is pared down until it is small enough to pattern-match
match = match[idx:]
# print "match attempted", match
# print "history full", history
# if the pattern exists in the history, find the next value
if match in history:
# simple way of grabbing next value
try:
befor_keyowrd, keyword, after_keyword = history.partition(match)
newVariable = beat_list[after_keyword[1]]
# print "we have a match, and it worked!"
except:
# print "we have a match, but it failed"
newVariable = second_defense(rockCount, paperCount, \
scissorsCount, random_pick, another_random)
break
# if the pattern does not exist in history, keep winding down match
else:
# if the match is down to a single value, just use a random variable
if len(match) == random.randint(2, 10):
# print "down to random"
newVariable = second_defense(rockCount, paperCount, \
scissorsCount, random_pick, another_random)
break
history += input
# print "________________"
output = newVariable
# time.sleep(.5)