Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- def randomRound(Competitors, roundNumber, nextRound): #Randomizes the rounds by choosing 2 random competitors
- while len(Competitors) >> 0:
- randomNumber1 = random.randint(0, len(Competitors)-1) #chooses random number
- Competitor1 = Competitors[randomNumber1] #uses the random number to get the first compeitor for the match
- Competitors.remove(Competitor1)
- randomNumber2 = random.randint(0, len(Competitors)-1) #chooses random number
- Competitor2 = Competitors[randomNumber2] #uses the random number to get the first compeitor for the match
- Competitors.remove(Competitor2)
- roundNumber += 1
- nextRound.append(Competitor1)
- nextRound.append(Competitor2)
- print 'Match #', roundNumber, Competitor1, 'vs ', Competitor2
- return roundNumber
- def startRound(roundNumberChecker, winners, nextRound, roundNumber, byes): #asks who won each match and sets up matches for the next round
- winners = []
- ready = raw_input('Type "y" to continue the next round ') #wait to ask until user is ready
- if ready == 'y':
- pass
- else:
- ready = raw_input('Invalid input. When finished type "y" to continue to the next round ')
- while len(nextRound) > 1: #asks who won each match
- winner = raw_input('Type in the winner of match #{}: '.format(roundNumberChecker+1))
- if winner == nextRound[0]: #checks if the input is a valid winner of the match
- winners.append(nextRound[0])
- nextRound.remove(nextRound[0])
- nextRound.remove(nextRound[0])
- roundNumberChecker += 1
- elif winner == nextRound[1]:
- winners.append(nextRound[1])
- roundNumberChecker += 1
- nextRound.remove(nextRound[0])
- nextRound.remove(nextRound[0])
- else:
- print 'Invalid winner'
- print 'Next Round'
- winners = winners + byes #buts the compeitors with byes into the next round
- while len(winners) > 1: #sets up the next matches in order
- print 'Match #{}:'.format(roundNumber+1), winners[0], 'vs ', winners[1]
- nextRound.append(winners[0])
- winners.remove(winners[0])
- nextRound.append(winners[0])
- winners.remove(winners[0])
- roundNumber +=1
- if len(winners) == 1: #if there was only one winner left the champion is revealed
- print winners[0] + ' is the champion! Congratulations ' + winners[0] + '!'
- else:
- pass
- return nextRound
- def bracketmaker():
- roundNumberChecker = 0 #match number for when checking who won
- nextRound=[] #list that holds all competitors from the current round
- winners=[] #list that holds winners of the round
- byeNumber = 0 #bye number for the people with byes
- competitorCounter = 0 #how many competitors there are
- Competitors = [] #list that holds competitor names
- roundNumber = 0 #counts up for the match numbers
- byes = [] #list that holds the competitors with byes
- numCompetitors = int(raw_input('Enter number of competitors: ')) #input how many competitors
- if numCompetitors <= 1:
- print 'Error. Please enter a value greater than 1.' #if 1 then a bracket is not need and an error is given
- numCompetitors = (raw_input('Enter number of competitors: '))
- else:
- pass
- while numCompetitors >= 1: #input the name of each competitor
- Competitor = (raw_input('Enter competitor #{}: '.format(competitorCounter+1)))
- Competitors.append(Competitor)
- numCompetitors = numCompetitors-1
- competitorCounter = competitorCounter+1
- else:
- pass
- if math.log(len(Competitors), 2.0).is_integer(): #if there are a power of 2 competitors then no byes are needed
- randomRound(Competitors, roundNumber, nextRound) #runs randomize round
- else:
- k = math.log(len(Competitors), 2.0) #calculates the log
- float(k)
- byeNum = int(2**round(k + .5) - len(Competitors)) #rounds the log up to the next whole number for how many byes are needed
- while byeNum > 0:
- randomNumber3 = random.randint(0, len(Competitors)-1) #chooses a random number
- competitorBye = Competitors[randomNumber3] #uses the random number to select who gets the bye
- Competitors.remove(competitorBye)
- byeNum -= 1
- byes.append(competitorBye)
- print 'Bye #{} '.format(byeNumber+1), competitorBye
- byeNumber += 1
- else:
- randomRound(Competitors, roundNumber, nextRound) #runs the round randomizer
- while len(nextRound) > 1: #runs the next round function when there is more than one competitor left
- nextRound = startRound(roundNumberChecker, winners, nextRound, roundNumber, byes)
- byes = []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement