-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim.py
82 lines (73 loc) · 2.53 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
#!/usr/bin/env python3
import random
import math
# Simulation file
# Simulate the game and return win/draw/loss
class Simulation(object):
"""docstring for Simulation."""
home_adv = 3
outcomes = [1,2,3]
result = 0
def __init__(self, arg):
super(Simulation, self).__init__()
self.arg = arg
def simulate(teams):
# simulate the game
home = teams[0]
away = teams[1]
home.overall = home.overall + Simulation.home_adv
diff = home.overall - away.overall
# print(diff)
# Different states the differential can take:
# - < -10 5% 10% 85%
# - negative 25%-5% 25%-10% 50%-85%
# - 0 33% 33% 33%
# - <10 50%-85% 25%-10% 25%-5%
# - >10 85% 10% 5%
if diff <= -10:
# print(1)
chances = [5, 10, 85]
result = random.choices(Simulation.outcomes, weights=chances)
elif diff < 0:
# print(2)
diff = math.fabs(diff)
A = 25-((20/9)*(diff-1))
B = 25-((15/9)*(diff-1))
C = 50+((35/9)*(diff-1))
chances = [A, B, C]
# print('Win: {}% \nDraw: {}% \nLoss: {}% \nTotal: {}%'.format(A,B,C,(A+B+C)))
result = random.choices(Simulation.outcomes, weights=chances)
elif diff == 0:
# print(3)
chances = [33, 33, 33]
result = random.choices(Simulation.outcomes, weights=chances)
elif diff < 10:
# print(4)
A = 50+((35/9)*(diff-1))
B = 25-((15/9)*(diff-1))
C = 25-((20/9)*(diff-1))
chances = [A, B, C]
# print('Win: {}% \nDraw: {}% \nLoss: {}% \nTotal: {}%'.format(A,B,C,(A+B+C)))
result = random.choices(Simulation.outcomes, weights=chances)
else:
# print(5)
chances = [85, 10, 5]
result = random.choices(Simulation.outcomes, weights=chances)
if result == [1]:
return Simulation.win(home, away)
elif result == [2]:
return Simulation.draw(home, away)
elif result == [3]:
return Simulation.loss(home, away)
def win(home, away):
home.result = 3
away.result = 0
return home, away
def draw(home, away):
home.result = 1
away.result = 1
return home, away
def loss(home, away):
home.result = 0
away.result = 3
return home, away