-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtennis.py
More file actions
172 lines (148 loc) · 5.38 KB
/
tennis.py
File metadata and controls
172 lines (148 loc) · 5.38 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
# -*- coding: utf-8 -*-
class TennisGame1:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0
def won_point(self, playerName):
if playerName == self.player1Name:
self.p1points += 1
else:
self.p2points += 1
def score(self):
result = ""
tempScore=0
if (self.p1points==self.p2points):
result = {
0 : "Love-All",
1 : "Fifteen-All",
2 : "Thirty-All",
}.get(self.p1points, "Deuce")
elif (self.p1points>=4 or self.p2points>=4):
minusResult = self.p1points-self.p2points
if (minusResult==1):
result ="Advantage " + self.player1Name
elif (minusResult ==-1):
result ="Advantage " + self.player2Name
elif (minusResult>=2):
result = "Win for " + self.player1Name
else:
result ="Win for " + self.player2Name
else:
for i in range(1,3):
if (i==1):
tempScore = self.p1points
else:
result+="-"
tempScore = self.p2points
result += {
0 : "Love",
1 : "Fifteen",
2 : "Thirty",
3 : "Forty",
}[tempScore]
return result
class TennisGame2:
def __init__(self, player1Name, player2Name):
self.player1Name = player1Name
self.player2Name = player2Name
self.p1points = 0
self.p2points = 0
def won_point(self, playerName):
if playerName == self.player1Name:
self.P1Score()
else:
self.P2Score()
def score(self):
result = ""
if (self.p1points == self.p2points and self.p1points < 3):
if (self.p1points==0):
result = "Love"
if (self.p1points==1):
result = "Fifteen"
if (self.p1points==2):
result = "Thirty"
result += "-All"
if (self.p1points==self.p2points and self.p1points>2):
result = "Deuce"
P1res = ""
P2res = ""
if (self.p1points > 0 and self.p2points==0):
if (self.p1points==1):
P1res = "Fifteen"
if (self.p1points==2):
P1res = "Thirty"
if (self.p1points==3):
P1res = "Forty"
P2res = "Love"
result = P1res + "-" + P2res
if (self.p2points > 0 and self.p1points==0):
if (self.p2points==1):
P2res = "Fifteen"
if (self.p2points==2):
P2res = "Thirty"
if (self.p2points==3):
P2res = "Forty"
P1res = "Love"
result = P1res + "-" + P2res
if (self.p1points>self.p2points and self.p1points < 4):
if (self.p1points==2):
P1res="Thirty"
if (self.p1points==3):
P1res="Forty"
if (self.p2points==1):
P2res="Fifteen"
if (self.p2points==2):
P2res="Thirty"
result = P1res + "-" + P2res
if (self.p2points>self.p1points and self.p2points < 4):
if (self.p2points==2):
P2res="Thirty"
if (self.p2points==3):
P2res="Forty"
if (self.p1points==1):
P1res="Fifteen"
if (self.p1points==2):
P1res="Thirty"
result = P1res + "-" + P2res
if (self.p1points > self.p2points and self.p2points >= 3):
result = "Advantage " + self.player1Name
if (self.p2points > self.p1points and self.p1points >= 3):
result = "Advantage " + self.player2Name
if (self.p1points>=4 and self.p2points>=0 and (self.p1points-self.p2points)>=2):
result = "Win for " + self.player1Name
if (self.p2points>=4 and self.p1points>=0 and (self.p2points-self.p1points)>=2):
result = "Win for " + self.player2Name
return result
def SetP1Score(self, number):
for i in range(number):
self.P1Score()
def SetP2Score(self, number):
for i in range(number):
self.P2Score()
def P1Score(self):
self.p1points +=1
def P2Score(self):
self.p2points +=1
class TennisGame3:
def __init__(self, player1Name, player2Name):
self.p1N = player1Name
self.p2N = player2Name
self.p1 = 0
self.p2 = 0
def won_point(self, n):
if n == self.p1N:
self.p1 += 1
else:
self.p2 += 1
def score(self):
if (self.p1 < 4 and self.p2 < 4) and (self.p1 + self.p2 < 6):
p = ["Love", "Fifteen", "Thirty", "Forty"]
s = p[self.p1]
return s + "-All" if (self.p1 == self.p2) else s + "-" + p[self.p2]
else:
if (self.p1 == self.p2):
return "Deuce"
s = self.p1N if self.p1 > self.p2 else self.p2N
return "Advantage " + s if ((self.p1-self.p2)*(self.p1-self.p2) == 1) else "Win for " + s