Author | Alphazone |
Submission date | 2018-07-08 17:09:49.510220 |
Rating | 3691 |
Matches played | 290 |
Win rate | 34.48 |
Use rpsrunner.py to play unranked matches on your computer.
#Run as many small universes as we can, apply best one:
if input == "":
import random
nvm = 10
nops = 6
naddr = 10
def make():
pspace = [[random.random() for _ in range(nops)] for _ in range(naddr)]
for cell in pspace:
s = sum(cell)
for n in cell:
n /= s
return {
"p": pspace,
"pc": 0,
"dp": 0,
"score": 0,
"move": "S",
}
vms = [make() for _ in range(nvm)]
for vm in vms:
vm["d"] = [0 for _ in range(naddr)]
def randselect(P):
r = random.random()
for i in range(len(P)):
p = P[i]
if r < p:
return i
r -= p
return len(P) - 1
def encode(x):
if x == "S":
return 0
elif x == "P":
return 1
return 2
def decode(x):
x = x % 3
if x == 0:
return "S"
elif x == 1:
return "P"
else:
return "R"
def payoff(mymove, opponents):
if mymove == "R":
if opponents == "R":
return 0
elif opponents == "P":
return -1
else: #S
return 1
elif mymove == "P":
if opponents == "R":
return 1
elif opponents == "P":
return 0
else: #S
return -1
elif mymove == "S":
if opponents == "R":
return -1
elif opponents == "P":
return 1
else: #S
return 0
def step(vm):
def halt():
return vm["move"]
pspace = vm["p"]
pc = vm["pc"]
if pc >= len(pspace):
return halt()
dp = vm["dp"]
dspace = vm["d"]
data = dspace[dp]
opcode = randselect(pspace[pc])
vm["pc"] += 1
if opcode == 0:
dspace[dp] += 1
elif opcode == 1:
dspace[dp] -= 1
elif opcode == 2:
vm["dp"] = min(dp + 1, len(pspace) - 1)
elif opcode == 3:
vm["dp"] = max(dp - 1, 0)
elif opcode == 4:
return decode(data)
elif opcode == 5:
dspace[dp] = encode(input)
return None
def run():
for vm in vms:
if input != "":
vm["score"] += payoff(vm["move"], input)
vm["pc"] = 0
res = None
while res is None:
res = step(vm)
vm["move"] = res
return max(vms, key = lambda x: x["score"])["move"]
output = run()