rpsbot3

This program has been disqualified.


Authorffenliv
Submission date2019-01-23 17:00:01.952760
Rating2631
Matches played60
Win rate23.33

Source code:

import random
from random import randint
import re
import math

MOVES = ["R","P","S"]
BEATS = {"R":"P", "P":"S", "S":"R"}
result = {"R":{"R":0, "P":-1, "S":1}, "P":{"R":1, "P":0, "S":-1}, "S":{"R":-1, "P":1, "S":0}}
SINGLE_WEIGHT = 3
MY_SINGLE_WEIGHT = 3
DOUBLE_WEIGHT = 3
MY_DOUBLE_WEIGHT = 2
TRIPLE_WEIGHT = 4

def their_single_history(THEIR_HISTORY, input, SINGLE_THROW):
    THEIR_LAST_THROW = input # pointless variable, just because I didn't want to find-replace all
    for i, val in enumerate(THEIR_HISTORY, start=1):
        if i < (len(THEIR_HISTORY) - 1):
            if val == THEIR_LAST_THROW:
                if THEIR_HISTORY[i+1] == 'R':
                    SINGLE_THROW[0] += 1
                elif THEIR_HISTORY[i+1] == 'P':
                    SINGLE_THROW[1] += 1
                elif THEIR_HISTORY[i+1] == 'S':
                    SINGLE_THROW[2] += 1
    TOTAL_SINGLE = sum(SINGLE_THROW) # sum of all throws, just in case it somehow differs from the round number, because I suck at this
    SINGLE_ROLLS = [0,0,0]

    for count, i in enumerate(SINGLE_THROW): # generate a random roll between 0 and the number of times a result was thrown, gives chance for lesser-thrown result to be accounted for
        SINGLE_ROLLS[count] = randint(0, (i*100)/TOTAL_SINGLE)

    SINGLE_ROLL_MAX = max(SINGLE_ROLLS) # gets the max roll
    MAX_ROLL_INDEX = SINGLE_ROLLS.index(SINGLE_ROLL_MAX) # gets the index of the max roll
    SINGLE_GUESS = MOVES[MAX_ROLL_INDEX] # get the associated move from the guess
    SINGLE_RESPONSE = BEATS[SINGLE_GUESS] # get the move that beats the guess

    return SINGLE_RESPONSE


def their_double_history(THEIR_HISTORY, inupt, DOUBLE_THROW):
    THEIR_LAST_THROW = input
    THEIR_LAST_2_THROWS = THEIR_HISTORY[-2]
    THEIR_HISTORY_JOIN = ''.join(THEIR_HISTORY)

    for match in re.finditer(THEIR_HISTORY_JOIN[-2:], THEIR_HISTORY_JOIN):
        if match.end() < len(THEIR_HISTORY_JOIN):
            common_response = THEIR_HISTORY_JOIN[match.end()]
            if common_response == 'R':
                DOUBLE_THROW[0] += 1
            elif common_response == 'P':
                DOUBLE_THROW[1] += 1
            elif common_response == 'S':
                DOUBLE_THROW[2] += 1

    TOTAL_DOUBLE = sum(DOUBLE_THROW) # sum of all throws, just in case it somehow differs from the round number, because I suck at this
    DOUBLE_ROLLS = [0,0,0]

    for count, i in enumerate(DOUBLE_THROW): # generate a random roll between 0 and the number of times a result was thrown, gives chance for lesser-thrown result to be accounted for
        if TOTAL_DOUBLE == 0:
            x = 2
        else:
            DOUBLE_ROLLS[count] = randint(0, (i*100)/TOTAL_DOUBLE)

    DOUBLE_ROLL_MAX = max(DOUBLE_ROLLS) # gets the max roll
    MAX_ROLL_INDEX = DOUBLE_ROLLS.index(DOUBLE_ROLL_MAX) # gets the index of the max roll
    DOUBLE_GUESS = MOVES[MAX_ROLL_INDEX] # get the associated move from the guess
    DOUBLE_RESPONSE = BEATS[DOUBLE_GUESS] # get the move that beats the guess

    return DOUBLE_RESPONSE

