46
\$\begingroup\$

I suddenly really want to play tennis, but alas I do not have a court!

Surprise! This is where you come in.

You have to print a tennis court for me, but you have to do it in the fewest bytes possible because of confidential reasons.

Tennis court

---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------

This is , so shortest code in bytes wins!

\$\endgroup\$
  • 1
    \$\begingroup\$ Trailing newline allowed? \$\endgroup\$ – Erik the Outgolfer Dec 18 '16 at 22:02
  • 1
    \$\begingroup\$ Can the large gaps in row 2 and 8 be done using tabs? \$\endgroup\$ – FlipTack Dec 18 '16 at 22:13
  • 3
    \$\begingroup\$ @Flp.Tkc, I don't think so. I think since nobody else in the six answers already posted knew that, it would be unfair. \$\endgroup\$ – Daniel Dec 18 '16 at 22:25
  • 2
    \$\begingroup\$ This is a surprisingly tricky challenge for such a short output string! I like it. :) \$\endgroup\$ – Lynn Dec 19 '16 at 12:33
  • 7
    \$\begingroup\$ You can just call this challenge a "Dennis court" since everybody knows who will win anyway... :) \$\endgroup\$ – RudolfJelin Dec 19 '16 at 19:31

47 Answers 47

1
\$\begingroup\$

Canvas, 13 bytes

A←⁹w5+7q⁴‟5n┼

Try it here!

\$\endgroup\$
13
\$\begingroup\$

Python 2, 65 bytes

s='-','|'+' '*7,'-','|   ','x'
for i in s+s[3::-1]:print(i*9)[:9]

Flp.Tkc saved a byte.

\$\endgroup\$
  • \$\begingroup\$ That's some crazy slicing. Nice! \$\endgroup\$ – Gurupad Mamadapur Dec 19 '16 at 14:14
  • \$\begingroup\$ You can use '|'+' '*7 for the second string in s to save a byte! \$\endgroup\$ – FlipTack Dec 19 '16 at 17:59
12
\$\begingroup\$

05AB1E, 29 27 26 bytes

'-9ש'|ð4׫Dûs®s¨¨ûû'x5×»û

Try it online!

'-9ש                      Push "---------" and store it as temporary value
     '|ð4׫Dûs             Push palindromized("|    ") = "|       |"
                           and push "|    "
              ®            Push "---------" again
               s¨¨û        Strip the last 2 characters from "|    " and push palindromized("|  ") = "|   |"
                   û       Palindromize last item -> "|   |   |"
                    'x5×   Push "xxxxx"
                        »  Join everything with newlines
                         û Palindromize the result and implicitly display it
\$\endgroup\$
7
\$\begingroup\$

Python 3 - 73 72 bytes

d=b,a,c='|       |','-'*9,'|   |   |'
print(a,*d,'x'*9,c,a,b,a,sep='\n')

Python 3.6 - 75 bytes

x=f"{'-'*9}\n|{' '*7}|\n{'-'*9}\n|   |   |\n"
print(x,'x'*9,x[::-1],sep='')

Credit goes to flp-tkc. Thank you :)

Try here!

\$\endgroup\$
6
\$\begingroup\$

///, 64 56 bytes

/f/---//e/   //a/fff
|ee |
fff
//d/|e|e|
/adxxxxxxxxx
da

Try it online!

Another 56 byte solution:

/f/---//e/|   //a/fff
e    |
fff
//d/ee|
/adxxxxxxxxx
da
\$\endgroup\$
6
\$\begingroup\$

V, 25 bytes

9é-ÄÙÒ r|$.4äGMãlr|jj.kÒX

Try it online!

This should be 23 bytes:

9é-ÄÙÒ r|$.4äMÒXãkr|jj.

