Advertisement
dougbaker45

football

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