9
\$\begingroup\$

Introduction:

I saw there was only one other badminton related challenge right now. Since I play badminton myself (for the past 13 years now), I figured I'd add some badminton-related challenges. Here the second (first one can be found here):

Challenge:

Some rules about badminton serves:

  • A serve will always be done diagonally over the net.
  • You must always serve after the line that's parallel and nearest to the net.
  • The area in which you are allowed to serve differs depending on whether it's a single (1 vs 1) or double/mix (2 vs 2).
    • For singles (1 vs 1), the blue area in the picture below is where you are allowed to serve. So this is including the part at the back, but excluding the parts at the side.
    • For doubles/mix (2 vs 2), the green area in the picture below is where you are allowed to server. So this is excluding the part at the back, but including the parts at the side.
  • You may not stand on the lines when serving. But the shuttle will still be inside if they land on top of a line.

Here the layout of a badminton field:
enter image description here

Challenge rules:

Input:

You will be given two inputs:

  • Something to indicate whether we're playing a single or double/mix (i.e. a boolean)
  • Something to indicate which block you're serving from (i.e. [1,2,3,4] or ['A','B','C','D'] as used in the picture above).

Output:

Only the relevant lines for the current serve (including the net), including an F to indicate where you serve from, and multiple T to indicate where you will potentially serve to.

Although in reality you're allowed to serve from and to anywhere in the designated areas, we assume a person that will serve will always stands in the corner of the serve area closes to the middle of the net, which is where you'll place the F. And they will serve to any of the four corners of the area where they have to serve to, which is where you'll place the Ts.
As ASCII-art, the entire badminton field would be the following (the numbers are added so you don't have to count them yourself):

 2        15              15        2

+--+---------------+---------------+--+
|  |               |               |  |    1
+--+---------------+---------------+--+
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |    9
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
+--+---------------+---------------+--+
|  |               |               |  |    2
|  |               |               |  |
O=====================================O    37 times '='
|  |               |               |  |
|  |               |               |  |    2
+--+---------------+---------------+--+
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |    9
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
|  |               |               |  |
+--+---------------+---------------+--+
|  |               |               |  |    1
+--+---------------+---------------+--+

Examples:

Here two examples for outputting only the relevant parts of the serve:

Input: Single and serve block A
Output:

                   T---------------T   
                   |               |   
                   +---------------+   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   |               |   
                   T---------------T   
                   |               |    
                   |               |    
O=====================================O
   |               |                     
   |               |                     
   +---------------+                   
   |              F|                   
   |               |                   
   |               |                   
   |               |                   
   |               |                   
   |               |                   
   |               |                   
   |               |                   
   |               |                   
   +---------------+                   
   |               |                   
   +---------------+                   

As you can see, the F is added in the corner within the block, but the T are replacing the + in the ASCI-art output.


Input: Double and serve block C
Output:

                                       

+--+---------------+                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |               |                   
|  |              F|                   
+--+---------------+                   
|  |               |                   
|  |               |                   
O=====================================O
                   |               |  |
                   |               |  |
                   T---------------+--T
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   |               |  |
                   T---------------+--T

                                       

Challenge rules:

  • Leading and trailing newlines are optional (including the leading and trailing two empty lines when the input is single). Trailing spaces are optional as well. Leading spaces are mandatory however.
  • Any four reasonable distinct inputs to indicate which block we're serving from are allowed (for integers, stay within the [-999,999] range); as well as any two reasonable distinct inputs to indicate whether its a single or double/mix (please note this relevant forbidden loophole, though). Please state the I/O you've used in your answer!
  • You are allowed to use a lowercase f and t (or mixed case) instead of F and T.
  • You are allowed to return a list of lines or matrix of characters instead of returning or printing a single output-string.

General rules:

  • This is , so shortest answer in bytes wins.
    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.
  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
  • Default Loopholes are forbidden.
  • If possible, please add a link with a test for your code (i.e. TIO).
  • Also, adding an explanation for your answer is highly recommended.
\$\endgroup\$
  • \$\begingroup\$ Ah, badminton. The one game I’ve always wanted to play but never got around to because forgot/couldn’t find players to play with \$\endgroup\$ – Quintec Apr 1 at 11:59
  • \$\begingroup\$ @Quintec Feel free to come visit our club in The Netherlands for a free evening ;p \$\endgroup\$ – Kevin Cruijssen Apr 1 at 12:20
  • 1
    \$\begingroup\$ Deal, if you pay for my plane ticket and hotel? :) \$\endgroup\$ – Quintec Apr 1 at 13:13
  • 1
    \$\begingroup\$ @Quintec If you win, I'll pay the flight ticket back, haha xD \$\endgroup\$ – Kevin Cruijssen Apr 1 at 13:21
  • 1
    \$\begingroup\$ @MagicOctopusUrn Yes, there are some professional badminton players from The Netherlands. Not sure what rank they have on the world list tbh, I don't watch badminton that often (and usually it's only 5 minutes on the sport news if mentioned at all anyway.. all other time is wasted with soccer). And no, as top player you might barely make an income I think. Definitely not millions. \$\endgroup\$ – Kevin Cruijssen Apr 2 at 6:59