But I ran into several bugs while creating it. :(

\$\endgroup\$
6
\$\begingroup\$

Jelly, 25 bytes

“¡⁵÷ḞȯḤɼ’b4ị“- x|”s5ŒBŒḄY

TryItOnline!

I had hoped to use the 1,3,1 quarter court repetition, but can't squeeze that into less (a raw version being 28: “ßṂuB’b4s3x“¢¤¢‘ị“- x|”ŒBŒḄY).

How?

“¡⁵÷ḞȯḤɼ’b4ị“- x|”s5ŒBŒḄY - Main link: no arguments
“¡⁵÷ḞȯḤɼ’                 - base 250 number: 375116358919167
         b4               - convert to base 4:
                                [1,1,1,1,1,0,2,2,2,2,1,1,1,1,1,0,2,2,2,0,3,3,3,3,3]
           ị“- x|”        - index into character list "- x|" (1-based):
                                "-----|    -----|   |xxxxx"
                  s5      - split into chunks of length 5:
                               ["-----","|    ","-----","|   |","xxxxx"]
                    ŒB    - bounce (vectorises):
                               ["---------","|        |","---------","|   |   |","xxxxxxxxx"]
                      ŒḄ  - bounce (non-vectorising version):
                               ["---------","|        |","---------","|   |   |","xxxxxxxxx","|   |   |","---------","|        |","---------"]
                        Y - join with line feeds:
                          - implicit print
\$\endgroup\$
5
\$\begingroup\$

J, 70 54 51 50 bytes

Saved a byte thanks to Zgarb!

9 9$'-| |-| | |x| | |'#~3 9 1 7{~4#.inv 1851926050

Some standard compression technique, using a compressed RLE.

   9 9$'-| |-| | |x| | |'#~3 9 1 7{~4#.inv 1851926050
---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------
\$\endgroup\$
  • \$\begingroup\$ Reorder and save a byte: 9 9$'-| |-| | |x| | |'#~3 9 1 7{~4#.inv 1851926050 \$\endgroup\$ – Zgarb Dec 19 '16 at 10:14
  • \$\begingroup\$ @Zgarb ah, nice! \$\endgroup\$ – Conor O'Brien Dec 19 '16 at 12:00
4
\$\begingroup\$

Ruby, 60 bytes

-4.upto(4){|i|puts''.rjust(9,%w{x |%3s - |%7s -}[i.abs]%'')}

Ungolfed

-4.upto(4){|i|                    #iterate -4 to 4.
   puts''.rjust(9,                #Print the null string right justified to length 9 padded by repititions of
   %w{x |%3s - |%7s -}[i.abs]%'') #the string from the %w{} array modified like sprintf by the % operator
}                                 #so that %Ns is replaced by N spaces.
\$\endgroup\$
4
\$\begingroup\$

bash/Unix tools, 58 57 bytes

dc<<<4o16i3FFFFp20002prp20202p15555pszpszfp|tr 0-3 ' x|-'

Only four characters other than newline appear in the desired output, allowing each line to be coded as a base-4 number. These numbers are then written in hexadecimal in the script, for brevity.

The Unix calculator dc is used both for the base-16 to base-4 conversion and for stack manipulation to allow easy repetition of lines in the right places.

Edit: Shaved one byte off by swapping two of the base-4 digits used to code characters, permitting the tr command to be one byte shorter than the previous version.

\$\endgroup\$
4
\$\begingroup\$

JavaScript, 85 bytes

console.log('---|  ---| |xxx| |---|  ---'.replace(/(.)((.).)/g,'$1$3$3$2$3$3$3$1\n'))
\$\endgroup\$
4
\$\begingroup\$

JavaScript (ES6), 86 84 83 81 bytes

Saved 2 bytes, thanks to Neil

a=`---------
`;a+=`|       |
`+a;b=`|   |   |
`;console.log(a+b+`xxxxxxxxx
`+b+a)

Alternate method #1, 93 bytes

console.log('01232101242421512424210123210'.replace(/./g,n=>`-
|  x`[n].repeat('911739'[n])))

Alternate method #2, 86 bytes

Suggested by Neil:

console.log(`-${s=`
       |
-
   |
`}x${s}-`.replace(/.*/g,s=>s.repeat(9).slice(-9)))

Alternate method #3, 91 bytes

A recursive approach:

console.log((f=(n,c=`xxxx
|   |   |
---------
|       |
`[n]||'-')=>~n--?c+f(n)+c:'x')(43))
\$\endgroup\$
  • \$\begingroup\$ You can make this a function to save a few bytes. \$\endgroup\$ – Conor O'Brien Dec 19 '16 at 2:28
  • \$\begingroup\$ @ConorO'Brien I'm not sure it's allowed to just return the output here: You have to print a tennis court \$\endgroup\$ – Arnauld Dec 19 '16 at 9:50
  • \$\begingroup\$ Simple run length encoding is a byte shorter than your alternate method: console.log('-9\n| 7|\n-9\n| 3| 3|\nx9\n| 3| 3|\n-9\n| 7|\n-9'.replace(/.\d/g,s=>s[0].repeat(s[1]))) (replace \n with newline obviously). \$\endgroup\$ – Neil Dec 19 '16 at 10:23
  • \$\begingroup\$ Another 92-byte formulation: console.log('-\n |\n-\n |\nx\n |\n-\n |\n-'.replace(/.*/g,s=>s.repeat(9).slice(-9))). \$\endgroup\$ – Neil Dec 19 '16 at 10:28
  • \$\begingroup\$ Sorry, there are supposed to be 7 or 3 spaces before each | as appropriate instead of the single space that appears in my comment. \$\endgroup\$ – Neil Dec 19 '16 at 10:34
4
\$\begingroup\$

SOGL 0.5, 24 23 20 bytes (non-competing)

 -9*"D√⅜‘⁴"Hοr‘¹q x9*o±

Explanation:

"'|⁶.f,‘9n_h¹q x9*o±
"'|⁶.f,‘              pushes "------------------|       ||   |   |"
        9n            splits into chunks of 9
                       stack: [["---------","---------","|       |","|   |   |"]]
          _           puts all the contents of the array into the stack
                       stack: ["---------","|       |","---------","|   |   |"]
           h          swaps the 2nd from top with 3rd from top
            ¹         turns back into array
                       stack: [["---------","|       |","---------","|   |   |"]]
             q        outputs the array without popping it from stack
               x      push "x"
                       stack: [["---------","|       |","---------","|   |   |"], "x"]
                9*    repeat it 9 times
                       stack: [["---------","|       |","---------","|   |   |"], "xxxxxxxxx"]
                  o   output that
                   ±  reverse the array
implicitly output the reversed array

So how does the compressed string work?

The string converted from base250 to binary is 1000000100111110010100001110100000001100010001

and approxametally what it does:

100 boxstring (a custom dictionary string with chars " -/\|_\n")
000010 choose only the "-"s
01111 with length of 15+(different chars used)+2 = 18 chars
there is only 1 char type, so results in "------------------"
100 another boxsting
101000 choosing " " and "|"
01110 with length of 14+(different chars used)+2 = 18 chars
100000001100010001 the data; 0 = " " and 1 = "|"
has "------------------" and "|       ||   |   |" and just joins them together

non-competing, because language postdates the challenge. And this challenge is only a day old. I knew I should've put something up yesterday. I did this as a test for my language and it ended up too good not to post. Pretty sure this is golfable more too.

\$\endgroup\$
3
\$\begingroup\$

///, 59 bytes

/_/---------
//s/   /_|ss |
_|s|s|
xxxxxxxxx
|s|s|
_|ss |
_

Try it online!

Outgolfed...

\$\endgroup\$
3
\$\begingroup\$

Javascript (ES6), 86 bytes:

a=`---------
|       |
---------`;b=`|   |   |
`;console.log(a+`
`+b+`xxxxxxxxx
`+b+a)

Test here:

a=`---------
|       |
---------`;b=`|   |   |
`;console.log(a+`
`+b+`xxxxxxxxx
`+b+a)

\$\endgroup\$
  • \$\begingroup\$ You can save 4 bytes by adding a newline at the start of b, as this means that you can remove the newline after xxxxxxxxx and the one that you add between a and b. You can save a further 3 bytes by setting a to --------- first and then concatenating the second row and a to itself. \$\endgroup\$ – Neil Dec 19 '16 at 10:15
3
\$\begingroup\$

05AB1E, 26 bytes

'-5ש'|4úD®sÂðñ)í€û»'x5×»û

