-
Notifications
You must be signed in to change notification settings - Fork 0
/
tennisScore.py
62 lines (47 loc) · 2.08 KB
/
tennisScore.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
import os
POINTSDICT = {0: 'love', 1: 15, 2: 30, 3: 40, 4: '40+adv', 5: '40+adv+adv'}
MIN_POINTS_REQ_TO_WIN = 4
MIN_DIFF_REQ = 2
MIN_GAMES_REQ_TO_WIN = 6
PLAYER_A = 'A'
PLAYER_B = 'B'
def updateScores(points, scores, player1, player2):
scores[player1] = POINTSDICT[points[player1]]
scores[player2] = POINTSDICT[points[player2]]
return scores
def updateSetWin(games, sets, player1, player2):
if (games[player1] > MIN_GAMES_REQ_TO_WIN and games[player2] == MIN_GAMES_REQ_TO_WIN) or games[player1]-games[player2] >= MIN_DIFF_REQ:
sets[player1] += 1
games[player1] = games[player2] = 0
return games, sets
def updateGameWin(points, games, sets, player1, player2):
if points[player1] == MIN_POINTS_REQ_TO_WIN and points[player2] == MIN_POINTS_REQ_TO_WIN:
points[player1] = points[player2] = MIN_POINTS_REQ_TO_WIN-1
if points[player1]-points[player2] >= MIN_DIFF_REQ:
games[player1] += 1
if games[player1] >= MIN_GAMES_REQ_TO_WIN:
games, sets = updateSetWin(games, sets, player1, player2)
points[player1] = points[player2] = 0
return points, games
def updatePoints(points, games, sets, player1, player2):
points[player1] += 1
if points[player1] >= MIN_POINTS_REQ_TO_WIN:
return updateGameWin(points, games, sets, player1, player2)
return points, games
def tennis_match(s):
points = [0, 0]
scores = [0, 0]
games = [0, 0]
sets = [0, 0]
currentPlayerIndex = 0
for player in s:
currentPlayerIndex = 0 if player == PLAYER_A else 1
points, games = updatePoints(
points, games, sets, currentPlayerIndex, abs(currentPlayerIndex-1))
scores = updateScores(points, scores,
currentPlayerIndex, abs(currentPlayerIndex-1))
return "Players : A B"+"\nSets : "+str(sets)+"\nGames : " + str(games)+"\nPoints : "+str(scores)
def tennis_match_graph():
os.system("dot -T png -o tennisGameGraph.png tennisGameGraph.dot")
print(tennis_match('ABABABABAAA'))
tennis_match_graph()