3
\$\begingroup\$

Python 2, 285 284 bytes

R=str.replace
s,q=input()
A=' '*19
l='| '[s]+'  |'+A[4:]+'|'+A
r=['T--+','   T'][s]+'-'*15+'T'+A
h=[r]+[l,R(r,*'T+')]*s+[l]*8+[l[:18]+'F'+'|'+A,r,l,l,'O'+'='*37+'O']
h+=[R(l[::-1],*'T+')for l in h[-2::-1]]
h[9+2*s]=R(h[9+2*s],*'F ')
for l in[l[::q%2*2-1]for l in h[::q/2*2-1]]:print l

Try it online!

Takes input as 0/1 (or False/True) for game type (Double/Single),

and 0-3 for serving block (0,1,2,3 = C,D,A,B)

\$\endgroup\$
  • \$\begingroup\$ That was fast! Nice answer. \$\endgroup\$ – Kevin Cruijssen Apr 1 at 8:20
  • 1
    \$\begingroup\$ @Yeah, I kinda tried it from sandbox last week :P \$\endgroup\$ – TFeld Apr 1 at 8:21
2
\$\begingroup\$

Charcoal, 81 bytes

NθF⮌Eθ⁺¹⁶׳ιF✂541⊖θURι±×³Iκ×=¹⁸O⟲O↙⁴J¹±³FF²F²«J×ι±⁺¹²×³θ⁺²×⁻¹⁵׳θκT»F№ABη‖↑F№ACη‖

Try it online! Link is to verbose version of code. Takes the first input as 1 or 2 for singles or doubles, second input as one of ABCD as in the question. Explanation:

F⮌Eθ⁺¹⁶׳ιF✂541⊖θURι±×³Iκ

Loop over the relevant widths and the heights of the D court and draw the rectangles.

×=¹⁸O⟲O↙⁴

Draw the net and apply rotational symmetry to add the A court.

J¹±³F

Add the F to the D court.

F²F²«J×ι±⁺¹²×³θ⁺²×⁻¹⁵׳θκT»

Add the Ts to the relevant places in the A court.

F№ABη‖↑F№ACη‖

Reflect the output as necessary to serve from the correct court.

\$\endgroup\$
  • 1
    \$\begingroup\$ @KevinCruijssen Sorry for overlooking that, should be fixed now, thanks. \$\endgroup\$ – Neil Apr 2 at 8:32
1
\$\begingroup\$

JavaScript (ES7),  216 ... 205 201  199 bytes

Takes input as (block)(double), where block is either \$-2\$ (top right), \$-1\$ (bottom left), \$1\$ (bottom right) or \$2\$ (top left) and double is a Boolean value.

b=>d=>(g=x=>y<31?`+-| =OTF
`[X=x-19,Y=y-15,p=X*Y*b<0,q=Y>0^b&1,X*=X,Y*=Y,i=x*24%35>2|~16>>Y%62%6&2,x<39?Y?p*X|(d?Y:X-87)>169?3:i?X-1|Y-16|q?i:7:q*(d?X-87:Y)%169&&6:x%38?4:5:++y&&8]+g(-~x%40):'')(y=0)

Try it online!

Formatted version

How?

We iterate from \$y=0\$ to \$y=30\$ and from from \$x=0\$ to \$x=39\$ for each value of \$y\$.

We first define \$X=x-19\$ and \$Y=y-15\$.

The variables p = X * Y * b < 0 and q = Y > 0 ^ b & 1 are used to determine what do draw in each quarter according to the block \$b\$.

From now on, both \$X\$ and \$Y\$ are squared in order to easily test absolute positions within each quarter of the field.

