View Single Post
Old 07-25-2014, 10:53 PM   #14
Groundhog
Coordinator
 
Join Date: Dec 2003
Location: Sydney, Australia
Code:
def jumpball(PlayerHome5, PlayerHome4, PlayerAway5, PlayerAway4, HomeTeam, AwayTeam): #Determine best jumper for both sides. My 1st attempt to make this #more clever didn't work: #home5jumpabl = sum(PlayerHome5[2:3]) #home4jumpabl = sum(PlayerHome4[2:3]) #away5jumpabl = sum(PlayerAway5[2:3]) #away4jumpabl = sum(PlayerAway4[2:3]) home5jumpabl = PlayerHome5[2] + PlayerHome5[3] home4jumpabl = PlayerHome4[2] + PlayerHome4[3] away5jumpabl = PlayerAway5[2] + PlayerAway5[3] away4jumpabl = PlayerAway4[2] + PlayerAway4[3] if home5jumpabl > home4jumpabl: homejumper = [PlayerHome5[1], home5jumpabl] elif home4jumpabl > home5jumpabl: homejumper = [PlayerHome4[1], home4jumpabl] else: homejumper = [PlayerHome5[1], home5jumpabl] if away5jumpabl > away4jumpabl: awayjumper = [PlayerAway5[1], away5jumpabl] elif away4jumpabl > away5jumpabl: awayjumper = [PlayerAway4[1], away4jumpabl] else: awayjumper = [PlayerAway5[1], away5jumpabl] #Jumpball calculations: homeplayerd20 = randrange(1,20) + homejumper[1] awayplayerd20 = randrange(1,20) + awayjumper[1] randomresult = 0 if homeplayerd20 > awayplayerd20: winner = [homejumper[0], HomeTeam[1], HomeTeam[2]] #this is where the other gamestate variables would be set. elif awayplayerd20 > homeplayerd20: winner = [awayjumper[0], AwayTeam[1], AwayTeam[2]] #as above - gamestate variables. else: randomresult = randrange(1,2) if randomresult == 2: winner = [AwayTeam[1], AwayTeam[2]] else: winner = [HomeTeam[1], HomeTeam[2]] #as above - gamestate variables. #Jumpball Commentary print "Welcome to {}, where tonight the {} {} are set to square off against the visiting {} {}.".format(HomeTeam[4], HomeTeam[1], HomeTeam[2], AwayTeam[1], AwayTeam[2], AwayTeam[2]) print "{} and {} are in the middle for the opening tip.".format(homejumper[0],awayjumper[0]) print "The ball is up..." if randomresult != 0: print "Both players get a hand to it, but the {} {} come up with possession.".format(winner[0],winner[1]) else: print "{} wins the tip, {} {} with the ball.".format(winner[0], winner[1], winner[2])

Running this from the Python shell returns:

Code:
>>> jumpball(PlayerHome5.list, PlayerHome4.list, PlayerAway5.list, PlayerAway4.list, HomeTeam, AwayTeam) Welcome to Quicken Loans Arena, where tonight the Cleveland Cavaliers are set to square off against the visiting Charlotte Hornets. A. Varejao and N. Vonleh are in the middle for the opening tip. The ball is up... A. Varejao wins the tip, Cleveland Cavaliers with the ball.

Woo hoo! Success! I took it out of the code above, but I was printing all the variables initially to confirm things were working as I expected them to. So there we have it, a simulated jump ball! I can already see a few ways to cut down on the amount of variables and lines of code I used above, but I was more keen to show I could make it work rather than make it work the most efficient way possible. Now, back to theory...
__________________
Politics, n. Strife of interests masquerading as a contest of principles.
--Ambrose Bierce

Last edited by Groundhog : 07-25-2014 at 10:56 PM.
Groundhog is offline   Reply With Quote