-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennis.py
93 lines (77 loc) · 2.37 KB
/
tennis.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
import random
class Player(object):
def __init__(self, name):
self.points_won = 0
self.games_won = 0
self.sets_won = 0
self.score = (
str(self.sets_won)
+ " sets "
+ str(self.games_won)
+ " games "
+ str(self.points_won)
+ " points "
)
self.name = name
def __repr__(self):
return self.name + " " + self.score
def set_score(self, score):
self.score = (
str(self.sets_won)
+ " sets "
+ str(self.games_won)
+ " games "
+ str(self.points_won)
+ " points "
)
def add_point(self):
if self.points_won == 0 or self.points_won == 15:
self.points_won += 15
elif self.points_won == 30:
self.points_won += 10
elif self.points_won == 40:
self.points_won = "Adv"
elif self.points_won == "Adv":
self.points_won = 0
self.games_won += 1
if self.games_won >= 6:
self.sets_won += 1
self.games_won = 0
self.points_won = 0
self.set_score(self.score)
class Match(object):
def __init__(self, player1, player2):
self.player1 = Player(player1)
self.player2 = Player(player2)
self.sets_played = 0
self.points_played = 0
self.winner = None
def __repr__(self):
if self.winner != None:
return str(self.winner) + " won! "
def simulate_match(self):
while self.sets_played <= 3:
self.points_played += 1
self.sets_played = self.player1.sets_won + self.player2.sets_won
if self.player1.sets_won == 2 or self.player2.sets_won == 2:
break
self.win_point()
def win_point(self):
decider = random.randint(1, 2)
if decider == 1:
self.player1.add_point()
return self.player1.name
if decider == 2:
self.player2.add_point()
return self.player2.name
def who_won(self):
if self.player1.sets_won > self.player2.sets_won:
self.winner = self.player1
else:
self.winner = self.player2
return self.winner
# trial
x = Match("Nadal", "Federer")
x.simulate_match()
x.who_won()
print(x.who_won())