25
3
In light of the 2014 Australian Open in Melbourne, and the win by Stanislas Wawrinka against Novak Djokovic, I propose the following code golf challenge!
Wawrinka and Djokovic are at it again for the basis of this CGC. Your challenge is to take a string consisting of 1's and 2's and determine the tennis scores based on the tennis scoring system. A "1" indicates that Wawrinka has scored a point, whereas a "2" indicates that Djokovic has scored a point.
For the sake of example: 1211222122111122111
will generate a two-line output:
Wawrinka 1 - 40
Djokovic 1 - 30
The longer the string, the further into the game the score is.
Rules:
- Shortest code wins, pure and simple.
- The scores when displayed must be right aligned and also column aligned; e.g.
Sample alignment of output:
Wawrinka 7 5 3 - 0 Djokovic 6 7 4 - 15
If a game is in progress, the score must be displayed after the set scores. Game scores must follow set scores with a space-dash-space separator. If this happens to be the first game, a 0-0 set score must be displayed.
Wawrinka 0 - 0 Djokovic 0 - 15
A game will go in order of 0, 15, 30, 40, and game point. If a game reaches 40-40, a deuce is declared, and the score must display the deuce on the player row scoring the point:
Wawrinka 7 5 3 - 40 Djokovic 6 7 4 - 40 Deuce
If a deuce is reached, the next point will be an advantage to the player scoring the point:
Wawrinka 7 5 3 - Ad Djokovic 6 7 4 - 40
The point after that, if scored by the advantaged player, wins the game, else the game goes back to deuce.
If more than one deuce is scored in between advantage points, the number of the deuce will be indicated by a number in parentheses:
Wawrinka 7 5 3 - 40 Djokovic 6 7 4 - 40 Deuce (2)
A set is won if a player reaches 6 games or more and with a two game margin in lead, e.g. 7-5, 6-4... 9-7 (In the case of last night's fifth set)
Consecutive sets must be separated with a space.
There are tie breaks in the Australian Open, therefore if a 6-6 set is reached, a tie-break is established by the first player reaching seven points with a two point margin lead. Display of tie breaks are shown within square brackets as follows:
Wawrinka 7 6 [6] Djokovic 6 6 [4]
The winner of the tie-break is said to have won the previous set 7-6.
If the fifth set reaches 6-6, play is continued without tie-break until a two game margin is reached with one player, that player being the winner of that set. There are NO tie-breaks in the fifth set.
If a player has won three sets, a third line will be displayed saying
Wawrinka wins
orDjokovic wins
depending on who has won and the code then terminates.Wawrinka 2 6 6 3 9 Djokovic 6 4 2 6 7 Wawrinka wins
If the string ends such that the last point has won a game, set or match, the game score is not displayed...
Wawrinka 7 5 3 Djokovic 6 7 4
If the string goes beyond a match being won, the rest of the string is ignored, the score displayed and the winner declared.
3I really don't think we need a new tag called [tag:sports]; [tag:game] should work just fine. – Justin – 2014-01-30T00:48:25.823
@Quincunx, good point... – WallyWest – 2014-01-30T04:34:33.457
1What will be the result? Just the end score or every step along the way? – Teun Pronk – 2014-01-30T08:17:30.720
1@TeunPronk depending on the score, the match will be finished or not. The idea seems to be: Given the string of points, what is the current score. – Tim Seguine – 2014-01-30T14:18:30.613
@TimSeguine ah okay, couldnt quite figure out if you should display just the current score or everytime a player gets points. – Teun Pronk – 2014-01-30T14:20:34.447
According to your rules "A set is won if a player reaches 6 games or more and with a two game margin in lead, e.g. 7-5, 6-4... 9-7 (In the case of last night's fifth set)" But it looks like the first set wasn't finished "7-6" because there wasn't a 2 game margin? – McKay – 2014-01-30T14:34:25.063
@McKay I think 7-6 is a finished set, but I agree that the description of the is problematic – Tim Seguine – 2014-01-30T15:05:09.120
Then what's the set win condition. It isn't stated? – McKay – 2014-01-30T15:05:45.020
@McKay I am not an expert in the rules of Tennis, but I think if the set comes to 6-6, then a tie break happens. Whoever wins the tie break wins the set 7-6. That is the only exception to the rule mentioned in the post. The OP did mention tie breaks but didn't specify how they work in that regard. – Tim Seguine – 2014-01-30T15:07:49.333
Then how does a score get to be 9-7? – McKay – 2014-01-30T15:10:21.933
@McKay because that is the final set. Okay, so I think it is fairly obvious that the OP needs to explain the scoring rules for tennis a bit better. There are no tiebreaks in the final set of a match. – Tim Seguine – 2014-01-30T15:13:45.233
The tie-breaker for the final set is often played as an Advantage Set, which can run on ridiculously long. The OP makes no mention of this, though. – primo – 2014-01-30T15:16:47.730
To the OP: I think some more test cases would be helpful. Also I think you need to clarify the scoring on some of the edge cases McKay has pointed out. I think this is a good question that wasn't quite ready to come out of the sandbox. – Tim Seguine – 2014-01-30T15:17:56.903
1@McKay - you shouldn't be able to reach 9-7 in the third set of the Australian Open; the tiebreak should apply in every set except the fifth. – Chowlett – 2014-01-30T16:41:18.407
My bad, guys, @Chowlett, you're right... I should have realized that tiebreaks should apply except in set 5... I'll update the description – WallyWest – 2014-01-31T00:41:00.450
you still say "•A set is won if a player reaches 6 games or more and with a two game margin in lead, e.g. 7-5, 6-4... 9-7 (In the case of last night's fifth set)" but yet have as example scoring 6-7. – McKay – 2014-01-31T02:34:43.963
1@WallyWest you were correct with your previous edit; only the fifth set has no tie-breaker. I fixed your example to make this more clear. – primo – 2014-01-31T02:58:03.957
@primo Thanks for the edit! – WallyWest – 2014-02-02T11:18:00.587
the tie break scores still don't make sense. "The winner of the tie-break is said to have won the previous set 7-6." So, what does it look like if it is 6-6, and someone wins a set? Wouldn't that be 7-6? – McKay – 2014-02-10T13:24:47.673
@McKay the tie-break is played as a separate set altogether (denoted by square brackets). After that set is won, the previous set is marked 7-6. – primo – 2014-02-11T04:14:38.300
You didn't directly answer the question. What does it look like if the score is 6 6, and someone wins a game? – McKay – 2014-02-11T13:45:32.560