Advertisement
dougbaker45

football_0.6

Aug 19th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.38 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 = 'New England Patriots'
  10. awayteam = 'Los Angeles Rams'
  11. possession = 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.     global possession
  32.     import csv
  33.     import pandas as pd
  34.  
  35.     if possession == hometeam:
  36.         nonpossession = awayteam
  37.     else: nonpossession = hometeam
  38.  
  39.     with open('offense.csv', newline='') as csvfile:
  40.          offenses = csv.DictReader(csvfile)
  41.          for row in offenses:
  42.              #print(row)
  43.              if row['Tm'] == hometeam:
  44.                 hometeamoffense = row
  45.              if row['Tm'] == awayteam:
  46.                 awayteamoffense = row
  47.                  #for i in row:
  48.  
  49.     print(hometeamoffense)
  50.     print(awayteamoffense)
  51.     with open('defense.csv', newline='') as csvfile:
  52.          defenses = csv.DictReader(csvfile)
  53.          for row in defenses:
  54.              #print(row)
  55.              if row['Tm'] == hometeam:
  56.                  hometeamdefense = row
  57.              if row['Tm'] == awayteam:
  58.                  awayteamdefense = row
  59.     print(hometeamdefense)
  60.     print(awayteamdefense)
  61.  
  62.  
  63.     #with open('defense.csv', newline='') as csvfile:
  64.         # defenses = csv.DictReader(csvfile)
  65.         # for row in defenses:
  66.             # print(row)
  67.  
  68.     #ypc = (offenses['Rushing  Y/A'])
  69.     #print(ypc)
  70.  
  71. def cointoss():
  72.     global gametime
  73.     global possession
  74.     global halftimepossession
  75.     global scores
  76.     gametime = 60
  77.     scores = {hometeam:0, awayteam:0}
  78.     cointossresult = random.randint(1, 2)
  79.     if cointossresult == 1:
  80.         possession = hometeam
  81.         halftimepossession = awayteam
  82.     else:
  83.         possession = awayteam
  84.         halftimepossession = hometeam
  85.     #print('cointoss winner', possession)
  86.  
  87. def halftime():
  88.     global possession
  89.     global halftimepossession
  90.     global scores
  91.     global ballposition
  92.     if ballposition >= 50:
  93.             fieldgoalodds = ballposition + random.randint(25, 50)
  94.             if fieldgoalodds >= 95:
  95.                 scores[possession] += 3
  96.                 #print('fieldgoal')
  97.     possession = halftimepossession
  98.     #print('halftime')
  99.     kickoff()
  100.  
  101. def kickoff():
  102.     global ballposition
  103.     global gametime
  104.     global possession
  105.     global down
  106.     ballposition = 0
  107.     returnteam = possession
  108.     kickofftd = random.randint(1, 100)
  109.     fumblechance = random.randint(1, 1000)
  110.     gametime -= playtime
  111.     if kickofftd <= 1:
  112.         scores[returnteam] += 6
  113.         turnover()
  114.         kickoff()
  115.     else:
  116.         ballposition += 25
  117.         down = 1
  118.         kickoffbigplay = random.randint(1, 100)
  119.         if kickoffbigplay <= 4:
  120.             ballposition += random.randint(1, 25)
  121.             #print('Kickoff big play!', kickoffbigplay)
  122.         if fumblechance <= 5:
  123.             turnover()
  124.             #print('Fumble')
  125.             #print('possession', possession)
  126.             #print('ballposition', ballposition)
  127.             #print('gametime', gametime)
  128.     #print('ballposition', ballposition)
  129.     #print('gametime', gametime)
  130.  
  131. def chooseplay():
  132.     global possession
  133.     global down
  134.     global playchoice
  135.     global ballposition
  136.     offense = possession
  137.     if down < 4:
  138.         playchoice = random.randint(1, 100)
  139.         if playchoice >= 55:
  140.             playchoice = 'passplay'
  141.         else: playchoice = 'runplay'
  142.     else:
  143.         if ballposition > 60:
  144.             playchoice = 'fieldgoalplay'
  145.         else:
  146.             playchoice = 'puntplay'
  147.     #print(playchoice)
  148.  
  149. def passplayexecute():
  150.     global possession
  151.     global ballposition
  152.     global down
  153.     global seriesyards
  154.     global totalpassyards
  155.     global totalpassplays
  156.     totalpassplays += 1
  157.     completionchace = random.randint(1,100)
  158.     if completionchace <= 66:
  159.         #print('Pass complete')
  160.         down += 1
  161.         playyards = 11 + random.randint(-11, 13)
  162.         totalpassyards += playyards
  163.         #print('playyards', playyards)
  164.         ballposition += playyards
  165.         seriesyards += playyards
  166.         if ballposition >= 100:
  167.             scores[possession] += 7
  168.             #print(' passing touchdown')
  169.             #print(scores)
  170.             touchdown = 'yes'
  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('incomplete')
  181.  
  182. def runplayexecute():
  183.     global possession
  184.     global ballposition
  185.     global down
  186.     global seriesyards
  187.     global touchdown
  188.     global totalrunplays
  189.     global totalrushyards
  190.     totalrunplays += 1
  191.     rungainchance = random.randint(1,100)
  192.     if rungainchance <= 70:
  193.         #print('Positive Run')
  194.         down += 1
  195.         playyards = int(4.2 + (random.randint(-30, 100) / 10))
  196.         #print('playyards', playyards)
  197.         ballposition += playyards
  198.         seriesyards += playyards
  199.         if ballposition >= 100:
  200.             scores[possession] += 7
  201.             #print('rushing touchdown')
  202.             #print(scores)
  203.             turnover()
  204.             kickoff()
  205.         if seriesyards >= 10:
  206.             down = 1
  207.             #print('first down', down)
  208.             seriesyards = 0
  209.             #print('ballposition', ballposition)
  210.     else:
  211.         down += 1
  212.         #print('negtive run')
  213.         playyards = int(random.randint(-3, 0))
  214.         #print('playyards', playyards)
  215.         ballposition += playyards
  216.         seriesyards += playyards
  217.     totalrushyards += playyards
  218.  
  219. def puntplayexecute():
  220.     global ballposition
  221.     global possession
  222.     global ballposition
  223.     global down
  224.     global totalpuntplays
  225.     global totalpuntyards
  226.     totalpuntplays += 1
  227.     puntyards = 45 + random.randint(-20, 20)
  228.     totalpuntyards += puntyards
  229.     #print('puntyards', puntyards)
  230.     ballposition += puntyards
  231.     if ballposition >= 100:
  232.         ballposition = 80
  233.     turnover()
  234.  
  235. def fieldgoalplayexecute():
  236.     global scores
  237.     global ballposition
  238.     global totalfieldgoalplays
  239.     totalfieldgoalplays += 1
  240.     fieldgoalodds = ballposition + random.randint(25, 50)
  241.     if fieldgoalodds >= 95:
  242.         scores[possession] += 3
  243.         #print('fieldgoal', scores)
  244.         turnover()
  245.         kickoff()
  246.     else:
  247.         turnover()
  248.  
  249. def turnover():
  250.     global down
  251.     global ballposition
  252.     global possession
  253.     global seriesyards
  254.     down = 1
  255.     ballposition = abs(ballposition - 100)
  256.     if possession == hometeam:
  257.         possession = awayteam
  258.     else:
  259.         possession = hometeam
  260.     seriesyards = 0
  261.     #print('possession', possession)
  262.     #print('ballposition', ballposition)
  263.     #print('gametime', gametime)
  264.  
  265. def executeplay():
  266.     global playchoice
  267.     global gametime
  268.     gametime -= playtime
  269.     if playchoice == 'passplay':
  270.         passplayexecute()
  271.     if playchoice == 'runplay':
  272.         runplayexecute()
  273.     if playchoice == 'fieldgoalplay':
  274.         fieldgoalplayexecute()
  275.     if playchoice == 'puntplay':
  276.         puntplayexecute()
  277.  
  278. def game():
  279.     cointoss()
  280.     kickoff()
  281.     while gametime >= 30:
  282.         chooseplay()
  283.         executeplay()
  284.     halftime()
  285.     while gametime >= 0:
  286.         chooseplay()
  287.         executeplay()
  288.     print('scores: ', scores)
  289.     print('gametime Over')
  290.     global wins
  291.     winner = max(scores.keys(), key=(lambda k: scores[k]))
  292.     print('winner :', winner)
  293.     global totalscores
  294.     hometeamscores = scores[hometeam]
  295.     awayteamscores = scores[awayteam]
  296.     totalscores[hometeam] += hometeamscores
  297.     totalscores[awayteam] += awayteamscores
  298.     print(totalscores)
  299.     print('Yards per attempt: ', (totalpassyards / totalpassplays))
  300.     print('Yards per Carry: ', (totalrushyards / totalrunplays))
  301.     print('yards per punt: ', (totalpuntyards / totalpuntplays))
  302.     print('Average scores :', (totalscores[hometeam] / 1), (totalscores[awayteam] / 1))
  303.  
  304.  
  305.  
  306.  
  307. for i in range(1):
  308.     game()
  309.     end = timer()
  310.     print('time: ', (end - start) * 10)
  311. teamstats()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement