Author | ElectraMiner |
Submission date | 2018-04-07 19:28:10.271190 |
Rating | 1473 |
Matches played | 309 |
Win rate | 13.92 |
Use rpsrunner.py to play unranked matches on your computer.
import random
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
else:
w[i] = -((1.0 / 3) - freq[i])**5
return w
if (input == ""): #Initialize
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
#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"
output = move
#if gameRandom: #Overwrite with random move to confuse opponent
# output = random.choice(["R","P","S"])
lastInput = input
realMove = output