def my_single_history(MY_HISTORY, MY_SINGLE_THROW):
    MY_LAST_THROW = MY_HISTORY[-1]
    for i, val in enumerate(MY_HISTORY, start=1):
        if i < (len(MY_HISTORY)-1):
            if MY_HISTORY[i+1] == 'R':
                MY_SINGLE_THROW[0] += 1
            elif MY_HISTORY[i+1] == 'P':
                MY_SINGLE_THROW[1] += 1
            elif MY_HISTORY[i+1] == 'S':
                MY_SINGLE_THROW[2] += 1

    MY_TOTAL_SINGLE = sum(MY_SINGLE_THROW) # sum of all throws, just in case it somehow differs from the round number, because I suck at this
    MY_SINGLE_ROLLS = [0,0,0]

    for count, i in enumerate(MY_SINGLE_THROW): # generate a random roll between 0 and the number of times a result was thrown, gives chance for lesser-thrown result to be accounted for
        MY_SINGLE_ROLLS[count] = randint(0, (i*100)/MY_TOTAL_SINGLE)

    MY_SINGLE_ROLL_MAX = max(MY_SINGLE_ROLLS) # gets the max roll
    MAX_ROLL_INDEX = MY_SINGLE_ROLLS.index(MY_SINGLE_ROLL_MAX) # gets the index of the max roll
    MY_SINGLE_GUESS = MOVES[MAX_ROLL_INDEX] # get the associated move from the guess
    MY_SINGLE_RESPONSE = BEATS[MY_SINGLE_GUESS] # get the move that beats the guess

    return MY_SINGLE_RESPONSE

def my_double_history(MY_HISTORY, inupt, MY_DOUBLE_THROW):
    MY_LAST_THROW = input
    MY_LAST_2_THROWS = MY_HISTORY[-2]
    MY_HISTORY_JOIN = ''.join(MY_HISTORY)

    for match in re.finditer(MY_HISTORY_JOIN[-2:], MY_HISTORY_JOIN):
        if match.end() < len(MY_HISTORY_JOIN):
            common_response = MY_HISTORY_JOIN[match.end()]
            if common_response == 'R':
                MY_DOUBLE_THROW[0] += 1
            elif common_response == 'P':
                MY_DOUBLE_THROW[1] += 1
            elif common_response == 'S':
                MY_DOUBLE_THROW[2] += 1

    response = MY_DOUBLE_THROW.index(max(MY_DOUBLE_THROW))
    MY_DOUBLE_GUESS = MOVES[response]
    MY_DOUBLE_RESPONSE = BEATS[MY_DOUBLE_GUESS]
    return MY_DOUBLE_RESPONSE

def get_output(SINGLE_RESPONSE, DOUBLE_RESPONSE, MY_SINGLE_RESPONSE, MY_DOUBLE_RESPONSE):

    CHOICES = [SINGLE_RESPONSE, DOUBLE_RESPONSE, MY_DOUBLE_RESPONSE, MY_SINGLE_RESPONSE]
    SINGLE_ROLL = randint(0,SINGLE_WEIGHT)
    DOUBLE_ROLL = randint(0, DOUBLE_WEIGHT)
    MY_SINGLE_ROLL = randint(0, MY_SINGLE_WEIGHT)
    MY_DOUBLE_ROLL = randint(0, MY_DOUBLE_WEIGHT)

    ROLLS_LIST=[SINGLE_ROLL, DOUBLE_ROLL, MY_SINGLE_ROLL, MY_DOUBLE_ROLL]
    MAX_ROLL = max(ROLLS_LIST)
    MAX_INDEX = ROLLS_LIST.index(MAX_ROLL)
    FINAL_GUESS = CHOICES[MAX_INDEX]
    FINAL_RESPONSE = BEATS[FINAL_GUESS]
    return FINAL_RESPONSE


if(1):
    if input == "": # no input means first round, initialize variables that will hold things long-term
        THEIR_HISTORY = []
        MY_HISTORY = []
        SINGLE_THROW = [0,0,0]
        DOUBLE_THROW = [0,0,0]
        MY_SINGLE_THROW = [0,0,0]
        MY_DOUBLE_THROW = [0,0,0]
        TRIPLE_THROW = [0,0,0]

        round = 0
        output = random.choice(MOVES)
        MY_HISTORY.append(output)
        round += 1
    else:
        THEIR_HISTORY.append(input)
        if round < 10: # relatively new game, just throw random output
            output = random.choice(MOVES)
            MY_HISTORY.append(output)
            round += 1

        else:
            SINGLE_RESPONSE = their_single_history(THEIR_HISTORY, input, SINGLE_THROW)
            DOUBLE_RESPONSE = their_double_history(THEIR_HISTORY, input, DOUBLE_THROW)
            MY_SINGLE_RESPONSE = my_single_history(MY_HISTORY, MY_SINGLE_THROW)
            MY_DOUBLE_RESPONSE = my_single_history(MY_HISTORY, MY_DOUBLE_THROW)
            output = get_output(SINGLE_RESPONSE, DOUBLE_RESPONSE, MY_SINGLE_RESPONSE, MY_DOUBLE_RESPONSE)


            MY_HISTORY.append(output)