Uses the CP-1252 encoding. Try it online!

\$\endgroup\$
3
\$\begingroup\$

PHP, 66 62 bytes

This is the original answer (66 bytes):

<?=$a="---------
|       |
---------
|   |   |
xxxx",x,strrev($a);

It generates a notice because of the unknown constant x. The notice can be suppressed by setting error_reporting=0 in php.ini or in the command line:

$ php -d error_reporting=0 tennis.php

The last line of the output doesn't end with a newline character.


The updated answer (62 bytes), improving an improvement suggested by @Titus:

<?=$a=($b="---------
|   ")."    |
$b|   |
xxxx",x,strrev($a);

Run it without a configuration file (it defaults with error_reporting=0 this way):

$ php -n tennis.php

Both versions of the code contain literal new lines embedded in the string (1 byte shorter than \n) and cannot we unwrapped.

\$\endgroup\$
  • 1
    \$\begingroup\$ Notices are turned off in the default config; no need for -d error_reporting=0, just use -n. \$\endgroup\$ – Titus Dec 21 '16 at 6:19
  • \$\begingroup\$ I count 66. You can make them 65 with <?=$a=($b="---------\n|").".... \$\endgroup\$ – Titus Dec 21 '16 at 6:32
  • \$\begingroup\$ @Titus I like your suggestion. -n is much shorter than -d error_reporting=0 :-) There are, indeed, only 66 useful chars in the answer. I counted them using ls -l and forgot my vi is configured to ensure the file ends with a new line. I improved your improvement and squeezed 3 more bytes. Thank you. \$\endgroup\$ – axiac Dec 21 '16 at 8:21
2
\$\begingroup\$

PHP, 72 bytes

<?=$a="---------
",$b="|       |
$a",$c="|   |   |
","xxxxxxxxx
$c$a$b";

I almost hate it when it´s shorter like this than with calculating a little.

\$\endgroup\$
2
\$\begingroup\$

Ruby, 52 bytes

puts [b=[a=?-*9,"|%8s"%?|,a],c="|   |   |",?x*9,c,b]

Using the double simmetry, the lines are: 0-1-0 / 2-3-2 / 0-1-0, the loop can be easily unrolled and the nested list is flattened on output.

\$\endgroup\$
2
\$\begingroup\$

Pyke, 28 26 25 bytes

\|ddsssd7*.X--||"R\x5*nJs

Try it here!

       d7*                -    " " * 7
          .X--||"         -   grid(^, corners="-", sides="-", left="|", right="|")
\|dds                     -     "|  "
     s                    -    palindromise(^)
      s                   -   palindromise(^)
                  \x5*    -   "x" * 5
                      nJ  -  "\n".join(^)
                        s - palindromise(^)
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 25 bytes

'-9ש'|ð4׫û®…|  ûû'x5×»û

Uses the CP-1252 encoding. Try it online!

Explanation:

'-9×                       # Push "---------"
    ©                      # Store in register_c
     '|ð4׫                # Push "|    "
           û               # Palindromize, giving "|       |"
            ®              # Retrieve from register_c
             …|            # Push "|  "
                 ûû        # Palindromize twice, giving "|   |   |"
                   'x5×    # Push "xxxxx"
                       »   # Join by newlines
                        û  # Palindromize
                           # Implicit print
\$\endgroup\$
2
\$\begingroup\$

Vim, 32 bytes

9i-^[Y3pO||^[7i ^[YGPkP4lr|YpO^[9ix^[

This will print the tennis court into a vim buffer. ^M represents the Enter key (0x0d) and ^[ is the Escape key (0x1b). You can run these keystrokes/code by saving them to a file and running

vim -s <filename> -u NONE

Printing to stdout

If it has to be printed to stdout instead, you could save the buffer to a file (I used "a") and use whichever shell vim is set to use (I used bash) as well as the cat program in order to print the tennis court to stdout (51 bytes):

9i-^[Y3pO||^[7i ^[YGPkP4lr|YpO^[9ix^[:w!a|sil !cat %^M:q^M

It's the same as the earlier version but with :w!a|sil !cat %^M:q^M added onto the end

\$\endgroup\$
2
\$\begingroup\$

J, 36 bytes

9$'-'([,(' '4}]),[,],'x',:])9$'|   '

This works on the REPL, which is the standard way of using J:

   9$'-'([,(' '4}]),[,],'x',:])9$'|   '
---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------

With 41 bytes, I can print the result to STDOUT:

echo 9$'-'([,(' '4}]),[,],'x',:])9$'|   '

Try it online!

Explanation

I construct the tennis court one row at a time.

9$'-'([,(' '4}]),[,],'x',:])9$'|   '
                            9$'|   '  The string repeated to length 9: y = '|   |   |'
  '-'(                     )          Apply this verb to x = '-' and y:
                     'x',:]            y with a row of 'x'-chars above it.
                                       This is a 2x9 matrix, and "," now works by
                                       prepending new rows to it.
                   ],                  Prepend another y.
                 [,                    Prepend x, which is repeated to a row of length 9.
        (' '4}]),                      Prepend y with 4th character replaced by a space.
      [,                               Prepend x again.
                                      Now we have this 6x9 matrix:
                                       ---------
                                       |       |
                                       ---------
                                       |   |   |
                                       xxxxxxxxx
                                       |   |   | 
9$                                    Repeat it to have 9 rows.
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 67 66 bytes

($a='-'*9)
($b="|       |")
$a
($c="|   "*2+"|")
'x'*9
$c
$a
$b
$a

Try it online!

Just some string multiplication, setting variables, and ensuring they're encapsulated in parens to place copies on the pipeline. The default Write-Output at program completion gives us newlines between for free.

Thanks to @ConnorLSW for saving an obvious byte.

\$\endgroup\$
  • \$\begingroup\$ $(' '*7) is actually 8 chars, would be shorter to just do | | as spaces. \$\endgroup\$ – colsw Feb 24 '17 at 11:27
  • \$\begingroup\$ @ConnorLSW Oh, haha. Thanks for the obvious golf! :D \$\endgroup\$ – AdmBorkBork Feb 24 '17 at 13:25
  • \$\begingroup\$ just paying you back for the ToLower() save on my other answer ;) \$\endgroup\$ – colsw Feb 24 '17 at 13:48
1
\$\begingroup\$

Python 2, 75 bytes

a,b,c='-'*9,'|       |','|   |   |'
for e in[a,b,a,c,'x'*9,c,a,b,a]:print e

Uses variables borrowed from @GurupadMamadapur

Alternative also for 75

a,b,c='-'*9,'|       |','|   |   |'
print'\n'.join([a,b,a,c,'x'*9,c,a,b,a])
\$\endgroup\$
1
\$\begingroup\$

Emacs, 43 35 keystrokes

M-9 x RET : nine x's, return
C-SPC : set mark
| M-3 SPC | M-3 SPC | RET : pipe, three spaces, pipe, three spaces, pipe, return
M-9 - RET : nine hyphens, return
| M-7 SPC | RET : pipe, seven spaces, pipe, return
M-9 - RET : nine hyphens, return
C-x C-x : exchange point and mark, selecting region
M-w : copy region
C-p : previous line
C-y : yank copied text
M-x rev-r RET : execute reverse-region command

\$\endgroup\$
1
\$\begingroup\$

Lua, 82 Bytes.

y=[[---------
|       |
---------
]]z="|   |   |\n"io.write(y,z,"xxxxxxxxx\n",z,y)

I tried many methods, and yet this one proved the victor.

\$\endgroup\$
1
\$\begingroup\$

Pushy, 33 bytes

This question had 33 upvotes, and there were 33 answers, so I just had to post a 33 byte solution...

9:45;T`|    `wT`|   |`4dT5:120;w"

Try it online!


Explanation

The code can be split into several parts, to make it easier to understand. The first part works like so:

9:45;    \ Push the char '-' 9 times
T        \ Push 10, which is a linefeed
`|    `  \ Push these charcodes

The stack is now:

---------
|

The mirror operator, w, then mirrors the whole stack, producing:

---------
|       |
---------

Then:

T        \ Push a linefeed
`|   |`  \ Push these chars
4d       \ Copy the last 4 characters
T        \ Push a linefeed
5:120;   \ Push the char 'x', 5 times

The stack is now beginning to look like the tennis court:

---------
|       |
---------
|   |   |
xxxxx

To finish it, we use the mirror operator w once more, which reflects this string to produce the full tennis court.

---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------

All that's left now is to print, which is done by the " character.

\$\endgroup\$
1
\$\begingroup\$

Unix Shell; using dc and tr; 55 Bytes: ( Optimization of Mitchell Spector solution )

dc<<<4o16i3FFFFp20002prp20202p15555psrfrp|tr 0-3 ' x|-'

Others Solutions : Using sed; 81 Bytes;

echo "---|  ---| |xxx| |---|  ---"|sed "s/\(.\)\(.\)\(.\)/\1\2\2\2\3\2\2\2\1\n/g"

Using dc in function : 88 Bytes

b(){ dc<<<2o16i1${1}p|tr '01' ' '$2;};(b FF -;b 01 \|;b FF -;b 11 \|)>f;cat f;b FF X;tac f

or

b(){ echo "obase=2;$1"|bc|tr '01' ' '$2;};(b 511 -;b 257 \|;b 511 -;b 273 \|)>f;cat f;b 511 X;tac f 

Using bc in function : 99 Bytes

b(){ echo "obase=2;$1"|bc|tr '01' ' '$2;};(b 511 -;b 257 \|;b 511 -;b 273 \|)>f;cat f;b 511 X;tac f 
\$\endgroup\$
1
\$\begingroup\$

Powershell, 56 bytes

(('-'*9),'|       |'+'-'*9+'|   |   |'+'x'*9)[0..4+3..0]

Explanation: straightforward half-of-the-court

Alternative, 68 bytes

('-----','|    '+'-'*5+'|   |'+'x'*5)[($r=0..4+3..0)]|%{-join$_[$r]}

Explanation: quarter-of-the-court uses the same indexes for both row and column display

\$\endgroup\$

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.