Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from timeit import default_timer as timer
- global scores
- start = timer()
- totalplays = 150
- gametime = 60
- playtime = .4
- hometeam = 'New England Patriots'
- awayteam = 'Los Angeles Rams'
- possession = None
- ballposition = 0
- scores = {hometeam:0, awayteam:0}
- totalplays = {}
- down = 0
- seriesyards = 0
- touchdown = 'No'
- awayteamscores = 0
- hometeamscores = 0
- totalscores = {hometeam:0, awayteam:0}
- totalrunplays = 0
- totalpassplays = 0
- totalpuntplays = 0
- totalfieldgoalplays = 0
- totalrushyards = 0
- totalpassyards = 0
- totalpuntyards = 0
- hometeammod = {}
- def teamstats():
- global possession
- import csv
- import pandas as pd
- if possession == hometeam:
- nonpossession = awayteam
- else: nonpossession = hometeam
- with open('offense.csv', newline='') as csvfile:
- offenses = csv.DictReader(csvfile)
- for row in offenses:
- #print(row)
- if row['Tm'] == hometeam:
- hometeamoffense = row
- if row['Tm'] == awayteam:
- awayteamoffense = row
- #for i in row:
- print(hometeamoffense)
- print(awayteamoffense)
- with open('defense.csv', newline='') as csvfile:
- defenses = csv.DictReader(csvfile)
- for row in defenses:
- #print(row)
- if row['Tm'] == hometeam:
- hometeamdefense = row
- if row['Tm'] == awayteam:
- awayteamdefense = row
- print(hometeamdefense)
- print(awayteamdefense)
- #with open('defense.csv', newline='') as csvfile:
- # defenses = csv.DictReader(csvfile)
- # for row in defenses:
- # print(row)
- #ypc = (offenses['Rushing Y/A'])
- #print(ypc)
- def cointoss():
- global gametime
- global possession
- global halftimepossession
- global scores
- gametime = 60
- scores = {hometeam:0, awayteam:0}
- cointossresult = random.randint(1, 2)
- if cointossresult == 1:
- possession = hometeam
- halftimepossession = awayteam
- else:
- possession = awayteam
- halftimepossession = hometeam
- #print('cointoss winner', possession)
- def halftime():
- global possession
- global halftimepossession
- global scores
- global ballposition
- if ballposition >= 50:
- fieldgoalodds = ballposition + random.randint(25, 50)
- if fieldgoalodds >= 95:
- scores[possession] += 3
- #print('fieldgoal')
- possession = halftimepossession
- #print('halftime')
- kickoff()
- def kickoff():
- global ballposition
- global gametime
- global possession
- global down
- ballposition = 0
- returnteam = possession
- kickofftd = random.randint(1, 100)
- fumblechance = random.randint(1, 1000)
- gametime -= playtime
- if kickofftd <= 1:
- scores[returnteam] += 6
- turnover()
- kickoff()
- else:
- ballposition += 25
- down = 1
- kickoffbigplay = random.randint(1, 100)
- if kickoffbigplay <= 4:
- ballposition += random.randint(1, 25)
- #print('Kickoff big play!', kickoffbigplay)
- if fumblechance <= 5:
- turnover()
- #print('Fumble')
- #print('possession', possession)
- #print('ballposition', ballposition)
- #print('gametime', gametime)
- #print('ballposition', ballposition)
- #print('gametime', gametime)
- def chooseplay():
- global possession
- global down
- global playchoice
- global ballposition
- offense = possession
- if down < 4:
- playchoice = random.randint(1, 100)
- if playchoice >= 55:
- playchoice = 'passplay'
- else: playchoice = 'runplay'
- else:
- if ballposition > 60:
- playchoice = 'fieldgoalplay'
- else:
- playchoice = 'puntplay'
- #print(playchoice)
- def passplayexecute():
- global possession
- global ballposition
- global down
- global seriesyards
- global totalpassyards
- global totalpassplays
- totalpassplays += 1
- completionchace = random.randint(1,100)
- if completionchace <= 66:
- #print('Pass complete')
- down += 1
- playyards = 11 + random.randint(-11, 13)
- totalpassyards += playyards
- #print('playyards', playyards)
- ballposition += playyards
- seriesyards += playyards
- if ballposition >= 100:
- scores[possession] += 7
- #print(' passing touchdown')
- #print(scores)
- touchdown = 'yes'
- turnover()
- kickoff()
- if seriesyards >= 10:
- down = 1
- #print('first down', down)
- seriesyards = 0
- #print('ballposition', ballposition)
- else:
- down += 1
- #print('incomplete')
- def runplayexecute():
- global possession
- global ballposition
- global down
- global seriesyards
- global touchdown
- global totalrunplays
- global totalrushyards
- totalrunplays += 1
- rungainchance = random.randint(1,100)
- if rungainchance <= 70:
- #print('Positive Run')
- down += 1
- playyards = int(4.2 + (random.randint(-30, 100) / 10))
- #print('playyards', playyards)
- ballposition += playyards
- seriesyards += playyards
- if ballposition >= 100:
- scores[possession] += 7
- #print('rushing touchdown')
- #print(scores)
- turnover()
- kickoff()
- if seriesyards >= 10:
- down = 1
- #print('first down', down)
- seriesyards = 0
- #print('ballposition', ballposition)
- else:
- down += 1
- #print('negtive run')
- playyards = int(random.randint(-3, 0))
- #print('playyards', playyards)
- ballposition += playyards
- seriesyards += playyards
- totalrushyards += playyards
- def puntplayexecute():
- global ballposition
- global possession
- global ballposition
- global down
- global totalpuntplays
- global totalpuntyards
- totalpuntplays += 1
- puntyards = 45 + random.randint(-20, 20)
- totalpuntyards += puntyards
- #print('puntyards', puntyards)
- ballposition += puntyards
- if ballposition >= 100:
- ballposition = 80
- turnover()
- def fieldgoalplayexecute():
- global scores
- global ballposition
- global totalfieldgoalplays
- totalfieldgoalplays += 1
- fieldgoalodds = ballposition + random.randint(25, 50)
- if fieldgoalodds >= 95:
- scores[possession] += 3
- #print('fieldgoal', scores)
- turnover()
- kickoff()
- else:
- turnover()
- def turnover():
- global down
- global ballposition
- global possession
- global seriesyards
- down = 1
- ballposition = abs(ballposition - 100)
- if possession == hometeam:
- possession = awayteam
- else:
- possession = hometeam
- seriesyards = 0
- #print('possession', possession)
- #print('ballposition', ballposition)
- #print('gametime', gametime)
- def executeplay():
- global playchoice
- global gametime
- gametime -= playtime
- if playchoice == 'passplay':
- passplayexecute()
- if playchoice == 'runplay':
- runplayexecute()
- if playchoice == 'fieldgoalplay':
- fieldgoalplayexecute()
- if playchoice == 'puntplay':
- puntplayexecute()
- def game():
- cointoss()
- kickoff()
- while gametime >= 30:
- chooseplay()
- executeplay()
- halftime()
- while gametime >= 0:
- chooseplay()
- executeplay()
- print('scores: ', scores)
- print('gametime Over')
- global wins
- winner = max(scores.keys(), key=(lambda k: scores[k]))
- print('winner :', winner)
- global totalscores
- hometeamscores = scores[hometeam]
- awayteamscores = scores[awayteam]
- totalscores[hometeam] += hometeamscores
- totalscores[awayteam] += awayteamscores
- print(totalscores)
- print('Yards per attempt: ', (totalpassyards / totalpassplays))
- print('Yards per Carry: ', (totalrushyards / totalrunplays))
- print('yards per punt: ', (totalpuntyards / totalpuntplays))
- print('Average scores :', (totalscores[hometeam] / 1), (totalscores[awayteam] / 1))
- for i in range(1):
- game()
- end = timer()
- print('time: ', (end - start) * 10)
- teamstats()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement