tester990

Authornnanananan
Submission date2018-05-19 18:02:14.410160
Rating5412
Matches played288
Win rate56.6

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

Source code:

#!/usr/bin/env python
import sys
import time
import random
import math

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):
    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 5 rounds
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)