Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. import random
  2.  
  3. max_outs = 3
  4. innings = 9
  5. game_num = 100000 #num games to run for each matchup
  6.  
  7. def batter_up(batting_power,batting_perc,i_bases,i_outs):
  8. hit = random.random() < batting_perc
  9. if hit:
  10. if batting_power == 1:
  11. #if you hit a single, advance runners and put a runner on first
  12. i_bases[3] += i_bases [2]
  13. i_bases[2] = i_bases[1]
  14. i_bases[1] = i_bases[0]
  15. i_bases[0] = 1
  16.  
  17. if batting_power == 2:
  18. #if you hit a double, advance runners two and put a runner on second
  19. i_bases[3] += i_bases [1]
  20. i_bases[1] = 1
  21.  
  22. if batting_power == 4:
  23. #if you hit a double, advance runners two and put a runner on second
  24. i_bases[3] += 1
  25. else: i_outs +=1
  26.  
  27. return i_bases, i_outs
  28.  
  29.  
  30. def play_inning(batting_power,batting_perc):
  31. i_outs = 0
  32. i_runs = 0
  33. i_bases = [0,0,0,0]
  34. while i_outs < max_outs:
  35. i_bases, i_outs = batter_up(batting_power,batting_perc,i_bases,i_outs)
  36. i_runs = i_bases[3]
  37. return i_runs
  38.  
  39. def extra_innings(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc):
  40. scores = [0,0]
  41.  
  42. #play innings until a team is in the lead
  43. while scores[0] == scores[1]:
  44. scores[0] += play_inning(t1_bat_power,t1_bat_perc)
  45. scores[1] += play_inning(t2_bat_power,t2_bat_perc)
  46. return scores[0] > scores[1]
  47.  
  48. def play_game(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc):
  49. scores = [0,0]
  50.  
  51. #play innings
  52. for i in range(innings):
  53. scores[0] += play_inning(t1_bat_power,t1_bat_perc)
  54. scores[1] += play_inning(t2_bat_power,t2_bat_perc)
  55.  
  56. #score is a tie
  57. if scores[0] == scores[1]:
  58. return extra_innings(t1_bat_power,t1_bat_perc,t2_bat_power,t2_bat_perc)
  59. #otherwise return if team 1 is bigger than 2
  60. else: return scores[0] > scores[1]
  61.  
  62. #Run games (can be done more efficiently but w/e)
  63.  
  64. #Singles team:
  65. t1_wins = 0
  66. for i in range(game_num):
  67. t1_wins += play_game(1,0.4,2,0.2)
  68. t1_wins += play_game(1,0.4,4,0.1)
  69.  
  70. print("Single team: ",float(t1_wins))
  71.  
  72. #Doubles team:
  73. t2_wins = 0
  74. for i in range(game_num):
  75. t2_wins += play_game(2,0.2,1,0.4)
  76. t2_wins += play_game(2,0.2,4,0.1)
  77.  
  78. print("Doubles team: ",float(t2_wins))
  79.  
  80. #Homers team:
  81. t3_wins = 0
  82. for i in range(game_num):
  83. t3_wins += play_game(4,0.1,2,0.2)
  84. t3_wins += play_game(4,0.1,1,0.4)
  85.  
  86. print("Homers team: ",float(t3_wins))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement