Advertisement
dougbaker45

football_1.16

Sep 5th, 2019
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 28.89 KB | None | 0 0
  1. import random
  2. from timeit import default_timer as timer
  3. import csv
  4. totalstart = timer()
  5.  
  6. finalscores = []
  7. matchcount = 1
  8. gamesinweek = 0
  9. correctcount = 0
  10. totalpenaltyyards = 0
  11. season = '2019'
  12. weekstart = 1
  13. weekend = 1
  14. gamesplayed = 100000
  15. totalcorrectcount = 0
  16. totalgamesinweek = 0
  17.  
  18. f = open("outcomes.csv", "w")
  19. f.truncate()
  20. f.close()
  21.  
  22. def header():
  23.     global start
  24.     start = timer()
  25.     with open('outcomes.csv', 'a', newline='') as csvFile:
  26.         writer = csv.writer(csvFile)
  27.         writer.writerow(['Winning Team', 'Win Percent','Correct', 'Home Team', 'Away Team', 'Home Team Avg Score', 'Away Team Avg Score', 'Home Team Win Count', 'Away Team Win Count'])
  28.  
  29. def statsreset():
  30.     global gamestats
  31.     global gamesplayed
  32.     global offset
  33.     global totalstats
  34.     global totalscores
  35.     global atstats
  36.     global htstats
  37.     offset = (gamesplayed / 100)
  38.     scores = {hometeam:0, awayteam:0}
  39.     totalscores = {hometeam:0, awayteam:0}
  40.     totalstats = {'totalrunplays':0, 'totalpassplays':0, 'totalpuntplays':0, 'totalfieldgoalplays':0, 'totalrushyards':0, 'totalpassyards':0, 'totalpuntyards':0, 'totalcompletions':0, 'cointosswinner '+str(hometeam):0, 'cointosswinner '+str(awayteam):0}
  41.     htstats = {'totalrunplays':0, 'totalpassplays':0, 'totalpuntplays':0, 'totalfieldgoalplays':0, 'totalrushyards':0, 'totalpassyards':0, 'totalpuntyards':0, 'totalcompletions':0, 'cointosswinner '+str(hometeam):0}
  42.     atstats = {'totalrunplays':0, 'totalpassplays':0, 'totalpuntplays':0, 'totalfieldgoalplays':0, 'totalrushyards':0, 'totalpassyards':0, 'totalpuntyards':0, 'totalcompletions':0, 'cointosswinner '+str(awayteam):0}
  43.     gamestats = {'games':0, 'gametime':60, 'possession':None, 'halftimepossession':0, 'ballposition':0, 'down':0, 'seriesyards':0, 'htwincount':0, 'atwincount':0}
  44.  
  45. def teamstats():
  46.     global htoffense
  47.     global atoffense
  48.     global htdefense
  49.     global atdefense
  50.     global htKicking_Punting
  51.     global atKicking_Punting
  52.     global htreturning
  53.     global atreturning
  54.     global gamesinweek
  55.  
  56.     with open('offense.csv', newline='') as csvfile:
  57.          offenses = csv.DictReader(csvfile)
  58.          for row in offenses:
  59.              if row['Tm'] == hometeam:
  60.                 htoffense = row
  61.              if row['Tm'] == awayteam:
  62.                 atoffense = row
  63.  
  64.     with open('defense.csv', newline='') as csvfile:
  65.          defenses = csv.DictReader(csvfile)
  66.          for row in defenses:
  67.              if row['Tm'] == hometeam:
  68.                  htdefense = row
  69.              if row['Tm'] == awayteam:
  70.                  atdefense = row
  71.  
  72.     with open('Kicking_Punting.csv', newline='') as csvfile:
  73.          Kicking_Punting = csv.DictReader(csvfile)
  74.          for row in Kicking_Punting:
  75.              if row['Tm'] == hometeam:
  76.                  htKicking_Punting = row
  77.              if row['Tm'] == awayteam:
  78.                  atKicking_Punting = row
  79.  
  80.     with open('returning.csv', newline='') as csvfile:
  81.          returning = csv.DictReader(csvfile)
  82.          for row in returning:
  83.              if row['Tm'] == hometeam:
  84.                  htreturning = row
  85.              if row['Tm'] == awayteam:
  86.                  atreturning = row
  87.  
  88.  
  89. def cointoss():
  90.     global gamestats
  91.     global scores
  92.     #print('cointoss')
  93.     gamestats['gametime'] = 60
  94.     scores = {hometeam:0, awayteam:0}
  95.     cointossresult = random.randint(0, 2)
  96.     if cointossresult == 1:
  97.         gamestats['possession'] = hometeam
  98.         gamestats['halftimepossession'] = awayteam
  99.         totalstats['cointosswinner '+str(awayteam)] += 1
  100.         atstats['cointosswinner '+str(awayteam)] += 1
  101.     else:
  102.         gamestats['possession'] = awayteam
  103.         gamestats['halftimepossession'] = hometeam
  104.         totalstats['cointosswinner '+str(hometeam)] += 1
  105.         htstats['cointosswinner '+str(hometeam)] += 1
  106.     #print(gamestats['halftimepossession'], ' chooses to defer, ', gamestats['possession'], 'will receieve')
  107.  
  108. def halftime():
  109.     global gamestats
  110.     global scores
  111.     #print('Halftime!')
  112.     if gamestats['ballposition'] >= 4000:
  113.         fieldgoalplayexecute()
  114.     gamestats['possession'] = gamestats['halftimepossession']
  115.     #print(gamestats['halftimepossession'], ' chooses to defer and will receieve')
  116.     kickoff()
  117.  
  118. def endgame():
  119.     global gamestats
  120.     global scores
  121.  
  122.     if gamestats['ballposition'] >= 4000:
  123.         #print('Final play of the game!')
  124.         fieldgoalplayexecute()
  125.     gamestats['possession'] = gamestats['halftimepossession']
  126.  
  127.  
  128. def kickoff():
  129.     global gamestats
  130.     gamestats['ballposition'] = 0
  131.     returnteam = gamestats['possession']
  132.     kickofftd = random.randint(0, 100)
  133.     gamestats['gametime'] -= random.uniform(.2, .6)
  134.     if kickofftd <= 1:
  135.         scores[returnteam] += 6
  136.         #print('Kickoff td!')
  137.         turnover()
  138.         extrapointexecute()
  139.         kickoff()
  140.     else:
  141.         if gamestats['possession'] == hometeam:
  142.             gamestats['ballposition'] = float(htreturning['Kick Returns Y/Rt']) * 95
  143.         if gamestats['possession'] == awayteam:
  144.             gamestats['ballposition'] = float(atreturning['Kick Returns Y/Rt']) * 95
  145.         gamestats['down'] = 1
  146.         kickoffbigplay = random.randint(0, 100)
  147.         if kickoffbigplay <= 5:
  148.             gamestats['ballposition'] += random.randint(1000, 2500)
  149.     #print('Kickoff returned to ', int((gamestats['ballposition'] / 100)))
  150.  
  151. def chooseplay():
  152.     global gamestats
  153.     global playchoice
  154.     offense = gamestats['possession']
  155.  
  156.     if gamestats['possession'] == hometeam:
  157.         if gamestats['down'] < 4:
  158.             playodds = random.randint(1, 100)
  159.             #print('playodds', playodds, 'play num', int((100 * (int(htoffense['Rushing Att']) / int(htoffense['Tot Ply'])))))
  160.             if playodds >= (int((100 * (int(htoffense['Rushing Att']) / int(htoffense['Tot Ply']))))):
  161.                 playchoice = 'passplay'
  162.             else:
  163.                 playchoice = 'runplay'
  164.         else:
  165.             if gamestats['ballposition'] > 5300:
  166.                 playchoice = 'fieldgoalplay'
  167.             else:
  168.                 playchoice = 'puntplay'
  169.     if gamestats['possession'] == awayteam:
  170.         if gamestats['down'] < 4:
  171.             playodds = random.randint(1, 100)
  172.             if playodds >= (int((100 * (int(atoffense['Rushing Att']) / int(atoffense['Tot Ply']))))):
  173.                 playchoice = 'passplay'
  174.             else:
  175.                 playchoice = 'runplay'
  176.         else:
  177.             if gamestats['ballposition'] > 5300:
  178.                 playchoice = 'fieldgoalplay'
  179.             else:
  180.                 playchoice = 'puntplay'
  181.  
  182. def passplayexecute():
  183.     global gamestats
  184.     global totalstats
  185.     global htstats
  186.     global atstats
  187.     totalstats['totalpassplays'] += 1
  188.     completionchace = random.randint(1,100)
  189.     if gamestats['possession'] == hometeam:
  190.         htstats['totalpassplays'] += 1
  191.         if completionchace <= int(100 * (((int(htoffense['Passing Cmp']) / int(htoffense['Passing Att']))  + (int(atdefense['Passing Cmp']) / int(atdefense['Passing Att']))) / 2)):
  192.             passcomplete = 'complete'
  193.         else:
  194.             passcomplete = 'incomplete'
  195.     if gamestats['possession'] == awayteam:
  196.         atstats['totalpassplays'] += 1
  197.         if completionchace <= int(100 * (((int(atoffense['Passing Cmp']) / int(atoffense['Passing Att']))  + (int(htdefense['Passing Cmp']) / int(htdefense['Passing Att']))) / 2)):
  198.             passcomplete = 'complete'
  199.         else:
  200.             passcomplete = 'incomplete'
  201.     if passcomplete == 'complete':
  202.         totalstats['totalcompletions'] += 1
  203.         gamestats['down'] += 1
  204.         if gamestats['possession'] == hometeam:
  205.             htstats['totalcompletions'] += 1
  206.             passmod = (100 * ((int(htoffense['Passing Yds']) / int(htoffense['Passing Cmp'])) + (int(atdefense['Passing Yds']) / int(atdefense['Passing Cmp']))) / 2)
  207.             passyards = passmod + random.uniform(-(passmod + 200), passmod + 200)
  208.             htstats['totalpassyards'] += passyards
  209.         if gamestats['possession'] == awayteam:
  210.             atstats['totalcompletions'] += 1
  211.             passmod = (100 * ((int(atoffense['Passing Yds']) / int(atoffense['Passing Cmp'])) + (int(htdefense['Passing Yds']) / int(htdefense['Passing Cmp']))) / 2)
  212.             passyards = passmod + random.uniform(-(passmod + 200), passmod + 200)
  213.             atstats['totalpassyards'] += passyards
  214.         totalstats['totalpassyards'] += passyards
  215.         gamestats['ballposition'] += passyards
  216.         gamestats['seriesyards'] += passyards
  217.         #print('Pass complete for', round((passyards / 100), 1), ' yards. Ball is at', int((gamestats['ballposition'] / 100)), 'Down is ', gamestats['down'])
  218.         if gamestats['ballposition'] >= 10000:
  219.             scores[gamestats['possession']] += 6
  220.             #print("Passing TD", scores)
  221.             extrapointexecute()
  222.             touchdown = 'yes'
  223.             turnover()
  224.             kickoff()
  225.         if gamestats['seriesyards'] >= 1000:
  226.             gamestats['down'] = 1
  227.             gamestats['seriesyards'] = 0
  228.             #print('First Down')
  229.     if passcomplete == 'incomplete':
  230.         gamestats['down'] += 1
  231.         #print('Pass incomeplete, down ', gamestats['down'])
  232.  
  233. def runplayexecute():
  234.     global gamestats
  235.     global totalstats
  236.     global htstats
  237.     global atstats
  238.     totalstats['totalrunplays'] += 1
  239.     if gamestats['possession'] == hometeam:
  240.         nonpossession = awayteam
  241.         htstats['totalrunplays'] += 1
  242.         runmod = ((100 * float(htoffense['Rushing YPC'])) + (100 * float(atdefense['Rushing YPC']))) / 2
  243.         runresult = runmod + random.uniform(-(runmod + 300), (runmod + 300))
  244.         htstats['totalrushyards'] += runresult
  245.     if gamestats['possession'] == awayteam:
  246.         nonpossession = hometeam
  247.         atstats['totalrunplays'] += 1
  248.         runmod = ((100 * float(htoffense['Rushing YPC'])) + (100 * float(atdefense['Rushing YPC']))) / 2
  249.         runresult = runmod + random.uniform(-(runmod + 300), (runmod + 300))
  250.         atstats['totalrushyards'] += runresult
  251.     gamestats['down'] += 1
  252.     gamestats['ballposition'] += runresult
  253.     gamestats['seriesyards'] += runresult
  254.     atstats['totalrushyards'] += 1
  255.     #print('Rush for', round((runresult / 100), 1), ' yards. Ball is at', int((gamestats['ballposition'] / 100)), 'Down is ', gamestats['down'])
  256.     if gamestats['ballposition'] >= 10000:
  257.         scores[gamestats['possession']] += 6
  258.         extrapointexecute()
  259.         turnover()
  260.         kickoff()
  261.     if gamestats['seriesyards'] >= 1000:
  262.         gamestats['down'] = 1
  263.         gamestats['seriesyards'] = 0
  264.         #print('First Down!')
  265.     totalstats['totalrushyards'] += runresult
  266.     if gamestats['ballposition'] <= 0:
  267.         scores[nonpossession] += 2
  268.         #print("Safety", scores)
  269.         puntplayexecute()
  270.  
  271. def puntplayexecute():
  272.     global gamestats
  273.     puntteam = gamestats['possession']
  274.     if puntteam == hometeam:
  275.         returnteam = awayteam
  276.     else: returnteam = hometeam
  277.  
  278.     punttd = random.randint(0, 1000)
  279.     if punttd <= 8:
  280.         scores[returnteam] += 6
  281.         #print('Punt TD')
  282.         turnover()
  283.         extrapointexecute()
  284.         kickoff()
  285.     else:
  286.         if puntteam == hometeam:
  287.             #print(gamestats['ballposition'], 'Starting  position')
  288.             gamestats['ballposition'] += ((float(htKicking_Punting['Punting Y/P']) * 100) /3) + random.uniform(-((float(htKicking_Punting['Punting Y/P']) * 100) /2), (float(htKicking_Punting['Punting Y/P']) * 100))
  289.             #print(gamestats['ballposition'], 'punt')
  290.             if gamestats['ballposition'] >= 10000:
  291.                 gamestats['ballposition'] = 8000
  292.             else:
  293.                 gamestats['ballposition'] -= (float(atreturning['Punt Returns Y/R']) + random.uniform(-(float(atreturning['Punt Returns Y/R'])), float(atreturning['Punt Returns Y/R'])) * 100)
  294.                 #print(gamestats['ballposition'], 'return')
  295.         if puntteam == awayteam:
  296.             #print(gamestats['ballposition'], 'starting')
  297.             gamestats['ballposition'] += ((float(atKicking_Punting['Punting Y/P']) * 100) /2) + random.uniform(-((float(atKicking_Punting['Punting Y/P']) * 100) /2), (float(atKicking_Punting['Punting Y/P']) * 100))
  298.             #print(gamestats['ballposition'], 'punt')
  299.             if gamestats['ballposition'] >= 10000:
  300.                 gamestats['ballposition'] = 8000
  301.             else:
  302.                 gamestats['ballposition'] -= (float(htreturning['Punt Returns Y/R']) + random.uniform(-(float(htreturning['Punt Returns Y/R'])), float(htreturning['Punt Returns Y/R'])) * 100)
  303.                 #print(gamestats['ballposition'], 'return')
  304.         #print(gamestats['ballposition'], 'before turnover')
  305.         turnover()
  306.         #print(gamestats['ballposition'], 'final position')
  307.         gamestats['down'] = 1
  308.  
  309.  
  310. def fieldgoalplayexecute():
  311.     global scores
  312.     global totalfieldgoalplays
  313.     global gamestats
  314.     gamestats['ballposition'] = int(gamestats['ballposition'])
  315.     totalstats['totalfieldgoalplays'] += 1
  316.     if 4000 <= gamestats['ballposition'] <= 4999:
  317.         if gamestats['possession'] == hometeam:
  318.             fgmod = ((float(htKicking_Punting['50 FGM']) / float(htKicking_Punting['50 FGA'])) * 100)
  319.         if gamestats['possession'] == awayteam:
  320.             fgmod = ((float(htKicking_Punting['50 FGM']) / float(htKicking_Punting['50 FGA'])) * 100)
  321.     if 5000 <= gamestats['ballposition'] <= 5999:
  322.         if gamestats['possession'] == hometeam:
  323.             fgmod = ((float(htKicking_Punting['40-49 FGM']) / float(htKicking_Punting['40-49 FGA'])) * 100)
  324.         if gamestats['possession'] == awayteam:
  325.             fgmod = ((float(htKicking_Punting['40-49 FGM']) / float(htKicking_Punting['40-49 FGA'])) * 100)
  326.     if 6000 <= gamestats['ballposition'] <= 6999:
  327.         if gamestats['possession'] == hometeam:
  328.             fgmod =((float(htKicking_Punting['30-39 FGM']) / float(htKicking_Punting['30-39 FGA'])) * 100)
  329.         if gamestats['possession'] == awayteam:
  330.             fgmod = ((float(htKicking_Punting['30-39 FGM']) / float(htKicking_Punting['30-39 FGA'])) * 100)
  331.     if 7000 <= gamestats['ballposition'] <= 7999:
  332.         if gamestats['possession'] == hometeam:
  333.             fgmod = (float(htKicking_Punting['20-29 FGM']) / float(htKicking_Punting['20-29 FGA'])) * 100
  334.         if gamestats['possession'] == awayteam:
  335.             fgmod = (float(htKicking_Punting['20-29 FGM']) / float(htKicking_Punting['20-29 FGA'])) * 100
  336.     if gamestats['ballposition'] >= 8000:
  337.         fgmod = 99
  338.     fgchance = random.randint(0, 100)
  339.     if fgchance <= fgmod:
  340.         scores[gamestats['possession']] += 3
  341.         turnover()
  342.         kickoff()
  343.     else:
  344.         turnover()
  345.  
  346. def extrapointexecute():
  347.     global scores
  348.     if gamestats['possession'] == hometeam:
  349.         xpmod = float(htKicking_Punting['Scoring XP%'])
  350.     if gamestats['possession'] == awayteam:
  351.         xpmod = float(atKicking_Punting['Scoring XP%'])
  352.     xpchance = random.randint(0, 100)
  353.     #print('xp', xpmod, xpchance)
  354.     if xpchance <= xpmod:
  355.         scores[gamestats['possession']] += 1
  356.  
  357. def interceptioncheck():
  358.     global gamestats
  359.     global hometeam
  360.     global awayteam
  361.     global playchoice
  362.     interceptionchance = random.randint(0, 100)
  363.     if gamestats['possession'] == hometeam:
  364.         interceptionodds = (((float(htoffense['Int']) / float(htoffense['Passing Att'])) + (float(atdefense['Passing Int']) / float(atdefense['Passing Att'])) / 2) * 100)
  365.         nonpossession = awayteam
  366.     if gamestats['possession'] == awayteam:
  367.         interceptionodds = (((float(atoffense['Int']) / float(atoffense['Passing Att'])) + (float(htdefense['Passing Int']) / float(htdefense['Passing Att'])) / 2) * 100)
  368.         nonpossession = hometeam
  369.     if interceptionodds >= interceptionchance:
  370.         playchoice = 'interception happened'
  371.         pick6chance = random.randint(0, 100)
  372.         if pick6chance >= 99:
  373.             scores[nonpossession] += 6
  374.             turnover()
  375.             extrapointexecute()
  376.             turnover()
  377.             kickoff()
  378.         else:
  379.             turnover()
  380.  
  381. def fumblecheck():
  382.     global gamestats
  383.     global hometeam
  384.     global awayteam
  385.     global playchoice
  386.     fumblechance = random.randint(0, 100)
  387.     if gamestats['possession'] == hometeam:
  388.         fumbleodds = (((float(htoffense['FL']) / float(htoffense['Rushing Att'])) + (float(atdefense['FL']) / float(atdefense['Rushing  Att'])) / 2) * 100)
  389.         nonpossession = awayteam
  390.     if gamestats['possession'] == awayteam:
  391.         fumbleodds = (((float(atoffense['FL']) / float(atoffense['Rushing Att'])) + (float(htdefense['FL']) / float(htdefense['Rushing  Att'])) / 2) * 100)
  392.         nonpossession = hometeam
  393.     if fumbleodds >= fumblechance:
  394.         playchoice = 'fumble happened'
  395.         fumble6chance = random.randint(0, 100)
  396.         if fumble6chance >= 99:
  397.             scores[nonpossession] += 6
  398.             turnover()
  399.             extrapointexecute()
  400.             kickoff()
  401.         else:
  402.             turnover()
  403.  
  404. def penaltycheck():
  405.     global gamestats
  406.     global totalpenaltyyards
  407.     global scores
  408.     global playchoice
  409.     offensepenaltychance = random.randint(0, 100)
  410.     defensepenaltychance = random.randint(0, 100)
  411.  
  412.     if gamestats['possession'] == hometeam:
  413.         offensepenaltyodds = ((float(htoffense['Pen Ct']) / float(htoffense['Tot Ply'])) * 100)
  414.         offensepenaltyyards = ((float(htoffense['Pen Yds']) / float(htoffense['Pen Ct'])) * 100)
  415.         defensepenaltyodds = ((float(atdefense['Pen Ct']) / float(atdefense['Ply'])) * 100)
  416.         defensepenaltyyards = ((float(atdefense['Pen Yds']) / float(atdefense['Pen Ct'])) * 100)
  417.         if offensepenaltychance <= offensepenaltyodds:
  418.             gamestats['gametime'] += random.uniform(.2, .6)
  419.             gamestats['ballposition'] -= offensepenaltyyards
  420.             gamestats['seriesyards'] -= offensepenaltyyards
  421.             totalpenaltyyards += offensepenaltyyards
  422.             playchoice = 'penaltyhappened'
  423.         if defensepenaltychance <= defensepenaltyodds:
  424.             gamestats['gametime'] += random.uniform(.2, .6)
  425.             gamestats['ballposition'] += offensepenaltyyards
  426.             gamestats['seriesyards'] += offensepenaltyyards
  427.             totalpenaltyyards += offensepenaltyyards
  428.             playchoice = 'penaltyhappened'
  429.  
  430.     if gamestats['possession'] == awayteam:
  431.         offensepenaltyodds = ((float(atoffense['Pen Ct']) / float(atoffense['Tot Ply'])) * 100)
  432.         offensepenaltyyards = ((float(atoffense['Pen Yds']) / float(atoffense['Pen Ct'])) * 100)
  433.         defensepenaltyodds = ((float(htdefense['Pen Ct']) / float(htdefense['Ply'])) * 100)
  434.         defensepenaltyyards = ((float(htdefense['Pen Yds']) / float(htdefense['Pen Ct'])) * 100)
  435.         if offensepenaltychance <= offensepenaltyodds:
  436.             gamestats['gametime'] += random.uniform(.2, .6)
  437.             gamestats['ballposition'] -= offensepenaltyyards
  438.             gamestats['seriesyards'] -= offensepenaltyyards
  439.             totalpenaltyyards += offensepenaltyyards
  440.             playchoice = 'penaltyhappened'
  441.         if defensepenaltychance <= defensepenaltyodds:
  442.             gamestats['gametime'] += random.uniform(.2, .6)
  443.             gamestats['ballposition'] += offensepenaltyyards
  444.             gamestats['seriesyards'] += offensepenaltyyards
  445.             totalpenaltyyards += offensepenaltyyards
  446.             playchoice = 'penaltyhappened'
  447.  
  448.     if gamestats['seriesyards'] >= 1000:
  449.         gamestats['down'] = 1
  450.         gamestats['seriesyards'] = 0
  451.  
  452. def sackcheck():
  453.     global playchoice
  454.     global gamestats
  455.     global scores
  456.     sackchance = random.randint(0, 100)
  457.     if gamestats['possession'] == hometeam:
  458.         nonpossession = awayteam
  459.         sackodds = ((((float(htoffense['Sk']) / float(htoffense['Passing Att'])) * 100) + ((float(atdefense['Sk']) / float(atdefense['Passing Att'])) * 100)) / 2)
  460.         sackyards = ((((float(htoffense['Sk Yds']) / float(htoffense['Sk'])) * 100) + ((float(atdefense['Sk Yds']) / float(atdefense['Sk'])) * 100)) / 2)
  461.     if gamestats['possession'] == awayteam:
  462.         nonpossession = hometeam
  463.         sackodds = ((((float(atoffense['Sk']) / float(atoffense['Passing Att'])) * 100) + ((float(htdefense['Sk']) / float(htdefense['Passing Att'])) * 100)) / 2)
  464.         sackyards = ((((float(atoffense['Sk Yds']) / float(atoffense['Sk'])) * 100) + ((float(htdefense['Sk Yds']) / float(htdefense['Sk'])) * 100)) / 2)
  465.     if sackchance <= sackodds:
  466.         playchoice = 'sackhappened'
  467.         gamestats['ballposition'] -= sackyards
  468.         gamestats['seriesyards'] -= sackyards
  469.         gamestats['down'] += 1
  470.         if gamestats['ballposition'] <= 0:
  471.             scores[nonpossession] += 2
  472.             puntplayexecute()
  473.  
  474. def turnover():
  475.     global gamestats
  476.     gamestats['down'] = 1
  477.     gamestats['ballposition'] = abs(gamestats['ballposition'] - 10000)
  478.     if gamestats['possession'] == hometeam:
  479.         gamestats['possession'] = awayteam
  480.     else:
  481.         gamestats['possession'] = hometeam
  482.     gamestats['seriesyards'] = 0
  483.  
  484. def executeplay():
  485.     global playchoice
  486.     global gamestats
  487.     gamestats['gametime'] -= random.uniform(.2, .6)
  488.     #print('The ball is at the ', int((gamestats['ballposition'] / 100)), 'Time remaining is ', round(gamestats['gametime'], 2))
  489.     #print(gamestats['possession'], ' Playchoice is', playchoice)
  490.     penaltycheck()
  491.     if playchoice == 'runplay':
  492.         fumblecheck()
  493.     if playchoice == 'runplay':
  494.         runplayexecute()
  495.     if playchoice == 'passplay':
  496.         sackcheck()
  497.     if playchoice == 'passplay':
  498.         interceptioncheck()
  499.     if playchoice == 'passplay':
  500.         passplayexecute()
  501.     if playchoice == 'fieldgoalplay':
  502.         fieldgoalplayexecute()
  503.     if playchoice == 'puntplay':
  504.         puntplayexecute()
  505.  
  506. def game():
  507.     cointoss()
  508.     kickoff()
  509.     while gamestats['gametime'] >= 30:
  510.         chooseplay()
  511.         executeplay()
  512.     halftime()
  513.     while gamestats['gametime'] >= 0:
  514.         chooseplay()
  515.         executeplay()
  516.     endgame()
  517.     global wins
  518.     winner = max(scores.keys(), key=(lambda k: scores[k]))
  519.     if winner == hometeam:
  520.         gamestats['htwincount'] += 1
  521.     if winner == awayteam:
  522.         gamestats['atwincount'] += 1
  523.     global totalscores
  524.     totalscores[hometeam] += scores[hometeam]
  525.     totalscores[awayteam] += scores[awayteam]
  526.  
  527. def printstats():
  528.     global totalpassplays
  529.     global totalpassyards
  530.     global totalcompletions
  531.     global end
  532.     global start
  533.     global gamesplayed
  534.     global finalscores
  535.     global matchcount
  536.     global totalscores
  537.     global gamestats
  538.     global realwinner
  539.     global correctcount
  540.     global gamesinweek
  541.     global percentcorrect
  542.     #print('Total Scores: ', totalscores)
  543.     #print('Total Yards per attempt: ', (totalstats['totalpassyards'] / totalstats['totalpassplays']) / 100)
  544.     #print('Total Average yards per completion', (totalstats['totalpassyards'] / totalstats['totalcompletions']) / 100)
  545.     #print('Total per Carry: ', (totalstats['totalrushyards'] / totalstats['totalrunplays']) / 100)
  546.     #print('Total yards per punt: ', (totalstats['totalpuntyards'] / totalstats['totalpuntplays']) / 100)
  547.     htstats['comp percent'] = 100 * (htstats['totalcompletions'] / htstats['totalpassplays'])
  548.     htstats['YP COMP'] = (htstats['totalpassyards'] / htstats['totalcompletions']) / 100
  549.     htstats['YP Carry'] = (htstats['totalrushyards'] / htstats['totalrunplays']) / 100
  550.     atstats['comp percent'] = 100 * (atstats['totalcompletions'] / atstats['totalpassplays'])
  551.     atstats['YP COMP'] = (atstats['totalpassyards'] / atstats['totalcompletions']) / 100
  552.     atstats['YP Carry'] = (atstats['totalrushyards'] / atstats['totalrunplays']) / 100
  553.     #print(hometeam, htstats)
  554.     #print(awayteam, atstats)
  555.     ###print(hometeam,'completion %: ', htstats['comp percent'],' Yards Per completion: ', htstats['YP COMP'],'Yards Per Carry: ', htstats['YP Carry'] )
  556.     ###print(awayteam,'completion %: ', atstats['comp percent'],' Yards Per completion: ', atstats['YP COMP'],'Yards Per Carry: ', atstats['YP Carry'] )
  557.     print('Average scores :', hometeam, (totalscores[hometeam] / gamesplayed), awayteam, (totalscores[awayteam] / gamesplayed))
  558.     end = timer()
  559.     #print('time: ', (end - start) )
  560.     #print('')
  561.     finalscores.append(hometeam + ' : ' + str((totalscores[hometeam] / gamesplayed)) + ' | ' + awayteam + ' : ' + str((totalscores[awayteam] / gamesplayed)))
  562.     print()
  563.     winner = max(totalscores.keys(), key=(lambda k: totalscores[k]))
  564.     if winner == hometeam:
  565.         winpercent = (gamestats['htwincount'] / gamesplayed) * 100
  566.         winpercent = str(round(winpercent, 2)) + '%'
  567.     if winner == awayteam:
  568.         winpercent = (gamestats['atwincount'] / gamesplayed) * 100
  569.         winpercent = str(round(winpercent, 2)) + '%'
  570.     if str(winner) == realwinner:
  571.         prediction = 'Yes'
  572.         correctcount += 1
  573.     if str(winner) != realwinner:
  574.         prediction = 'No'
  575.     percentcorrect = (correctcount / gamesinweek) * 100
  576.     percentcorrect = str(round(percentcorrect, 2)) + '%'
  577.     with open('outcomes.csv', 'a', newline='') as csvFile:
  578.         writer = csv.writer(csvFile)
  579.         writer.writerow([winner, winpercent, prediction, hometeam, awayteam, str((totalscores[hometeam] / gamesplayed)), str((totalscores[awayteam] / gamesplayed)), gamestats['htwincount'], gamestats['atwincount']])
  580.  
  581.  
  582. def pickteams():
  583.     global matchcount
  584.     global hometeam
  585.     global awayteam
  586.     import csv
  587.     global week
  588.     global season
  589.     global realwinner
  590.     with open('schedule.csv', newline='') as csvfile:
  591.          schedule = csv.DictReader(csvfile)
  592.          for row in schedule:
  593.              if row['Season'] == season:
  594.                  if row['Week'] == str(weekstart):
  595.                      if row['Match Number'] == str(matchcount):
  596.                          hometeam = str(row['hometeam'])
  597.                          awayteam = str(row['awayteam'])
  598.                          realwinner = row['Winner']
  599.  
  600.  
  601.  
  602.  
  603. def match():
  604.     global gamestats
  605.     global offset
  606.     global gamesplayed
  607.     global matchcount
  608.     global totalpenaltyyards
  609.     global gamesinweek
  610.     global percentcorrect
  611.     global week
  612.     pickteams()
  613.     teamstats()
  614.     statsreset()
  615.     for i in range(gamesplayed):
  616.         game()
  617.         gamestats['games'] += 1
  618.         if gamestats['games'] >= offset:
  619.             print('Percent complete: ', int(( 100 * (offset / gamesplayed))), ' | Match Number: ', matchcount, ' | Week Number: ', weekstart)
  620.             offset += (gamesplayed / 100)
  621.     matchcount += 1
  622.     printstats()
  623.  
  624. def weekgames():
  625.     global season
  626.     global weekstart
  627.     global numberofgames
  628.     global gamesplayed
  629.     global end
  630.     global gamesinweek
  631.     global percentcorrect
  632.     global matchcount
  633.     header()
  634.     with open('schedule.csv', newline='') as csvfile:
  635.         schedule = csv.DictReader(csvfile)
  636.         for row in schedule:
  637.             if row['Season'] == season:
  638.                 if row['Week'] == str(weekstart):
  639.                      gamesinweek += 1
  640.     for i in range(gamesinweek):
  641.         match()
  642.     print(finalscores)
  643.     print('time: ', (end - start) )
  644.     print('')
  645.     print('')
  646.     print('')
  647.  
  648.     with open('outcomes.csv', 'a', newline='') as csvFile:
  649.         writer = csv.writer(csvFile)
  650.         writer.writerow('')
  651.         writer.writerow(['Correct prediction', 'Correct %', 'Season', 'Week', 'Number of Games in Week', 'Count of Matchups Played', 'Run Time', 'Run Minutes', 'Run Hours'])
  652.         writer.writerow([correctcount, percentcorrect, season, weekstart, gamesinweek, gamesplayed, (end - start), round((end - start) / 60, 2), round((end - start) / 60 / 60, 2)])
  653.         writer.writerow('')
  654.         writer.writerow('')
  655.  
  656. def mainloop():
  657.     global weekstart
  658.     global weekend
  659.     global totalcorrectcount
  660.     global correctcount
  661.     global matchcount
  662.     global totalgamesinweek
  663.     global gamesinweek
  664.     global totalpercentcorrect
  665.  
  666.     while weekstart <= weekend:
  667.         weekgames()
  668.         weekstart += 1
  669.         totalcorrectcount += correctcount
  670.         matchcount = 1
  671.         totalgamesinweek += gamesinweek
  672.         gamesinweek = 0
  673.         correctcount = 0
  674.         totalpercentcorrect = (totalcorrectcount / totalgamesinweek) * 100
  675.         totalpercentcorrect = str(round(totalpercentcorrect, 2)) + '%'
  676.  
  677.  
  678. def totalstatsprint():
  679.     global totalcorrectcount
  680.     global totalpercentcorrect
  681.     global totalgamesinweek
  682.     global end
  683.     global totalstart
  684.     with open('outcomes.csv', 'a', newline='') as csvFile:
  685.         writer = csv.writer(csvFile)
  686.         writer.writerow('')
  687.         writer.writerow(['Total Correct prediction', 'Total Correct %', ' Total Count of Games Played', 'Total Run Time', 'Total Run Minutes', 'Total Run Hours'])
  688.         writer.writerow([totalcorrectcount, totalpercentcorrect, totalgamesinweek, (end - totalstart), round((end - totalstart) / 60, 2), round((end - totalstart) / 60 / 60, 2)])
  689.         writer.writerow('')
  690.         writer.writerow('')
  691.  
  692. mainloop()
  693. totalstatsprint()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement