-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathMain.py
More file actions
1196 lines (1055 loc) · 41 KB
/
Copy pathMain.py
File metadata and controls
1196 lines (1055 loc) · 41 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# https://github.com/benryan03/baseball_simulator/
import random
import time
import math
from datetime import datetime
from CalculatePitchOutcome import *
from PrintBoxScore import *
from SaveBoxScore import *
from GetUserInputs import *
from Functions import *
from LoadPlayers import *
# For text colors
from colorama import init, Fore, Back, Style
init()
home_score = 0
away_score = 0
half_inning = 1
balls = 0
strikes = 0
outs = 0
rand = 0
pitch_result = "_"
gameover = False
atbat_pitch_count = 1
pitch_count = {"home": 1, "away": -1}
current_batter = {"home": 0, "away": -1}
margin = 0
edge = ["", 0]
redo_pitch_loops = 0
runs_in_current_inning = 0
score_by_inning = {"home":[], "away":[]}
on_base = [-1, -1, -1, -1]
earned_runs = 0
def out(num):
global outs
global half_inning
global gameover
global balls
global strikes
global runs_in_current_inning
global on_base
for x in range(num):
# For box score
# Totals will get rounded to 1 decimal so .3333 is accurate enough :)
if half_inning % 2 == 0:
pitchers_used["away"][-1][2] = pitchers_used["away"][-1][2] + 0.3333
elif half_inning % 2 != 0:
pitchers_used["home"][-1][2] = pitchers_used["home"][-1][2] + 0.3333
if outs <= 1:
resetcount()
outs += 1
elif outs == 2 and half_inning < 17:
# before 9th inning, no win possible
outs = 3
wait()
half_inning += 1
inning_status()
outs = 0
on_base = [-1, -1, -1, -1]
balls = 0
strikes = 0
runs_in_current_inning = 0
if half_inning == 2:
print("\033[1;93;40m" + starting_pitchers["away"][0] + "\033[0m is now pitching for the " + teams["away"] + ".")
wait()
print(str(years["away"]) + " ERA: " + str(format_era(starting_pitchers["away"][1])))
wait()
elif (outs == 2 and half_inning >= 17 and half_inning % 2 != 0 and home_score <= away_score):
# if 2 outs and 9th inning or later and end of top of inning and away team is ahead or tied
outs = 3
print("")
wait()
half_inning += 1
inning_status()
outs = 0
on_base = [-1, -1, -1, -1]
balls = 0
strikes = 0
elif (outs == 2 and half_inning >= 17 and half_inning % 2 == 0 and home_score == away_score):
# if 2 outs and 9th inning or later and end of bottom of inning and score is tied
outs = 3
print("")
wait()
half_inning += 1
inning_status()
outs = 0
on_base = [-1, -1, -1, -1]
balls = 0
strikes = 0
else:
# Game over
gameover = True
def run(num):
global away_score
global home_score
global gameover
global runs_in_current_inning
global score_by_inning
global earned_runs
global batters
runners = {0: None, 1: None, 2: None, 3: None}
# Determine and who scored the runs, and update their Run stats for box score
if num == 1 and on_base[3] > -1:
# 1 run scored from third
batters[batting_team(half_inning)][on_base[3]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
elif num == 1 and on_base[2] > -1:
# 1 run scored from second
batters[batting_team(half_inning)][on_base[2]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[2]]
elif num == 1 and on_base[1] > -1:
# 1 run scored from first
batters[batting_team(half_inning)][on_base[1]][3] += 1
elif num == 1 and (on_base[3] == -1 and on_base[2] == -1 and on_base[1] == -1):
# Solo home run
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 2 and on_base[3] > -1 and on_base[2] > -1:
# 2 runs scored from second and third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[2]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[2]]
elif num == 2 and on_base[3] > -1 and on_base[1] > -1:
# 2 runs scored from first and third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[1]]
elif num == 2 and on_base[2] > -1 and on_base[1] > -1:
# 2 runs scored from first and second
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[2]]
runners[1] = batters[batting_team(half_inning)][on_base[1]]
elif num == 2 and (on_base[3] > -1 and on_base[2] == -1 and on_base[1] == -1):
# 2 run HR with runner on third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 2 and (on_base[3] == -1 and on_base[2] > -1 and on_base[1] == -1):
# 2 run HR with runner on second
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[2]]
runners[1] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 2 and (on_base[3] == -1 and on_base[2] == -1 and on_base[1] > -1):
# 2 run HR with runner on first
batters[batting_team(half_inning)][on_base[1]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[1]]
runners[1] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 3 and (on_base[3] > -1 and on_base[2] > -1 and on_base[1] > -1):
# 3 runs scored from first, second, and third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[2]]
runners[2] = batters[batting_team(half_inning)][on_base[1]]
elif num == 3 and on_base[3] > -1 and on_base[2] > -1:
# 3 run HR, runners on second and third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[2]]
runners[2] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 3 and on_base[3] > -1 and on_base[1] > -1:
# 3 run HR, runners on first and third
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[1]]
runners[2] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 3 and on_base[2] > -1 and on_base[1] > -1:
# 3 run HR, runners on first and second
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[2]]
runners[1] = batters[batting_team(half_inning)][on_base[1]]
runners[2] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
elif num == 4: # Grand slam
batters[batting_team(half_inning)][on_base[3]][3] += 1
batters[batting_team(half_inning)][on_base[2]][3] += 1
batters[batting_team(half_inning)][on_base[1]][3] += 1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][3] += 1
runners[0] = batters[batting_team(half_inning)][on_base[3]]
runners[1] = batters[batting_team(half_inning)][on_base[2]]
runners[2] = batters[batting_team(half_inning)][on_base[1]]
runners[3] = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]]
# Print who scored the runs
for x in range(3):
if runners[x] != None:
wait()
print("")
wait()
print("\033[1;30;102m" + runners[x][0] + " scored a run for the " + teams[batting_team(half_inning)] + "!\033[0m")
for x in range(num):
# Line score
inning = int((half_inning / 2) + 0.5)
if len(score_by_inning[batting_team(half_inning)]) < inning:
score_by_inning[batting_team(half_inning)].append(1)
else:
score_by_inning[batting_team(half_inning)][-1] += 1
# Box score - earned runs
pitchers_used[pitching_team(half_inning)][-1][3] += 1
if earned_runs < 0:
earned_runs += 1
else:
pitchers_used[pitching_team(half_inning)][-1][5] += 1
if half_inning < 18 and half_inning % 2 != 0:
# normal innings - run for away
away_score += 1
runs_in_current_inning += 1 # For pitching change check
batters["away"][current_batter["away"]][5] += 1 # RBI count for box score
wait_short()
elif half_inning < 18 and half_inning % 2 == 0:
# normal innings - run for home
home_score += 1
runs_in_current_inning += 1 # For pitching change check
batters["home"][current_batter["home"]][5] += 1 # RBI count for box score
wait_short()
elif half_inning >= 18 and half_inning % 2 != 0:
# extra innings - run for away
away_score += 1
runs_in_current_inning += 1 # For pitching change check
batters["away"][current_batter["away"]][5] += 1 # RBI count for box score
wait_short()
elif half_inning >= 18 and half_inning % 2 == 0 and away_score > home_score:
# extra innings - run for home, no walkoff
home_score += 1
batters["home"][current_batter["home"]][5] += 1 # RBI count for box score
runs_in_current_inning += 1 # For pitching change check
wait_short()
elif half_inning >= 18 and half_inning % 2 == 0 and away_score == home_score:
# walkoff run!
home_score += 1
batters["home"][current_batter["home"]][5] += 1 # RBI count for box score
print("\033[1;30;102mWALKOFF for the " + teams["home"] + "!\033[0m")
wait()
print("")
wait()
print("Game has ended. " + teams["home"] + " wins.")
gameover = True
def inning_status():
# Update line score for end-of-game stats
prev_half_inning = half_inning - 1
if len(score_by_inning[batting_team(half_inning)]) < prev_half_inning - 1:
score_by_inning[batting_team(half_inning)].append(0)
# This will be accurate until the 21st inning - will fix eventually
if half_inning == 1 or half_inning == 2:
x = "st"
elif half_inning == 3 or half_inning == 4:
x = "nd"
elif half_inning == 5 or half_inning == 6:
x = "rd"
else:
x = "th"
# Print inning status
wait()
print("")
print("")
print("------------------------------------")
print(
"It is now the ", end = "")
if half_inning % 2 != 0:
print("top", end = "")
elif half_inning % 2 == 0:
print("bottom", end = "")
print(" of the "
+ str((half_inning / 2) + 0.5).split(".")[0]
+ x
+ " inning."
)
print("------------------------------------")
print("")
wait()
def status(): # Print number of outs, inning number, score, and on-base statuses
wait()
print("-------------------------------------------------------------")
wait()
print("Outs: " + str(outs) + " | Inning: ", end="")
if half_inning % 2 != 0:
print("Top ", end="")
elif half_inning % 2 == 0:
print("Bot ", end="")
print(
str(math.ceil(half_inning / 2))
+ " | "
+ abbrs["home"]
+ ": "
+ str(home_score)
+ " | "
+ abbrs["away"]
+ ": "
+ str(away_score)
+ " | 3B: ",
end="",
)
if on_base[3] > -1:
print("\033[1;93;40mX\033[0m 2B: ", end="")
elif on_base[3] == -1:
print(" 2B: ", end="")
if on_base[2] > -1:
print("\033[1;93;40mX\033[0m 1B: ", end="")
elif on_base[2] == -1:
print(" 1B: ", end="")
if on_base[1] > -1:
print("\033[1;93;40mX\033[0m")
elif on_base[1] == -1:
print(" ")
wait()
now_batting()
wait()
def now_batting():
global edge
global edge_pos
global margin
global redo_pitch_loops
global half_inning
# Print name and average of current batter
print(
"\033[1;93;40m"
+ str(batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][0])
+ "\033[0m is now batting for the "
+ teams[batting_team(half_inning)]
+ ". "
+ str(years[batting_team(half_inning)])
+ " AVG: "
+ format_batting_average(batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][1])
)
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][2] += 1 # Update at-bat count for box score
redo_pitch_loops = 0
# Determine edge
avg = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][1]
era = current_pitcher[pitching_team(half_inning)][1]
x = avg / 0.250
y = (2 - (era / 4)) - (pitch_count[pitching_team(half_inning)] * 0.005)
if x > y:
# Batter has edge
edge = batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][0]
edge_pos = "Batter"
margin = x - y
elif x <= y:
# Pitcher has edge
edge = current_pitcher[pitching_team(half_inning)][0]
edge_pos = "Pitcher"
margin = y - x
# Print edge
wait()
margin = round(margin * 50, 1)
print("Edge: " + edge + " - " + str(margin) + "%")
def check_if_pitching_change():
if (current_pitcher[pitching_team(half_inning)][0] == starting_pitchers[pitching_team(half_inning)][0] and pitch_count[pitching_team(half_inning)] >= 100):
# Starter is still in and has thrown 100 pitches
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] == starting_pitchers[pitching_team(half_inning)][0] and half_inning >= 13):
# Starter is still in and it is the top of the 7th inning
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] == starting_pitchers[pitching_team(half_inning)][0] and pitchers_used[pitching_team(half_inning)][0][5] > 4):
# Starter is still in and has allowed more than 4 runs
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] != starting_pitchers[pitching_team(half_inning)][0] and half_inning <= 9 and runs_in_current_inning > 2 and len(relief_pitchers[pitching_team(half_inning)]) > 0):
# A reliever is in and has allowed more than 2 runs and it is before the 6th inning
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] != starting_pitchers[pitching_team(half_inning)][0] and current_pitcher[pitching_team(half_inning)][0] != closers[pitching_team(half_inning)][0] and (half_inning == 17 or half_inning == 18)):
# Top of 9th inning (Send in closer)
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] != starting_pitchers[pitching_team(half_inning)][0] and half_inning > 9 and outs == 0 and on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1 and runs_in_current_inning == 0 and len(relief_pitchers[pitching_team(half_inning)]) > 0):
# A reliever is in and it is the start of an inning, 6th or later
pitching_change()
elif (current_pitcher[pitching_team(half_inning)][0] != starting_pitchers[pitching_team(half_inning)][0] and runs_in_current_inning > 2 and len(relief_pitchers[pitching_team(half_inning)]) > 0):
# A reliever is in and has allowed more than 2 runs
pitching_change()
def pitching_change():
global relief_pitchers
global current_pitcher
global pitch_count
global runs_in_current_inning
global earned_runs
runs_in_current_inning = 0
# Used for Earned Runs in box score
earned_runs = 0
if on_base[1] > -1:
earned_runs = earned_runs - 1
if on_base[2] > -1:
earned_runs = earned_runs - 1
if on_base[3] > -1:
earned_runs = earned_runs - 1
if half_inning == 17 or half_inning == 18:
# 9th inning - send in closer
current_pitcher[pitching_team(half_inning)] = closers[pitching_team(half_inning)]
else:
# Not 9th inning - Choose a random relief pitcher
x = len(relief_pitchers[pitching_team(half_inning)])
rand = random.randint(0, x - 1)
current_pitcher[pitching_team(half_inning)] = relief_pitchers[pitching_team(half_inning)][rand]
del relief_pitchers[pitching_team(half_inning)][rand]
pitch_count[pitching_team(half_inning)] = 1
pitchers_used[pitching_team(half_inning)].append(current_pitcher[pitching_team(half_inning)]) # Add pitcher to array for box score
for x in range(10): # Generate blank stats for box score
pitchers_used[pitching_team(half_inning)][-1].append(0)
# Print new pitcher
wait()
print("Pitching change!")
wait()
print("")
wait()
print("\033[1;93;40m" + current_pitcher[pitching_team(half_inning)][0] + "\033[0m is now pitching for the " + teams[pitching_team(half_inning)] + ".")
wait()
print(str(years[pitching_team(half_inning)]) + " ERA: " + str(format_era(current_pitcher[pitching_team(half_inning)][1])))
wait()
print("")
wait()
def resetcount():
global balls
global strikes
balls = 0
strikes = 0
#######################################################################################################################
#######################################################################################################################
# Program start
print("Welcome to Baseball Simulator")
# Get user inputs to choose teams
inputs = GetUserInputs()
teams = {"home": inputs[0], "away": inputs[1]}
abbrs = {"home": inputs[2], "away": inputs[3]}
years = {"home": inputs[4], "away": inputs[5]}
print("")
print("Loading players...")
# Load players
batters = LoadBatters(abbrs, years)
pitchers = LoadPitchers(abbrs, years)
relief_pitchers = LoadRelievers(abbrs, years)
closers = LoadClosers(abbrs, years)
current_pitcher = {"home": ["", 0], "away": ["", 0]}
# For box score
starting_pitchers = {"home": ["", 0], "away": ["", 0]}
pitchers_used = {"home": [], "away": []}
# Print sorted batters for Home
print("\nStarting lineup for the " + str(years["home"]) + " " + teams["home"] + ":")
wait()
for x in batters["home"]:
print(x[0] + " - " + format_batting_average(x[1]))
wait_short()
# Print sorted batters for Away
print("\nStarting lineup for the " + str(years["away"]) + " " + teams["away"] + ":")
wait()
for x in batters["away"]:
print(x[0] + " - " + format_batting_average(x[1]))
wait_short()
# Choose a random starting pitcher for each team
pitcher_rand = random.randint(0, 4)
starting_pitchers["home"] = pitchers["home"][pitcher_rand]
current_pitcher["home"] = starting_pitchers["home"]
pitcher_rand = random.randint(0, 4)
starting_pitchers["away"] = pitchers["away"][pitcher_rand]
current_pitcher["away"] = starting_pitchers["away"]
# Keep track of what starting pitchers were used, for end-of-game box score
pitchers_used["home"].append(starting_pitchers["home"])
for x in range(10):
pitchers_used["home"][-1].append(0)
pitchers_used["away"].append(starting_pitchers["away"])
for x in range(10):
pitchers_used["away"][-1].append(0)
wait()
print("")
wait()
print("PLAY BALL!")
wait()
print("")
wait()
print("\033[1;93;40m" + starting_pitchers["home"][0] + "\033[0m is now pitching for the " + teams["home"] + ".")
wait()
print(str(years["home"]) + " ERA: " + str(format_era(starting_pitchers["home"][1])))
wait()
print()
wait()
status()
first_pitch_time = (
(datetime.strftime(datetime.now(), "%Y")) + "-"
+ (datetime.strftime(datetime.now(), "%m")) + "-"
+ (datetime.strftime(datetime.now(), "%d")) + " at "
+ (datetime.strftime(datetime.now(), "%H")) + ":"
+ (datetime.strftime(datetime.now(), "%M")))
#######################################################################################################################
#######################################################################################################################
while gameover == False: # Main game loop
pitch_result = calculate_pitch_outcome(atbat_pitch_count, False, edge_pos, margin, redo_pitch_loops)
if pitch_result == "Ball" and balls < 3: # Ball
balls += 1
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("Ball. (" + str(balls) + " - " + str(strikes) + ")")
elif pitch_result == "Ball" and balls == 3: # Walk
pitch_result = "Walk"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("\033[1;30;102mWALK!\033[0m")
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1):
on_base[3] = on_base[2]
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1):
run(1)
on_base[3] = on_base[2]
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1):
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][7] += 1 # Batter walk count for box score
pitchers_used[pitching_team(half_inning)][-1][7] += 1 # Pitcher walk count for box score
resetcount()
elif pitch_result == "Strike" and strikes < 2: #Strike
strikes += 1
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("Strike. (" + str(balls) + " - " + str(strikes) + ")")
elif pitch_result == "Strike" and strikes == 2: # Strikeout
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("\033[1;97;101mSTRIKEOUT!\033[0m")
pitch_result = "Strikeout"
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][8] += 1 # Batter strikeout count for box score
pitchers_used[pitching_team(half_inning)][-1][8] += 1 # Pitcher strikeout count for box score
out(1)
elif pitch_result == "Foul" and strikes < 2: # Foul
strikes += 1
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("Foul. (" + str(balls) + " - " + str(strikes) + ")")
elif pitch_result == "Foul" and strikes == 2: # Foul (with 2 strikes)
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("Foul. (" + str(balls) + " - " + str(strikes) + ")")
elif pitch_result == "Ball_in_play":
rand = random.randint(1, 100)
if 1 <= rand <= 40: # Fly out
if edge_pos == "Batter":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Fly"
else:
pitch_result == "Fly"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
ball_in_play_animation()
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
# Runner on first
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1 and outs < 2):
# Runner on and second, less than 2 outs
print("\033[1;30;103mFLY OUT! RUNNER ADVANCED.\033[0m")
out(1)
on_base[3] = on_base[2]
on_base[2] = -1
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1 and outs == 2):
# Runners on first and second, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1 and outs < 2):
#Runner on third, less than 2 outs
print("\033[1;30;102mSACRIFICE FLY!\033[0m")
out(1)
run(1)
on_base[3] = -1
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1 and outs == 2):
# Runner on third, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1 and outs < 2):
# Runners on first and second, less than 2 outs
print("\033[1;30;103mFLY OUT! RUNNER ADVANCED.\033[0m")
out(1)
on_base[3] = on_base[2]
on_base[2] = -1
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1 and outs == 2):
# Runners on first and second, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1 and outs < 2):
# Runners on second and third, less than 2 outs
print("\033[1;30;102mSACRIFICE FLY!\033[0m")
out(1)
run(1)
on_base[3] = -1
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1 and outs == 2):
# Runners on second and third, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1 and outs < 2):
# Runners on first and third, less than 2 outs
print("\033[1;30;102mSACRIFICE FLY!\033[0m")
out(1)
run(1)
on_base[3] = -1
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1 and outs == 2):
# Runners on first and third, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1 and outs < 2):
# Bases loaded, less than 2 outs
print("\033[1;30;102mSACRIFICE FLY!\033[0m")
out(1)
run(1)
on_base[3] = -1
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1 and outs < 2):
# Bases loaded, 2 outs
print("\033[1;97;101mFLY OUT!\033[0m")
out(1)
resetcount()
pitch_result = "Fly"
elif 41 <= rand <= 70: # Ground out
if edge_pos == "Batter":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Grounder"
else:
pitch_result == "Grounder"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
ball_in_play_animation()
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1 and outs < 2):
# Runner on first, 0-1 outs
print("\033[1;97;101mDOUBLE PLAY!\033[0m")
out(2)
on_base[1] = -1
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1 and outs == 2):
# Runner on first, 2 outs
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
# Runner on second
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
# Runner on third
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1 and outs == 0):
# Runners on first and second, 0 outs
print("\033[1;97;101mTRIPLE PLAY\033[0m")
out(3)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1 and outs == 1):
# Runners on first and second, 1 out
print("\033[1;97;101mDOUBLE PLAY!\033[0m")
out(2)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1 and outs == 2):
# Runners on first and second, 2 outs
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):
# Runners on second and third
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1 and outs == 0):
# Bases loaded, no outs
print("\033[1;97;101mTRIPLE PLAY!\033[0m")
out(3)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1 and outs == 1):
# Bases loaded, 2 out
print("\033[1;97;101mDOUBLE PLAY!\033[0m")
out(2)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1 and outs == 2):
# Bases loaded, 2 outs
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1 and outs < 2):
# Runners on first and third, 0-1 outs
print("\033[1;97;101mDOUBLE PLAY!\033[0m")
out(2)
on_base[1] = -1
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1 and outs == 2):
# Runners on first and third, 2 outs
print("\033[1;97;101mGROUND OUT!\033[0m")
out(1)
resetcount()
pitch_result = "Grounder"
elif 71 <= rand <= 87: # Single
if edge_pos == "Pitcher":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Single"
else:
pitch_result == "Single"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
ball_in_play_animation()
print("\033[1;30;102mSINGLE!\033[0m")
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
# Runner on first
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
# Runner on second
on_base[3] = on_base[2]
on_base[2] = -1
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
# Runner on third
run(1)
on_base[3] = -1
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1):
# Runners on first and second
on_base[3] = on_base[2]
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):
# Runners on second and third
run(1)
on_base[3] = on_base[2]
on_base[2] = -1
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1):
# Bases loaded
run(1)
on_base[3] = on_base[2]
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1):
# Runners on first and third
run(1)
on_base[3] = -1
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][4] += 1 # Batter hit count for box score
pitchers_used[pitching_team(half_inning)][-1][4] += 1 # Pitcher hit count for box score
resetcount()
pitch_result = "Single"
elif 88 <= rand <= 93: # Double
if edge_pos == "Pitcher":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Double"
else:
pitch_result == "Double"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
ball_in_play_animation()
print("\033[1;30;102mDOUBLE!\033[0m")
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
on_base[2] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
# Runner on first
on_base[3] = on_base[1]
on_base[2] = current_batter[batting_team(half_inning)]
on_base[1] = -1
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
# Runner on second
run(1)
on_base[2] = -1
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
# Runner on third
run(1)
on_base[3] = -1
on_base[2] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1):
# Runners on first and second
run(2)
on_base[2] = current_batter[batting_team(half_inning)]
on_base[1] = -1
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):
# Runners on second and third
run(2)
on_base[2] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1):
# Bases loaded
run(3)
on_base[3] = on_base[1]
on_base[2] = current_batter[batting_team(half_inning)]
on_base[1] = -1
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1):
# Runners on first and third
run(2)
on_base[3] = on_base[1]
on_base[2] = current_batter[batting_team(half_inning)]
on_base[1] = -1
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][4] += 1 # Batter hit count for box score
pitchers_used[pitching_team(half_inning)][-1][4] += 1 # Pitcher hit count for box score
resetcount()
pitch_result = "Double"
elif 94 <= rand <= 98: # Home run
if edge_pos == "pitcher":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Home run"
else:
pitch_result == "Home run"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
ball_in_play_animation()
print("\033[1;30;102mHOME RUN!\033[0m")
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
run(1)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
# Runner on first
run(2)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
# Runner on second
run(2)
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
# Runner on third
run(2)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1):
# Runners on first and second
run(3)
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):
# Runners on second and third
run(3)
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] > -1):
# Bases loaded
run(4)
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] > -1):
# Runners on first and third
run(3)
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][4] += 1 # Hit count for box score
batters[batting_team(half_inning)][current_batter[batting_team(half_inning)]][6] += 1 # HR count for box score
pitchers_used[pitching_team(half_inning)][-1][4] += 1 # Pitcher hit count for box scoure
pitchers_used[pitching_team(half_inning)][-1][6] += 1 # Pitcher HR count for box score
resetcount()
pitch_result = "Home run"
on_base[1] = -1
on_base[2] = -1
on_base[3] = -1
elif 97 <= rand <= 99: # Hit by pitch
if edge_pos == "pitcher":
rand = random.randint(1, 100)
if 1 <= rand <= round(margin, 0):
redo_pitch_loops += 1
continue
else:
pitch_result = "Hit by pitch"
else:
pitch_result == "Hit by pitch"
pitching_animation(pitch_count, half_inning, current_pitcher, redo_pitch_loops)
print("\033[1;30;102mHIT BY PITCH!\033[0m")
if (on_base[1] == -1 and on_base[2] == -1 and on_base[3] == -1):
# Bases empty
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] == -1 and on_base[3] == -1):
# Runner on first
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] == -1):
# Runner on second
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] == -1 and on_base[3] > -1):
# Runner on third
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] > -1 and on_base[2] > -1 and on_base[3] == -1):
# Runners on first and second
on_base[3] = on_base[2]
on_base[2] = on_base[1]
on_base[1] = current_batter[batting_team(half_inning)]
elif (on_base[1] == -1 and on_base[2] > -1 and on_base[3] > -1):