-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
531 lines (488 loc) · 20.5 KB
/
sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
from read_csv import *
import random
#Player,Tm,Cmp,Att,CmpPct,Yds,TD,TDPct,Inter,InterPct,SuccPct,YPA,AYPA,YPC,YPG,Rate,QBR,Sk,SkYds,SkPct,NYPA,ANYPA
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
def play_game(home,away):
#Initializing variables for stats and team array
home_team = []
away_team = []
home_pass_yards = 0
away_pass_yards = 0
home_run_yards = 0
away_run_yards = 0
#Append onto the teams arrays the necessary players and stats
for x in range(len(rowsQB)):
if(rowsQB[x][1] == home):
home_team.append(rowsQB[x])
break
for x in range(len(rowsRB)):
if(rowsRB[x][1] == home):
home_team.append(rowsRB[x])
break
for x in range(len(rowsQB)):
if(rowsQB[x][1] == away):
away_team.append(rowsQB[x])
break
for x in range(len(rowsRB)):
if(rowsRB[x][1] == away):
away_team.append(rowsRB[x])
break
for x in range(len(rowsOff)):
if(rowsOff[x][0] == home):
home_team.append(rowsOff[x])
break
for x in range(len(rowsOff)):
if(rowsOff[x][0] == away):
away_team.append(rowsOff[x])
break
for x in range(len(rowsPDef)):
if(rowsPDef[x][0] == home):
home_team.append(rowsPDef[x])
break
for x in range(len(rowsPDef)):
if(rowsPDef[x][0] == away):
away_team.append(rowsPDef[x])
break
for x in range(len(rowsRDef)):
if(rowsRDef[x][0] == home):
home_team.append(rowsRDef[x])
break
for x in range(len(rowsRDef)):
if(rowsRDef[x][0] == away):
away_team.append(rowsRDef[x])
break
#Error checking for adding
if(len(home_team) != 5):
print("Problem with home team adding")
if(len(away_team) != 5):
print("Problem with home team adding")
#Creating values for simulation that takes into account defense and offense
home_cpct = (float(home_team[0][4]) + float(away_team[3][1]))/2 #home adjusted comp percent
away_cpct = (float(away_team[0][4]) + float(home_team[3][1]))/2 #away adjusted comp percent
home_ypp = (float(home_team[0][13]) + float(away_team[3][4]))/2 #home adjusted yards per pass
away_ypp = (float(away_team[0][13]) + float(home_team[3][4]))/2 #away adjusted yards per pass
home_ypr = (float(home_team[1][6]) + float(away_team[4][1]))/2 #home adjusted yards per rush
away_ypr = (float(away_team[1][6]) + float(home_team[4][1]))/2 #away adjusted yards per rush
home_sack = (float(home_team[0][19]) + float(away_team[3][6]))/2 #home adjusted sack percentage
away_sack = (float(away_team[0][19]) + float(home_team[3][6]))/2 #away adjusted sack percentage
home_int = (float(home_team[0][9]) + float(away_team[3][3]))/2 #home adjusted interception percentage
away_int = (float(away_team[0][9]) + float(home_team[3][3]))/2 #away adjusted interception percentage
def pass_play(team,yard,down,togo,time,pct,ypp,inter,sack):
#Pass play called
num1 = random.random()
num2 = random.random()
hasball = True
if(num1*100 <= pct):
#Pass completed
yards = 0
time -= 40
if(num2 <= 0.5):
#Short pass
yards = random.randint(0,int(ypp))
elif(num2 < (1-(float(home_team[0][7])/100))):
#Medium pass
yards = random.randint(int(ypp),30)
else:
#Long pass
yards = random.randint(30,100)
yard += yards
togo -= yards
if(togo <= 0): #Check if first down
down = 1
togo = 10
else:
down += 1
else:
if(num1 > 1-(inter/100)):
#Pass intercepted
hasball = False
time -= 10
yard = 100-yard
elif(num1 > 1-(inter/100)-(sack/100)):
# QB sacked
yard -= 5
down += 1
togo += 5
time -= 40
else:
#Pass incomplete
time -= 10
down += 1
return hasball, yard, down, togo, time
def run_play(team,yard,down,togo,time,ypr):
#Run play
num1 = random.random()
num2 = random.random()
hasball = True
yards = 0
if(num1 <= 0.7):
#Short/average run
yards = random.randint(-1,int(ypr))
time -= 50
yard += yards
togo -= yards
if(togo <= 0): #Check if first down
down = 1
togo = 10
else:
down += 1
else:
if((int(team[1][8]) < 2) and (num1 >= 0.98)):
#Fumble for non fumble prone RB
hasball = False
time -= 10
yard = 100-yard
elif((int(team[1][8]) < 4) and (num1 >= 0.96)):
#Fumble for not very fumble prone RB
hasball = False
time -= 10
yard = 100-yard
elif((int(team[1][8]) < 6) and (num1 >= 0.94)):
#Fumble for somewhat fumble prone RB
hasball = False
time -= 10
yard = 100-yard
elif((int(team[1][8]) >= 6) and (num1 >= 0.92)):
#Fumble for sorta fumble prone RB
hasball = False
time -= 10
yard = 100-yard
else:
#Long run
yards = random.randint(int(float(team[1][6])),40)
time -= 50
yard += yards
togo -= yards
if(togo <= 0):
down = 1
togo = 10
else:
down += 1
return hasball, yard, down, togo, time
def field_goal_attempt(yard):
if(yardline >= 90):
#0-27 Yard FG
if(random.random() <= 0.98):
return True
else:
return False
elif(yardline >= 80):
#28-37 Yard FG
if(random.random() <= 0.95):
return True
else:
return False
elif(yardline >= 70):
#38-47 Yard FG
if(random.random() <= 0.85):
return True
else:
return False
elif(yardline >= 60):
#48-57 Yard FG
if(random.random() <= 0.67):
return True
else:
return False
#Initializing variables necessary for the sim
home_score = 0
away_score = 0
time_left = 1800
down = 1
yardline = 25
yardsleft = 10
yardline2 = 25
secondHalf = False
while(True):
if(time_left <= 0 and secondHalf == True):
#Check if game is over
break
#initialize the down and distance
down = 1
yardsleft = 10
#HOME TEAM OFFENSE
while(True):
if(time_left == 1800 and secondHalf == True):
#Check if it is after half and away team should have ball
break
if(time_left <= 0):
#Check if it is halftime
secondHalf = True
time_left = 1800
break
if(down == 1 and (100-yardline < yardsleft)):
#Check if it's a goal to go situation
yardsleft = 100-yardline
if(time_left <= 10 and yardline >= 60):
#Buzzer beater field goal
if(field_goal_attempt(yardline)):
home_score += 3
yardline = 25
time_left = 1800
secondHalf = True
break
elif(down == 4 and yardline >= 60):
#4th down field goal attempt
if(field_goal_attempt(yardline)):
home_score += 3
yardline = 25
time_left -= 5
#Made field goal
break
else:
#Switch possession
yardline = 100-yardline
time_left -= 5
break
elif(down == 4):
#Punt
time_left -= 10
yardline = yardline + 50
if(yardline >= 100):
#Touchback
yardline = 20
else:
yardline = 100-yardline
break
#First down pass/run selection
elif(down == 1 and random.random() < (float(home_team[2][1])-20)/100):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 1):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
elif(yardsleft <= 1 and home == "PHI"): #Gotta account for the Eagles Tush Push
yardline += 2
down = 1
yardsleft = 10
time_left -= 40
home_run_yards += 2
#Second down pass/run selection based on yards left and offense tendencies
elif(down == 2 and yardsleft > 7 and random.random() < ((float(home_team[2][1])+15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 2 and yardsleft > 7):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
elif(down == 2 and yardsleft > 3 and random.random() < ((float(home_team[2][1])-10)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 2 and yardsleft > 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
elif(down == 2 and random.random() < ((float(home_team[2][1])-15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 2):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
#3rd down pass/run selection based on yards left and offense tendencies
elif(down == 3 and yardsleft > 7 and random.random() < ((float(home_team[2][1])+35)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 3 and yardsleft > 7):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
elif(down == 3 and yardsleft > 3 and random.random() < ((float(home_team[2][1])+15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 3 and yardsleft > 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
elif(down == 3 and random.random() < ((float(home_team[2][1])-7)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(home_team,yardline,down,yardsleft,time_left,
home_cpct,home_ypp,home_int,home_sack)
if(a):
home_pass_yards += (yardline - temp)
elif(down == 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(home_team,yardline,down,yardsleft,time_left,home_ypr)
if(a):
home_run_yards += (yardline - temp)
if(yardline <= 0):
#Safety
away_score += 2
break
if(a == False):
#Turnover
break
if(yardline >= 100):
#Touchdown
home_score += 6
if(random.random() <= 0.95):
#Extra point
home_score += 1
yardline = 25
break
#AWAY TEAM OFFENSE
#Same procedure as above just with the away team
down = 1
yardsleft = 10
if(time_left <= 0 and secondHalf == True):
break
elif(time_left <= 0):
secondHalf = True
time_left = 1800
while(True):
if(down == 1 and (100-yardline < yardsleft)):
yardsleft = 100-yardline
num1 = random.random()
num2 = random.random()
if(time_left <= 10 and yardline >= 60):
if(field_goal_attempt(yardline)):
away_score += 3
yardline = 25
time_left -= 10
secondHalf = True
break
elif(down == 4 and yardline >= 65):
if(field_goal_attempt(yardline)):
away_score += 3
yardline = 25
time_left -= 10
break
else:
yardline = 100-yardline
time_left -= 10
break
elif(down == 4):
yardline = yardline + 50
time_left -= 10
if(yardline >= 100):
yardline = 20
else:
yardline = 100-yardline
break
elif(down == 1 and random.random() < (float(away_team[2][1])-20)/100):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 1):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(yardsleft <= 1 and away == "PHI"): #Gotta account for the Eagles QB sneak
yardline += 2
down = 1
yardsleft = 10
time_left -= 40
away_run_yards += 2
elif(down == 2 and yardsleft > 7 and random.random() < ((float(away_team[2][1])+15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 2 and yardsleft > 7):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(down == 2 and yardsleft > 3 and random.random() < ((float(away_team[2][1])-10)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 2 and yardsleft > 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(down == 2 and random.random() < ((float(away_team[2][1])-15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 2):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(down == 3 and yardsleft > 7 and random.random() < ((float(away_team[2][1])+35)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 3 and yardsleft > 7):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(down == 3 and yardsleft > 3 and random.random() < ((float(away_team[2][1])+15)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 3 and yardsleft > 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
elif(down == 3 and random.random() < ((float(away_team[2][1])-7)/100)):
temp = yardline
a,yardline,down,yardsleft,time_left = pass_play(away_team,yardline,down,yardsleft,time_left,
away_cpct,away_ypp,away_int,away_sack)
if(a):
away_pass_yards += (yardline - temp)
elif(down == 3):
temp = yardline
a,yardline,down,yardsleft,time_left = run_play(away_team,yardline,down,yardsleft,time_left,away_ypr)
if(a):
away_run_yards += (yardline - temp)
if(a == False):
break
if(yardline <= 0):
home_score += 2
break
if(yardline >= 100):
away_score += 6
if(random.random() <= 0.95):
away_score += 1
yardline = 25
break
if(home_score == away_score):
#Overtime, random for now
#TODO: Implement overtime
if(random.random() >= 0.5):
home_score += 3
else:
away_score += 3
return home_score,away_score,home_pass_yards,home_run_yards,away_pass_yards,away_run_yards