IocainePowder

AuthorScienceMentorsTest
Submission date2019-07-25 06:24:30.549665
Rating5121
Matches played233
Win rate54.94

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

Source code:

import random
import operator
    
output = ""

playCounter = {"R": 0, "P": 0, "S": 0}
winTypeCounter = {"random": 0, "freq2": 0, "freq5": 0, "freq10": 0, "freq100": 0, "freq1000": 0, "history2": 0, "history5": 0, "history10": 0, "history100": 0, "history1000": 0}

metaStratList = ["naive", "secondGuess", "thirdGuess", "oppNaive", "oppSecondGuess", "oppThirdGuess"]

randomStats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}

freqStats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
freq2Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
freq5Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
freq10Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
freq100Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
freq1000Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}

history2Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
history5Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
history10Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
history100Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}
history1000Stats = {"naive": 0, "secondGuess": 0, "thirdGuess": 0, "oppNaive": 0, "oppSecondGuess": 0, "oppThirdGuess": 0}

winningMoves = {"R": "P", "P": "S", "S": "R"}
choices = ["R", "P", "S"]


oppMoveHistory = []
myMoveHistory = []

numPlays = 1

def returnDictMax(dictionary):
    return max(dictionary.items(), key=operator.itemgetter(1))[0]

def metaStrategy(oppPick, origin = ""):
    def naive():
        return winningMoves[oppPick]
    def secondGuess():
        return winningMoves[winningMoves[naive()]]
    def thirdGuess():
        return winningMoves[winningMoves[secondGuess()]]
    def oppNaive():
        return winningMoves[naive()]
    def oppSecondGuess():
        return winningMoves[secondGuess()]
    def oppThirdGuess():
        return winningMoves[thirdGuess()]

    def decisionMaker(dictMax):
        if dictMax == "naive":
            return naive()
        elif dictMax == "secondGuess":
            return secondGuess()
        elif dictMax == "thirdGuess":
            return thirdGuess()
        elif dictMax == "oppNaive":
            return oppNaive()
        elif dictMax == "oppSecondGuess":
            return oppSecondGuess()
        elif dictMax == "oppThirdGuess":
            return oppThirdGuess()

    def picker(dictMax):
        if dictMax != 0:
            pick = decisionMaker(dictMax)
        else:
            pick = choices[random.randint(0, 2)]
        return pick
      
    if origin == "random":
        dictMax = returnDictMax(randomStats)
        pick = picker(dictMax)
        return pick
     
    elif origin == "freq2":
        dictMax = returnDictMax(freq2Stats)
        pick = picker(dictMax)
        return pick
    
    elif origin == "freq5":
        dictMax = returnDictMax(freq5Stats)
        pick = picker(dictMax)
        return pick
       
    elif origin == "freq10":
        dictMax = returnDictMax(freq10Stats)
        pick = picker(dictMax)
        return pick
     
    elif origin == "freq100":
        dictMax = returnDictMax(freq100Stats)
        pick = picker(dictMax)
        return pick
      
    elif origin == "freq1000":
        dictMax = returnDictMax(freq1000Stats)
        pick = picker(dictMax)
        return pick
    
    elif origin == "history2":
        dictMax = returnDictMax(history2Stats)
        pick = picker(dictMax)
        return pick
        
    elif origin == "history5":
        dictMax = returnDictMax(history5Stats)
        pick = picker(dictMax)
        return pick
   
    elif origin == "history10":
        dictMax = returnDictMax(history10Stats)
        pick = picker(dictMax)
        return pick
     
    elif origin == "history100":
        dictMax = returnDictMax(history100Stats)
        pick = picker(dictMax)
        return pick
  
    elif origin == "history1000":
        dictMax = returnDictMax(history1000Stats)
        pick = picker(dictMax)
        return pick

    elif origin == "test":
        return [winChecker(naive(), oppMoveHistory[-1]), winChecker(secondGuess(), oppMoveHistory[-1]), winChecker(thirdGuess(), oppMoveHistory[-1]), winChecker(oppNaive(), oppMoveHistory[-1]), winChecker(oppSecondGuess(), oppMoveHistory[-1]), winChecker(oppThirdGuess(), oppMoveHistory[-1])]

def randomGuesser(returnVal = "winning move"):
    oppPick = choices[random.randint(0,2)]
    if returnVal == "winning move":
        return metaStrategy(oppPick, "random")
    elif returnVal == "predicted move":
        return oppPick

def freqAnalysis(searchRange, returnVal = "winning move"):
    dictPlaysInRange = {"R": 0, "P": 0, "S": 0}

    tempList = oppMoveHistory[-searchRange:]

    for pick in tempList:
        dictPlaysInRange[pick] += 1

    oppPick = returnDictMax(dictPlaysInRange)
    
    if returnVal == "winning move":
        return metaStrategy(oppPick, "freq"+str(searchRange))
    elif returnVal == "predicted move":
        return oppPick

def historyMatcher(checkSequence, searchRange = len(oppMoveHistory), returnVal = "winning move"):
    checkList = oppMoveHistory[-checkSequence:]
    searchList = oppMoveHistory[-searchRange:-checkSequence]
    arrLength = len(searchList)
    oppPick = ""

    while (arrLength >= checkSequence):
        check = searchList[-checkSequence-1:-1]
        if check == checkList:
            oppPick = searchList[-1]
            break
        searchList.pop()
        arrLength = len(searchList)
    
    
    if oppPick == "":
        oppPick = choices[random.randint(0, 2)]
    
    if returnVal == "winning move":
        return metaStrategy(oppPick, "history"+str(checkSequence))
    elif returnVal == "predicted move":
        return oppPick