The expression x * 24 % 35 > 2 yields false if \$x\$ belongs to \$\{0, 3, 19, 35, 38\}\$ (the positions of the vertical lines) or true otherwise.

Try it online!

The expression ~16 >> Y % 62 % 6 & 2 yields \$0\$ if \$y\$ belongs to \$\{0, 2, 12, 18, 28, 30\}\$ (the positions of the horizontal lines, excluding the net) or \$2\$ otherwise.

Try it online!

The variable \$i\$ is defined as the result of a bitwise OR between the two values above, and is therefore interpreted as:

  • 3: space
  • 2: |
  • 1: -
  • 0: + or T

The expression (d ? Y : X - 87) > 169 is used to crop the field according to the game type \$d\$ (single or double). The similar expression (d ? X - 87 : Y) % 169 is used to draw the T's at the appropriate positions.

\$\endgroup\$
  • \$\begingroup\$ *Opens TIO and starts verifying the output* Looks good; all eight outputs are correct, as expected. *Looks at actual code* Huh.. wth is going on¿.. :S Looking forward to that explanation later on, @Arnauld. An unexpected amount of arithmetic, ternary, and bitwise calculations for an ASCII-art challenge. xD \$\endgroup\$ – Kevin Cruijssen Apr 1 at 17:29
  • 1
    \$\begingroup\$ @KevinCruijssen Actually, I wish my formulas were even more freaky so that I could get this under 200 bytes, which was my initial target. ;) But my approach is probably too much optimized towards drawing the full field and not enough towards taking the parameters into account at a reasonable byte cost. \$\endgroup\$ – Arnauld Apr 2 at 8:51
  • \$\begingroup\$ Probably yeah, since I allowed any input-value in the range [-999,999] for the four distinct inputs, so perhaps you could somehow use that to your advantage to golf some bytes. It would mean to partially start over, which is perhaps not be worth the effort. Unfortunately I can't give you any golfing tips to help you under 200 bytes; I can only wish you good luck in your attempts. ;p \$\endgroup\$ – Kevin Cruijssen Apr 2 at 9:02
  • \$\begingroup\$ @KevinCruijssen Done. :) \$\endgroup\$ – Arnauld Apr 2 at 10:17
1
\$\begingroup\$

Jelly, 108 99 bytes

“¢¥Þ‘Ṭ+þ³ḤN+“¢¤€‘¤ṬḤ;Ø0¤×3R¤¦€³+0x39¤µ‘03³?‘;20¤¦€1,-2¦;565DWx“¢%¢‘¤;UṚ$ị“|-+TO= ””F21¦€³Ḥ_⁵¤¦UṚƭ⁴¡

Try it online!

I’m sure this can be better golfed.

Dyadic link with left argument 0 or 1 for singles/doubles and right argument 0,1,2,3 for different serve quadrants. Returns a list of strings

Thanks to @KevinCruijssen for saving a byte!

\$\endgroup\$
  • \$\begingroup\$ I don't know Jelly, so I probably say something stupid here, but with “|-+TO= ”“F”, can't the “F” be golfed to another type of string for single characters? In 05AB1E for example, there are builtins for strings of size 1 ('), 2 (), or 3 (), so it could be 'F. Don't know if Jelly has something similar, or if you have another reason for it to be “|-+TO= ”“F”? \$\endgroup\$ – Kevin Cruijssen Apr 2 at 8:51
  • 1
    \$\begingroup\$ @KevinCruijssen Thanks, and nice challenge. I don’t think so. There are two character literals (with ⁾), but not one. I could use a number 7 and add the F to the lookup, but it’s the same number of characters because of the need to follow the 7 with a 21 which therefore needs a space to separate the two. \$\endgroup\$ – Nick Kennedy Apr 2 at 13:13
  • \$\begingroup\$ Well, as I said, I don't know Jelly. Thought it might have some builtins for 1 or 2-character strings as well, but if you say not I believe you. :) \$\endgroup\$ – Kevin Cruijssen Apr 2 at 13:15
  • \$\begingroup\$ @KevinCruijssen I’m happy for someone else to jump in - still learning! \$\endgroup\$ – Nick Kennedy Apr 2 at 13:15
  • 1
    \$\begingroup\$ @KevinCruijssen I completely missed that could be used for a single character literal - oops! Thanks for saving a byte. \$\endgroup\$ – Nick Kennedy Apr 10 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.