44
\$\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\$
14
  • 1
    \$\begingroup\$ Trailing newline allowed? \$\endgroup\$ Commented Dec 18, 2016 at 22:02
  • 1
    \$\begingroup\$ Can the large gaps in row 2 and 8 be done using tabs? \$\endgroup\$
    – FlipTack
    Commented Dec 18, 2016 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
    Commented Dec 18, 2016 at 22:25
  • 2
    \$\begingroup\$ This is a surprisingly tricky challenge for such a short output string! I like it. :) \$\endgroup\$
    – lynn
    Commented Dec 19, 2016 at 12:33
  • 7
    \$\begingroup\$ You can just call this challenge a "Dennis court" since everybody knows who will win anyway... :) \$\endgroup\$
    – user60199
    Commented Dec 19, 2016 at 19:31

47 Answers 47

1
2
1
\$\begingroup\$

Python 2, 60 bytes

for i in 0,7,0,3,-2,3,0,7,0:print(('-?x|'[i%4]+' '*i)*9)[:9]

Try it online!

Uses Lynn's multiply-and-truncate method.

\$\endgroup\$
0
\$\begingroup\$

Lua, 105 bytes

Sure, I've already been beaten, but it ain't fun if you don't try!

Actual answer:

a,c,d="---------","xxxxxxxxx","|   |   |"p=print p(a)p("|       |")p(a)p(d)p(c)p(d)p(a)p("|       |")p(a)

Less hardcoded:

a,b,c,d=("-"):rep(9),(" "):rep(7),("x"):rep(9),"|   |   |"p=print p(a)p("|"..b.."|")p(a)p(d)p(c)p(d)p(a)p("|"..b.."|")p(a)

Uses String.rep to make it less hardcoded (though it adds bytes!) (instead of xxxxxxxxx, write ("x"):rep(9))

Just for fun, how easy would it be to just print it hardcoded?

print([[---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------]])

100 bytes. That's shorter than my code. :(

\$\endgroup\$
2
  • \$\begingroup\$ Didn't checked the other ones, but the hardcoded version can be 98 characters if you remove the parenthesis: print[[…]]. \$\endgroup\$
    – manatwork
    Commented Dec 19, 2016 at 10:16
  • \$\begingroup\$ But the 2 mirrored halves approach is of course shorter: pastebin.com/pJLFEuck \$\endgroup\$
    – manatwork
    Commented Dec 19, 2016 at 10:26
0
\$\begingroup\$

Batch, 116 bytes

@set s=---------
@for %%s in (%s% "|       |" %s% "|   |   |" %s:-=x% "|   |   |" %s% "|       |" %s%)do @echo %%~s

Conveniently, expansion of for variables happens after parsing, so I only have to quote the |s once. However, only the horizontal lines are sufficiently repetitive that using a variable saves bytes.

\$\endgroup\$
0
\$\begingroup\$

Actually, 49 bytes

'x9*' 3*;2╟"|%s|%s|"%9'-*' 7*"|%s|"%9'-*k;RdX@i(i

Try it online!

Explanation:

