Advertisement
dougbaker45

football_0.7

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