def metaStratUpdater(winList, dictionaryName):
    for index, item in enumerate(winList):
        if item == "W":
            dictionaryName[metaStratList[index]] += 1
        elif item == "L":
                dictionaryName[metaStratList[index]] -= 1

def winChecker(output, _input):
    if output == _input:
        return "D"
    elif winningMoves[_input] == output:
        return "W"
    elif winningMoves[output] == _input:
        return "L"

stratFunctionList = [randomGuesser, freqAnalysis, historyMatcher]

if input is not "":
    oppMoveHistory.append(input)

    lenOfList = len(oppMoveHistory)

    for function in stratFunctionList:
        if function is randomGuesser:
            move = function()
            winList = metaStrategy(function("predicted move"), origin = "test")
            moveResult = winChecker(move, input)
            if moveResult == "W":
                winTypeCounter["random"] += 1
                metaStratUpdater(winList, randomStats)
            elif moveResult == "L":
                winTypeCounter["random"] -= 1
                metaStratUpdater(winList, randomStats)

        elif function is freqAnalysis:
            if lenOfList > 2:
                move = function(2)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(2, returnVal = "predicted move"), origin = "test")
                if moveResult == "W":
                    winTypeCounter["freq2"] += 1
                elif moveResult == "L":
                    winTypeCounter["freq2"] -= 1
                    
                metaStratUpdater(winList, freq2Stats)
                
            if lenOfList > 5:
                move = function(5)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(5, returnVal = "predicted move"), origin = "test")
                
                if moveResult == "W":
                    winTypeCounter["freq5"] += 1
                elif moveResult == "L":
                    winTypeCounter["freq5"] -= 1
                    
                metaStratUpdater(winList, freq5Stats)
            
            if lenOfList > 10:
                move = function(10)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(10, returnVal = "predicted move"), origin = "test")

                if moveResult == "W":
                    winTypeCounter["freq10"] += 1
                elif moveResult == "L":
                    winTypeCounter["freq10"] -= 1
                    
                metaStratUpdater(winList, freq10Stats)

            if lenOfList > 100:
                move = function(100)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(100, returnVal = "predicted move"), origin = "test")
                
                if moveResult == "W":
                    winTypeCounter["freq100"] += 1
                elif moveResult == "L":
                    winTypeCounter["freq100"] -= 1
                    
                metaStratUpdater(winList, freq100Stats)

            if lenOfList > 1000:
                move = function(1000)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(1000, returnVal = "predicted move"), origin = "test")

                if moveResult == "W":
                    winTypeCounter["freq1000"] += 1
                elif moveResult == "L":
                    winTypeCounter["freq1000"] -= 1
                
                metaStratUpdater(winList, freq1000Stats)
                
        elif function is historyMatcher:
            if lenOfList > 4:
                move = function(2)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(2, lenOfList, returnVal = "predicted move"), origin = "test")
                if moveResult == "W":
                    winTypeCounter["history2"] += 1
                elif moveResult == "L":
                    winTypeCounter["history2"] -= 1
                    
                metaStratUpdater(winList, history2Stats)
                
            if lenOfList > 10:
                move = function(5)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(5, lenOfList, returnVal = "predicted move"), origin = "test")
                
                if moveResult == "W":
                    winTypeCounter["history5"] += 1
                elif moveResult == "L":
                    winTypeCounter["history5"] -= 1
                    
                metaStratUpdater(winList, history5Stats)
            
            if lenOfList > 20:
                move = function(10)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(10, lenOfList, returnVal = "predicted move"), origin = "test")

                if moveResult == "W":
                    winTypeCounter["history10"] += 1
                elif moveResult == "L":
                    winTypeCounter["history10"] -= 1
                    
                metaStratUpdater(winList, history10Stats)

            if lenOfList > 200:
                move = function(100)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(100, lenOfList, returnVal = "predicted move"), origin = "test")
                
                if moveResult == "W":
                    winTypeCounter["history100"] += 1
                elif moveResult == "L":
                    winTypeCounter["history100"] -= 1
                    
                metaStratUpdater(winList, history100Stats)

            if lenOfList > 2000:
                move = function(1000)
                moveResult = winChecker(move, input)
                winList = metaStrategy(function(1000, lenOfList, returnVal = "predicted move"), origin = "test")

                if moveResult == "W":
                    winTypeCounter["history1000"] += 1
                elif moveResult == "L":
                    winTypeCounter["history1000"] -= 1
                
                metaStratUpdater(winList, history1000Stats)

    playCounter[input] += 1

if len(oppMoveHistory) == 0:
    output = choices[random.randint(0, 2)]
else:
    dictMax = returnDictMax(winTypeCounter)
    
    if winTypeCounter[dictMax] <= 0 or dictMax == "random":
        output = randomGuesser()
    elif dictMax == "freq2":
        output = freqAnalysis(2)
    elif dictMax == "freq5":
        output = freqAnalysis(5)
    elif dictMax == "freq10":
        output = freqAnalysis(10)
    elif dictMax == "freq100":
        output = freqAnalysis(100)
    elif dictMax == "freq1000":
        output = freqAnalysis(1000)
    elif dictMax == "history2":
        output = historyMatcher(2)
    elif dictMax == "history5":
        output = historyMatcher(5)
    elif dictMax == "history10":
        output = historyMatcher(10)
    elif dictMax == "history100":
        output = historyMatcher(100)
    elif dictMax == "history1000":
        output = historyMatcher(1000)
 
myMoveHistory.append(output)

numPlays += 1