Skip to content

Instantly share code, notes, and snippets.

@74togo
Created September 13, 2012 18:49
Show Gist options
  • Save 74togo/3716658 to your computer and use it in GitHub Desktop.
Save 74togo/3716658 to your computer and use it in GitHub Desktop.
A demonstration of the Elo rating system for 2-player competitions.
class Player:
""" The "player" to be ranked """
def __init__(self):
""" Base rating of 1600 always """
self.rating = 1600
def getWinProb(self, opponent):
""" Probablility that self will win against opponent """
return 1.0 / ( 10**(( opponent.rating - self.rating)/400) + 1 )
def elo(winner, loser):
# Constant for forumla
K = 20
# Winner's new rating
a = winner.rating + (K * (1 - winner.getWinProb(loser)))
# Loser's new rating
b = loser.rating + (K * (0 - loser.getWinProb(winner)))
# Set players' scores
winner.rating = int(a + 0.5)
loser.rating = int(b + 0.5)
alex = Player()
bob = Player()
while __name__ == "__main__":
print "\n" + "=" * 10 + "\nAlex: ", alex.rating
print "Bob : ", bob.rating, "\n" + "=" * 10
i = raw_input("Should Alex or Bob win (a/b)? ")
if "a" in i:
elo(alex, bob)
elif "b" in i:
elo(bob, alex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment