Author | flug |
Submission date | 2019-06-04 00:22:20.770124 |
Rating | 1358 |
Matches played | 232 |
Win rate | 14.66 |
Use rpsrunner.py to play unranked matches on your computer.
#test 3 - worsenified 0.1
#Author ElectraMiner
#Submission date 2018-04-07 19:22:43.480890
#Worsenified by flug, 2019-06-02, to see if we can make it even worse
import random
debug = 0
def beats(move, oppMove): #Score of a game, 1 = win 0.5 = tie 0 = loss
s = move + oppMove
if s == "RS" or s == "PR" or s == "SP":
return 1
if move == oppMove:
return 0.5
return 0
def toInt(move): #Turns RPS into 012
if move == "R":
return 0
if move == "P":
return 1
return 2
def greatest(freq):
if freq[0] > freq[1] and freq[0] > freq[2]:
return 0
if freq[1] > freq[2]:
return 1
return 2
def weights(freq):
w = [0,0,0]
for i in range(3):
if freq[i] > (1.0 / 3):
w[i] = ((1.0 / 3) - freq[i])**5
return w
if (input == ""): #Initialize
choice1 = ""
saveinput = ""
saveoutput = ""
rounds=0
won=0
lost=0
draw=0
hold=0
match = 0
wonGamesWeighted = 0
totalGamesWeighted = 0
weightCoeff = 0.9
move = "R"
lastInput = "R"
lastMove = "R"
realMove = "R"
gameRandom = True
#Game history info
loopHistory = []
loopHistoryWeighted = []
loopSize = 5
for i in range(loopSize):
loopHistory.append([])
loopHistoryWeighted.append([])
for j in range(i+1):
loopHistory[i].append([0,0,0])
loopHistoryWeighted[i].append([0,0,0])
moveHistory = []
moveHistoryWeighted = []
for i in range(3):
moveHistory.append([])
moveHistoryWeighted.append([])
for j in range(3):
moveHistory[i].append([0,0,0])
moveHistoryWeighted[i].append([0,0,0])
moveModes = [0,0,0]
moveModesMe = [0,0,0]
moveModesWeighted = [0,0,0]
moveModesMeWeighted = [0,0,0]
commonMoves = []
commonMovesWeighted = []
for i in range(3):
commonMoves.append([])
commonMovesWeighted.append([])
for j in range(3):
commonMoves[i].append([0,0,0])
commonMovesWeighted[i].append([0,0,0])
else: #After the first game
rounds += 1;
#Calculate accuracy
match += 1
wonGamesWeighted *= weightCoeff
totalGamesWeighted *= weightCoeff
wonGamesWeighted += beats(move, input)
totalGamesWeighted += 1
accuracy = wonGamesWeighted / totalGamesWeighted
gameRandom = random.uniform(0,1) > accuracy
#Loop checker
for i in range(loopSize):
for j in range(i):
for m in range(3):
loopHistoryWeighted[i][j][m] *= weightCoeff
for i in range(loopSize):
loopHistory[i][match % (i+1)][toInt(input)] += 1
loopHistoryWeighted[i][match % (i+1)][toInt(input)] += 1
#Last turn checker
for i in range(3):
for j in range(3):
for m in range(3):
moveHistoryWeighted[i][j][m] *= weightCoeff
moveHistory[toInt(lastInput)][toInt(lastMove)][toInt(input)] += 1
moveHistoryWeighted[toInt(lastInput)][toInt(lastMove)][toInt(input)] += 1
for m in range(3):
moveModesWeighted[m] *= weightCoeff
moveModesMeWeighted[m] *= weightCoeff
#Common move checker
for i in range(3):
for j in range(3):
for m in range(3):
commonMovesWeighted[i][j][m] *= weightCoeff
moveModesWeighted[i] *= weightCoeff
moveModesMeWeighted[i] *= weightCoeff
commonMoves[greatest(moveModes)][greatest(moveModesMe)][toInt(input)] += 1
commonMovesWeighted[greatest(moveModesWeighted)][greatest(moveModesMeWeighted)][toInt(input)] += 1
moveModes[toInt(input)] += 1
moveModesWeighted[toInt(input)] += 1
moveModesMe[toInt(realMove)] += 1
moveModesMeWeighted[toInt(realMove)] += 1
#Combines different possibile strategies
strategies = []
#for i in range(loopSize):
# strategies.append(loopHistory[i][match % (i+1)])
# strategies.append(loopHistoryWeighted[i][match % (i+1)])
strategies.append(moveHistory[toInt(input)][toInt(realMove)])
strategies.append(moveHistoryWeighted[toInt(input)][toInt(realMove)])
#strategies.append(commonMoves[greatest(moveModes)][greatest(moveModesMe)])
#strategies.append(commonMovesWeighted[greatest(moveModesWeighted)][greatest(moveModesMeWeighted)])
probability = [0,0,0]
for s in strategies:
w = weights(s)
for i in range(3):
probability[i] += w[i]
lastMove = realMove
g = greatest(probability)
if g == 0:
move = "P"
elif g == 1:
move = "S"
else:
move = "R"
# if g == 0:
# move = "R"
# elif g == 1:
# move = "P"
# else:
# move = "S"
#figure up total win/lose/draw
if saveoutput==input:
draw += 1
elif (input == "R" and saveoutput == "S") or (input == "S" and saveoutput == "P") or (input == "P" and saveoutput == "R"):
lost += 1
else:
won += 1
##If we have a decent little loss buildup, we just try to hold it
#if (hold == 1 or won < (lost - 150) or (rounds > 500 and won < (lost - 40))):
choice1 = ""
if (hold == 1 or won < (lost - 50) or (rounds > 500 and won < (lost - 25))):
choice1 = "random"
if hold == 0:
if (debug == 1): print rounds, won, lost, draw
if (debug == 1): print "HOLDING: ", choice1, rounds
hold = 1
output = move
if choice1 == "random":
output = random.choice(["R","P","S"])
#print "TEST3: ", output
#if gameRandom: #Overwrite with random move to confuse opponent
# output = random.choice(["R","P","S"])
saveoutput = output
lastInput = input
realMove = output