Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BillyBobBlood/9ec122a5a2b12dab21ea0ad5a219e335 to your computer and use it in GitHub Desktop.
Save BillyBobBlood/9ec122a5a2b12dab21ea0ad5a219e335 to your computer and use it in GitHub Desktop.
import random
from tournament import connect
from tournament import reportMatch
from tournament import deleteMatches
from tournament import deletePlayers
rounds = 30
the_players = [
(1, 'Jeff'),
(2, 'Adarsh'),
(3, 'Amanda'),
(4, 'Eduardo'),
(5, 'Philip'),
(6, 'Jee')
]
def registerPlayerUpdated(player_id, name):
"""Add a player to the tournament database.
The database assigns a unique serial id number for the player. (This
should be handled by your SQL database schema, not in your Python code.)
Args:
name: the player's full name (need not be unique).
"""
db = connect()
db_cursor = db.cursor()
query = "INSERT INTO players (id, name) VALUES (%s, %s)"
db_cursor.execute(query, (player_id, name))
db.commit()
db.close()
def createRandomMatches(player_list, rounds):
""" Note: I have replaced 'num matches' with 'rounds'."""
num_players = len(player_list)
p_index_lst = range(0, num_players)
r = 1
for r in range(1, rounds + 1):
print "\nRound" + str(r)
r_pindex_list = random.sample(p_index_lst, num_players)
while len(r_pindex_list) > 0:
p1 = r_pindex_list.pop(0)
p2 = r_pindex_list.pop(0)
(winner_id, winner_name) = player_list[p1]
(loser_id, loser_name) = player_list[p2]
reportMatch(winner_id, loser_id)
print "%s (id=%s) beat %s (id=%s)" % (
winner_name,
winner_id,
loser_name,
loser_id)
def setup_players_and_matches()
deleteMatches()
deletePlayers()
for player in the_players:
registerPlayerUpdated(player[0], player[1])
createRandomMatches(the_players, rounds)
if __name__ == '__main__':
setup_players_and_matches()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment