Confused badminton players

8

Introduction

Jim and Bob are playing badminton doubles against two other players, but mid-game they face a dilemma:
After a fast-paced rally they have no idea who has to serve next and where they have to stand for the next serve.
Bad enough they only know the scores in order.

Rules:

To help them out, you have to know the rules:

  1. If the serving team has an even score, serving is done by the player on the right side (relatively facing towards the net)
  2. If the serving team has an odd score, serving is done by the left player.
  3. If the serving team changes, the players' position remains.
  4. If one team keeps the right to serve the ball, the two players of that team swap places, so the same player serves again.
  5. The team, that scored last, serves next.

This is where you come to help them out.

Your task:

Your task is to take the scores in order and return the position for all four players according to the rules as well as the player, who has to serve next.

Specification:

Input:

Input is a string / array / set / whatever you like containing all scores in the order they are scored.
Example: "0:0, 0:1, 0:2, 1:2, 1:3" or ["0:0", "0:1", "0:2", "1:2", "1:3"]
You may assume team 1 (the left part of the score) served first in the beginning.

The input is not secured to be valid!

Output:

Your program / function / script has to output an ASCII-art style badminton court (don't care about orientation) with the position of the players and some representation of the player serving next (a single line saying Next to serve is >Player1< is enough).

Example output of the example given above ("0:0, 0:1, 0:2, 1:2, 1:3"):

|-----|-----|
|-Jim-|-Bob-| //Team 1
|###########|
|-P4--|-P3--| //Team 2
|-----|-----|
Player P4 of team 2 serves next.

The players are named Jim, Bob, P3 and P4. P3 and P4 swap their places after scoring "0:2" and keeping the right to serve. Jim and Bob never scored two times in a row and thus never swap places.

Last notes about input and output:

  • Input is assumed to be always in order with the first (left) score to be team 1, which may be assumed to have served first.
  • The names of the players are irrelevant as long as it is documented which player belongs to which team.
  • Team 1 is always the upper/left team in your output.
  • Even though a usual badminton round ends when one team scores 21 (without overtime), you may assume the game to be as long as the two teams want to play.
  • Sometimes the input may be incomplete (missing a score). Then your program has to output an error message indicating invalid input (E for Error! is enough for golfing reasons).

Scoring:

This is code-golf, so the least amount of bytes wins.
There are bonuses to claim though:

  • -20% if you output the court and server for every single serve.
  • -10% if your program allows to choose the serving team.
  • -15% if the user can choose the names for the players.

The bonus percentages are first added and then applied to the byte count, resulting in a maximum bonus of 45%!

Test cases (so you don't have to come up with your own):

(Strings only, including output and validation):
Example1:

0:0, 1:0, 2:0, 3:0, 3:1, 3:2, 4:2, 4:3, 5:3, 6:3, 6:4, 7:4, 7:5, 7:6, 8:6, 8:7, 9:7, 10:7, 11:7, 12:7, 12:8, 12:9, 13:9

Output:

|-----|-----|
|-Bob-|-Jim-| //Team 1
|###########|
|-P4--|-P3--| //Team 2
|-----|-----|
Player Jim of team 1 serves next.

Example2:

0:0, 0:1, 0:2, 1:2, 2:2, 3:3, 4:3, 4:4 -> Invalid
                          /\              Both teams can't score at the same time/missing intermediate score

Output:

Invalid input! Please check input and try again.

In case you have questions or need clarification, please ask so I can improve the challenge.

GiantTree

Posted 2015-01-24T15:42:18.900

Reputation: 885

1Nice challenge! Just a question though - does the error message have to exactly match the one in the test case? "Appropriate error message" seems a little ambiguous to me - seeing as this is code-golf, one might argue that "E" is their error message or something. – Sp3000 – 2015-01-24T15:48:14.373

It's just for clarification. Because this is code-golf, "E" is enough for an error message. – GiantTree – 2015-01-24T15:51:06.707

Answers

4

Python 2: 320 chars - 45% = 176

def f(n,t,s):
 for l,r in zip(s,s[1:]):
  d=[r[0]-l[0],r[1]-l[1]]
  if[0,1]!=sorted(d):print'E';break
  if d[1]==t:v=2*t;n[v:v+2]=n[v:v+2][::-1]
  t=d[1];m=max(map(len,n))+2;z=('|'+'-'*m)*2;c='|{:^%d}'%m;print'|\n'.join([z,c*2,'|#'+'#'*2*m,c*2,z,'Player {} of team {} serves next.\n']).format(*n+[n[2*t+(t+r[t])%2],t+1])

I'm not happy with lots of things. For instance the line d=[r[0]-l[0],r[1]-l[1]] is so ugly. But spend quite some time in golfing this.

Claims all 3 bonuses. Please tell me, if I my output is not entirely the way you wanted. The question was not very clear.

Usage:

This defines a python method. The parameters are a list of strings (the names of the players), an integer 0 or 1 (serving team) and a list of tuples of the scores.

f(["Jim","Bob","P3","P4"],0,[(0, 0), (0, 1), (0, 2), (1, 2), (1, 3)])
f(["Jim","Bob","A","Montgomery"],0,[(0, 0), (1, 0), (2, 0), (3, 0), (3, 1), (3, 2), (4, 2), (4, 3), (5, 3), (6, 3), (6, 4), (7, 4), (7, 5), (7, 6), (8, 6), (8, 7), (9, 7), (10, 7), (11, 7), (12, 7), (12, 8), (12, 9), (13, 9)])
f(["Jim","Bob","P3","P4"],0,[(0, 0), (0, 1), (0, 2), (1, 2), (2, 2), (3, 3), (4, 3), (4, 4)])

Output:

|-----|-----|
| Jim | Bob |
|###########|
| P4  | P3  |
|-----|-----|
Player P4 of team 2 serves next.

|------------|------------|
|    Bob     |    Jim     |
|#########################|
| Montgomery |     A      |
|------------|------------|
Player Jim of team 1 serves next.

E

This is of course shortened, for the full output visit http://pastebin.com/AzbnEbaH.

Jakube

Posted 2015-01-24T15:42:18.900

Reputation: 21 462

The output is looks exactly like what I'm looking for. If you have questions about rules, please ask so I can improve the challenge. – GiantTree – 2015-01-24T21:13:27.773

@GiantTree What does an even score mean? Is 1:3 (1+3=4) even, or is 2:? even for team 1 and ?:2 even for team 2. I figured the second option. And the question never says, when the serving team changes. I figured, when they make a mistake. I was a little bit confused about these two clarification, searched for the official rules via Youtube, and there someone explained complete different rules. – Jakube – 2015-01-24T21:19:58.443

"Even" means a score divisible by 2/the score is a multiple of 2 (eg. 0, 2, 4, 6 etc.). Oh, it looks like I missed out that the team that scored last serves next, sorry about that. – GiantTree – 2015-01-24T21:24:44.280