'x9*' 3*;2╟"|%s|%s|"%9'-*' 7*"|%s|"%9'-*k;RdX@i(i
'x9*                                               "xxxxxxxxx"
    ' 3*;2╟"|%s|%s|"%                              "|   |   |"
                     '-*' 7*"|%s|"%                "|       |"
                                   9'-*            "---------"
                                       k;          push entire stack to list, duplicate it
                                         RdX       reverse one copy and remove the string of "x"s from it
                                            @i     flatten the other copy
                                              (i   flatten the first copy and implicitly print
\$\endgroup\$
0
\$\begingroup\$

Scala, 90 bytes

val l="-"*9
val g="\n|       |\n"
val s="\n|   |   |\n"
print(l+g+l+s+"x"*9+s+l+g+l)
\$\endgroup\$
0
\$\begingroup\$

MATL, 30 bytes

9Z"13+'|'7Z"yhhyy6M5(v'x'9Y"yP

Try it online!

\$\endgroup\$
0
\$\begingroup\$

R, 121 bytes

m=rbind(a<-rep("-",9),b<-c("|",rep(" ",7),"|"),a,b,rep("x",9),b,a,b,a);m[c(4,6),5]="|";for(i in 1:9)cat(m[i,],"\n",sep="")

First time trying a Kolmogorov-Complexity challenge, not sure R is really suited for it.

Ungolfed:

a <- rep("-", 9)
b <- c("|", rep(" ", 7), "|")
m <- rbind(a, b, a, b, rep("x", 9), b, a, b, a)

m[c(4,6), 5] <- "|"

for(i in 1:9){
  cat(m[i,], "\n", sep = "")
}

Output:

---------
|       |
---------
|   |   |
xxxxxxxxx
|   |   |
---------
|       |
---------
\$\endgroup\$
3
  • \$\begingroup\$ "|", rep(" ", 7), "|" is longer than "| ​ ​ ​ ​ ​ ​ |" \$\endgroup\$
    – 12Me21
    Commented Feb 23, 2017 at 21:25
  • \$\begingroup\$ @12Me21 The code as I use it now requires a vector with each of the spaces in a separate string :/ so this wouldn't work with a direct substitution. \$\endgroup\$
    – JAD
    Commented Feb 24, 2017 at 22:15
  • \$\begingroup\$ This does make me realise though that this answer is longer than just printing the raw code. Ohwell... \$\endgroup\$
    – JAD
    Commented Feb 24, 2017 at 22:16
0
\$\begingroup\$

Scala, 83 bytes

val(a,b)=("-"*9+"\n|   ","   |\n")  
val c=s"$a $b$a|$b"  
print(c+"x"*9+c.reverse)
\$\endgroup\$
0
\$\begingroup\$

C, 97 bytes

n=11;i,k;f(){i=n/10%10;k=n++%10;printf("%c",k?"| -x"[i%2?i==5|2:k%8-1&&k-5|i*i%5-1]:10);i?f():0;}
\$\endgroup\$
0
\$\begingroup\$

///, 55 bytes

/l/---//s/   //m/
|s|s|
//a/lll
|s s|
lll/amxxxxxxxxxma

Try it online

\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 68 65 62 59 57 bytes

M$="|   
L$="-"*9?L$?M$,M$?L$?M$*3?"x"*9?M$*3?L$?M$,M$?L$

(note the 3 trailing spaces in line 1)

Of course, this will work in many other dialects of BASIC.

Explained:

V$="|   " 'vertical line
H$="-"*9 'horizontal line
PRINT H$    '---------
PRINT V$,V$ '|       |
PRINT H$    '---------
PRINT V$*3  '|   |   |
PRINT "x"*9 'xxxxxxxxx
PRINT V$*3  '|   |   |
PRINT H$    '---------
PRINT V$,V$ '|       |
PRINT H$    '---------

, in PRINT is a tab, and luckily the default size is perfect for this challenge.

\$\endgroup\$
0
\$\begingroup\$

Java 8, 98 bytes

Store repeated lines and add it all together.

()->{String a="---------\n",b="|       |\n",c="|   |   |\n";return a+b+a+c+"xxxxxxxxx\n"+c+a+b+a;}
\$\endgroup\$
0
\$\begingroup\$

C#, 88 bytes

()=>{string a="---------\n",b="|   |   |\n";return(a+="|\t|\n"+a)+b+"xxxxxxxxx\n"+b+a;};

Anonymous function which returns the tennis court as a string.

Full program with ungolfed method:

using System;

namespace LetsPlayTennis
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<string> f =
            () =>
            {
                string a = "---------\n",
                    b = "|   |   |\n";
                return (a += "|\t|\n" + a)
                    + b
                    + "xxxxxxxxx\n"
                    + b
                    + a;
            };

            Console.WriteLine(f());
        }
    }
}
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 90 86 85 81 79 bytes

(format t"~5@{~@?
~}~4@{~2:*~@?
~}"#1="~9@{-~}""|~8t|"#1#"|   |   |""~9@{x~}"1)

First we print out all arguments, and then we print them in reversed order (without xxxxxxxxx).

Similiar solution, but instead of providing every variable directly I give (where it's efficient) control string which will output needed text.

\$\endgroup\$
0
\$\begingroup\$

Pip -l, 31 bytes

"--x--"M_X9WV:" || "M_WRsX3WR'|

Try it online!

Explanation

We construct the even rows, then the odd rows, and then weave them together.

"--x--"M_X9WV:" || "M_WRsX3WR'|
                                 s is space (implicit)
       M                         Map this function
"--x--"                          to each character of this string:
        _X9                       Repeat the character 9 times
                                 The result is a list of five 9-character strings

                    M            Map this function
              " || "             to each character of this string:
                     _            The character
                      WR          wrapped on each side in
                        sX3       three spaces
                           WR'|   wrapped on each side in |
                                 The result is a list of four 9-character strings

           WV:                   Weave the two lists together (the : helps with parsing)
                                 Print, with each list element on its own line (-l flag)
\$\endgroup\$
0
\$\begingroup\$

MBASIC, 89 bytes

1 WIDTH 13:P$="---------":S$="|       |":T$="|   |   |":PRINT $S$P$T$"xxxxxxxxx"T$P$S$P$

More abuse of the WIDTH directive to save multiple PRINTs.

\$\endgroup\$
0
\$\begingroup\$

R, 76 bytes

"*"=strrep;write(c(a<-c(b<-"-"*9,"|       |",b,"|   |   |"),"x"*9,rev(a)),1)

Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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