-
Notifications
You must be signed in to change notification settings - Fork 0
/
team.py
183 lines (146 loc) · 4.14 KB
/
team.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
import random, copy
from helpers import goals_scored
class Team:
"""
A team has a manager. (1.1)
A team has many players.(1.n)
A team has ranking in a league.
A team plays game against other team.
"""
def __init__(self, name):
self.name = name
self.players = []
self.wins = 0
self.draw = 0
self.losses = 0
# budget at the beginning
self.money = 1000000
def weekly_salary(self):
salary = 0
for player in self.players:
salary += player.salary()
return salary
def pay_players(self):
self.money -= self.weekly_salary()
def rating(self):
"""
What is the rating of the team
"""
rating = 0
for player in self.players:
rating += player.skill
return rating
def __str__(self):
return f"{self.name}-{self.rating()}"
class Game:
"""
A game between 2 team
A game belong to a league
"""
def __init__(self, league, home_team, away_team):
self.league = league
self.home_team = home_team
self.away_team = away_team
self.home_team_won = None
self.away_team_won = None
print(f" {self.home_team} vs. {self.away_team}")
def play(self):
"""
Play a game and return who won
True means home team won, otherwise
Away team won
"""
print("play begins.")
goals_by_home_team = goals_scored(self.home_team.rating())
goals_by_away_team = goals_scored(self.away_team.rating())
print(f"Home team scored {goals_by_home_team}")
print(f"Away team scored {goals_by_away_team}")
print("play ends.")
if goals_by_home_team == goals_by_away_team:
# Game draw
print(f"Game between {self.home_team} and {self.away_team} is draw with {goals_by_home_team} - {goals_by_away_team} score.")
else:
# Got result
if goals_by_home_team > goals_by_away_team:
print(f"{self.home_team} wins by {goals_by_home_team} - {goals_by_away_team}")
self.home_team_won = True
else:
print(f"{self.home_team} wins by {goals_by_away_team} - {goals_by_home_team}")
self.away_team_won = True
class League:
"""
A league has many teams
Each team has ranking in the league
"""
def __init__(self, name, teams, players):
self.name = name
self.teams = teams
self.players = players
self.round_played = 0
def set_teams(self, teams):
self.teams = teams
def play_round(self):
"""
Play a round, which is 3 games
"""
num_team = len(self.teams)
num_games = num_team // 2
# making copy of the list
# doing copy.copy so it will not
# remove data from original list.
# because in python if you just
# assign a list to another list
# like, teams_to_play = self.teams
# then it would remove data from
# self.teams even if you try to
# delete data from teams_to_play
teams_to_play = copy.copy(self.teams)
print("Round begins...")
for game_num in range(num_games):
home_team = random.choice(teams_to_play)
teams_to_play.remove(home_team)
away_team = random.choice(teams_to_play)
game = Game(self, home_team, away_team)
game.play()
self.resolve_game(game)
print("Round ends...")
print("")
self.round_played += 1
self.ladder()
# ladder status
def ladder(self):
print("******* LADDER *******")
for team in sorted(self.teams, key=lambda t: -t.wins):
print(f"{team} {team.wins} wins.")
print("**********************")
def resolve_game(self, game):
prize_amount = round(200000 * random.random())
if game.home_team_won:
# home team won
game.home_team.wins += 1
game.away_team.losses += 1
# add prize money out of max 200000
game.home_team.money += prize_amount
print("")
print(f"{game.home_team} won {prize_amount}.")
print("")
elif game.away_team_won:
# away team won
game.away_team.wins += 1
game.home_team.losses += 1
# add prize money out of max 200000
game.away_team.money += prize_amount
print("")
print(f"{game.away_team} won {prize_amount}.")
print("")
else:
game.away_team.draw += 1
game.home_team.draw += 1
# Distribute prize money to both
game.home_team.money += prize_amount // 2
game.away_team.money += prize_amount // 2
print("")
print(f"Both {game.home_team} and {game.away_team} won {prize_amount // 2}.")
print("")
game.home_team.pay_players()
game.away_team.pay_players()