Author | Joe |
Submission date | 2014-08-04 16:35:53.392917 |
Rating | 4154 |
Matches played | 575 |
Win rate | 38.43 |
Use rpsrunner.py to play unranked matches on your computer.
import random
def most_likely(history):
if history != []:
d = {"R": history.count("R"), "P": history.count("P"), "S": history.count("S")}
return max(d, key=d.get)
def opposite(move):
if move == "R":
return "P"
elif move == "P":
return "S"
else:
return "R"
def updateStats(myMove, theirMove, results):
if (myMove == "R" and theirMove == "S" or
myMove == "P" and theirMove == "R" or
myMove == "S" and theirMove == "P"):
results[0] += 1
elif myMove == theirMove:
results[2] += 1
else:
results[1] += 1
def winPercentage(results):
if results[0] == 0:
print results[0]
return 0
elif results[1] == 0:
return 100
return (float(results[0]) / (results[0] + results[1])) * 100
choices = ["R", "P", "S"]
if input == "":
my_history = []
their_history = []
results = [0, 0, 0]
output = random.choice(choices)
elif input in choices:
if my_history and their_history:
updateStats(my_history[len(my_history) - 1], their_history[len(their_history) - 1], results)
their_history.append(input)
smart_choice = opposite(opposite(most_likely(my_history)))
counter_choice = opposite(most_likely(their_history))
random_choice = random.choice(choices)
output = smart_choice if (random.random() >= 0.3) else counter_choice
my_history.append(output)