Advertisement
dougbaker45

football.0.5

Aug 18th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.43 KB | None | 0 0
  1. import random
  2. from timeit import default_timer as timer
  3.  
  4. global scores
  5. start = timer()
  6. totalplays = 150
  7. gametime = 60
  8. playtime = .4
  9. hometeam = 'patriots'
  10. awayteam = 'rams'
  11. posession = None
  12. ballposition = 0
  13. scores = {hometeam:0, awayteam:0}
  14. totalplays = {}
  15. down = 0
  16. seriesyards = 0
  17. touchdown = 'No'
  18. awayteamscores = 0
  19. hometeamscores = 0
  20. totalscores = {hometeam:0, awayteam:0}
  21. totalrunplays = 0
  22. totalpassplays = 0
  23. totalpuntplays = 0
  24. totalfieldgoalplays = 0
  25. totalrushyards = 0
  26. totalpassyards = 0
  27. totalpuntyards = 0
  28. hometeammod = {}
  29.  
  30. def teamstats():
  31.     import csv
  32.  
  33.     with open('football_offense.csv', newline='') as csvfile:
  34.          offense = csv.DictReader(csvfile)
  35.          for row in offense:
  36.              print(row['Tm'],'cmp%', row['Passing Comp Percent'])
  37.  
  38. def cointoss():
  39.     global gametime
  40.     global posession
  41.     global halftimeposession
  42.     global scores
  43.     gametime = 60
  44.     scores = {hometeam:0, awayteam:0}
  45.     cointossresult = random.randint(1, 2)
  46.     if cointossresult == 1:
  47.         posession = hometeam
  48.         halftimeposession = awayteam
  49.     else:
  50.         posession = awayteam
  51.         halftimeposession = hometeam
  52.     #print('cointoss winner', posession)
  53.  
  54. def halftime():
  55.     global posession
  56.     global halftimeposession
  57.     global scores
  58.     global ballposition
  59.     if ballposition >= 50:
  60.             fieldgoalodds = ballposition + random.randint(25, 50)
  61.             if fieldgoalodds >= 95:
  62.                 scores[posession] += 3
  63.                 #print('fieldgoal')
  64.     posession = halftimeposession
  65.     #print('halftime')
  66.     kickoff()
  67.  
  68. def kickoff():
  69.     global ballposition
  70.     global gametime
  71.     global posession
  72.     global down
  73.     ballposition = 0
  74.     returnteam = posession
  75.     kickofftd = random.randint(1, 100)
  76.     fumblechance = random.randint(1, 1000)
  77.     gametime -= playtime
  78.     if kickofftd <= 1:
  79.         scores[returnteam] += 6
  80.         turnover()
  81.         kickoff()
  82.     else:
  83.         ballposition += 25
  84.         down = 1
  85.         kickoffbigplay = random.randint(1, 100)
  86.         if kickoffbigplay <= 4:
  87.             ballposition += random.randint(1, 25)
  88.             #print('Kickoff big play!', kickoffbigplay)
  89.         if fumblechance <= 5:
  90.             turnover()
  91.             #print('Fumble')
  92.             #print('posession', posession)
  93.             #print('ballposition', ballposition)
  94.             #print('gametime', gametime)
  95.     #print('ballposition', ballposition)
  96.     #print('gametime', gametime)
  97.  
  98. def chooseplay():
  99.     global posession
  100.     global down
  101.     global playchoice
  102.     global ballposition
  103.     offense = posession
  104.     if down < 4:
  105.         playchoice = random.randint(1, 100)
  106.         if playchoice >= 55:
  107.             playchoice = 'passplay'
  108.         else: playchoice = 'runplay'
  109.     else:
  110.         if ballposition > 60:
  111.             playchoice = 'fieldgoalplay'
  112.         else:
  113.             playchoice = 'puntplay'
  114.     #print(playchoice)
  115.  
  116. def passplayexecute():
  117.     global posession
  118.     global nonposession
  119.     global ballposition
  120.     global down
  121.     global seriesyards
  122.     global totalpassyards
  123.     global totalpassplays
  124.     totalpassplays += 1
  125.     completionchace = random.randint(1,100)
  126.     if completionchace <= 66:
  127.         #print('Pass complete')
  128.         down += 1
  129.         playyards = 11 + random.randint(-11, 13)
  130.         totalpassyards += playyards
  131.         #print('playyards', playyards)
  132.         ballposition += playyards
  133.         seriesyards += playyards
  134.         if ballposition >= 100:
  135.             scores[posession] += 7
  136.             #print(' passing touchdown')
  137.             #print(scores)
  138.             touchdown = 'yes'
  139.             turnover()
  140.             kickoff()
  141.         if seriesyards >= 10:
  142.             down = 1
  143.             #print('first down', down)
  144.             seriesyards = 0
  145.             #print('ballposition', ballposition)
  146.     else:
  147.         down += 1
  148.         #print('incomplete')
  149.  
  150. def runplayexecute():
  151.     global posession
  152.     global ballposition
  153.     global down
  154.     global seriesyards
  155.     global touchdown
  156.     global totalrunplays
  157.     global totalrushyards
  158.     totalrunplays += 1
  159.     rungainchance = random.randint(1,100)
  160.     if rungainchance <= 70:
  161.         #print('Positive Run')
  162.         down += 1
  163.         playyards = int(4.2 + (random.randint(-30, 100) / 10))
  164.         #print('playyards', playyards)
  165.         ballposition += playyards
  166.         seriesyards += playyards
  167.         if ballposition >= 100:
  168.             scores[posession] += 7
  169.             #print('rushing touchdown')
  170.             #print(scores)
  171.             turnover()
  172.             kickoff()
  173.         if seriesyards >= 10:
  174.             down = 1
  175.             #print('first down', down)
  176.             seriesyards = 0
  177.             #print('ballposition', ballposition)
  178.     else:
  179.         down += 1
  180.         #print('negtive run')
  181.         playyards = int(random.randint(-3, 0))
  182.         #print('playyards', playyards)
  183.         ballposition += playyards
  184.         seriesyards += playyards
  185.     totalrushyards += playyards
  186.  
  187. def puntplayexecute():
  188.     global ballposition
  189.     global posession
  190.     global ballposition
  191.     global down
  192.     global totalpuntplays
  193.     global totalpuntyards
  194.     totalpuntplays += 1
  195.     puntyards = 45 + random.randint(-20, 20)
  196.     totalpuntyards += puntyards
  197.     #print('puntyards', puntyards)
  198.     ballposition += puntyards
  199.     if ballposition >= 100:
  200.         ballposition = 80
  201.     turnover()
  202.  
  203. def fieldgoalplayexecute():
  204.     global scores
  205.     global ballposition
  206.     global totalfieldgoalplays
  207.     totalfieldgoalplays += 1
  208.     fieldgoalodds = ballposition + random.randint(25, 50)
  209.     if fieldgoalodds >= 95:
  210.         scores[posession] += 3
  211.         #print('fieldgoal', scores)
  212.         turnover()
  213.         kickoff()
  214.     else:
  215.         turnover()
  216.  
  217. def turnover():
  218.     global down
  219.     global ballposition
  220.     global posession
  221.     global seriesyards
  222.     down = 1
  223.     ballposition = abs(ballposition - 100)
  224.     if posession == hometeam:
  225.         posession = awayteam
  226.     else:
  227.         posession = hometeam
  228.     seriesyards = 0
  229.     #print('posession', posession)
  230.     #print('ballposition', ballposition)
  231.     #print('gametime', gametime)
  232.  
  233. def executeplay():
  234.     global playchoice
  235.     global gametime
  236.     gametime -= playtime
  237.     if playchoice == 'passplay':
  238.         passplayexecute()
  239.     if playchoice == 'runplay':
  240.         runplayexecute()
  241.     if playchoice == 'fieldgoalplay':
  242.         fieldgoalplayexecute()
  243.     if playchoice == 'puntplay':
  244.         puntplayexecute()
  245.  
  246. def game():
  247.     cointoss()
  248.     kickoff()
  249.     while gametime >= 30:
  250.         chooseplay()
  251.         executeplay()
  252.     halftime()
  253.     while gametime >= 0:
  254.         chooseplay()
  255.         executeplay()
  256.     print('scores: ', scores)
  257.     print('gametime Over')
  258.     global wins
  259.     winner = max(scores.keys(), key=(lambda k: scores[k]))
  260.     print('winner :', winner)
  261.     global totalscores
  262.     hometeamscores = scores[hometeam]
  263.     awayteamscores = scores[awayteam]
  264.     totalscores[hometeam] += hometeamscores
  265.     totalscores[awayteam] += awayteamscores
  266.     print(totalscores)
  267.     print('Yards per attempt: ', (totalpassyards / totalpassplays))
  268.     print('Yards per Carry: ', (totalrushyards / totalrunplays))
  269.     print('yards per punt: ', (totalpuntyards / totalpuntplays))
  270.     print('Average scores :', (totalscores[hometeam] / 1), (totalscores[awayteam] / 1))
  271.  
  272.  
  273.  
  274.  
  275. for i in range(1):
  276.     game()
  277.     end = timer()
  278.     print('time: ', (end - start) * 10)
  279. teamstats()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement