Author | ffenliv |
Submission date | 2019-01-22 18:25:11.750459 |
Rating | 3019 |
Matches played | 257 |
Win rate | 30.35 |
Use rpsrunner.py to play unranked matches on your computer.
import random
from random import randint
import re
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 = 10
DOUBLE_WEIGHT = 30
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]
round = 0
output = "P"
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: # running at least 10 rounds, start using history to make a choice
THEIR_LAST_THROW = input
MY_LAST_THROW = MY_HISTORY[-1]
# using single-throw response history for basic guess #
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
response = SINGLE_THROW.index(max(SINGLE_THROW))
SINGLE_GUESS = MOVES[response]
SINGLE_RESPONSE = BEATS[SINGLE_GUESS]
# using double-throw response history for less basic guess #
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
reponse = DOUBLE_THROW.index(max(DOUBLE_THROW))
DOUBLE_GUESS = MOVES[response]
DOUBLE_RESPONSE = BEATS[DOUBLE_GUESS]
SINGLE_ROLL = randint(0,SINGLE_WEIGHT)
DOUBLE_ROLL = randint(0, DOUBLE_WEIGHT)
if SINGLE_ROLL > DOUBLE_ROLL:
output = SINGLE_RESPONSE
else:
output = DOUBLE_RESPONSE