18
\$\begingroup\$

Input: a positive number, smaller then 80, from stdin or as a command-line argument.

Output: A square chessboard pattern, the size of the input number. The dark fields are represented by the letter 'X', the white fields by a space. The top-left field should be 'X'.

A complete program is required.


Examples:

Input: 1

Output:

X

Input: 8

Output:

X X X X 
 X X X X
X X X X 
 X X X X
X X X X 
 X X X X
X X X X 
 X X X X
\$\endgroup\$
  • 1
    \$\begingroup\$ I was looking for a tag like 'light-weight' for this. \$\endgroup\$ – steenslag Mar 7 '11 at 22:52
  • 1
    \$\begingroup\$ Whole and complete program, I suppose? \$\endgroup\$ – J B Mar 7 '11 at 22:52
  • \$\begingroup\$ @J B: Yes. How do I formulate that? Add 'to stdout' to the required output? \$\endgroup\$ – steenslag Mar 7 '11 at 22:59
  • 1
    \$\begingroup\$ Just say you want a complete program. You might also want to specify command-line arguments, to prevent confusion with function arguments. \$\endgroup\$ – J B Mar 7 '11 at 23:05
  • \$\begingroup\$ When you say top-right, do you mean top-left? If not, please correct the example output for input 8. \$\endgroup\$ – Peter Taylor Mar 8 '11 at 0:02

49 Answers 49

1
\$\begingroup\$

Canvas, 6 bytes

XX\m⤢m

Try it here!

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

Japt v1.4.6 -R, 10 bytes

Æç"X " tXU

Try it

-3 bytes thanks to @Shaggy!

Æç"X " tXU   # full program
Æ            # generate a range of numbers based on input
             # and map each number through a function 
 ç"X "       # inside the function, repeat "X " a bunch of times
       tXU   # select a substring from the "X " repeated string
             # The -R flag joins the array with new lines
\$\endgroup\$
  • \$\begingroup\$ 10 bytes \$\endgroup\$ – Shaggy Mar 20 at 18:10
  • \$\begingroup\$ Nice interpreter :) \$\endgroup\$ – dana Mar 20 at 18:12
  • 1
    \$\begingroup\$ Thanks :) Drop into the chatroom if you have any feedback or feature requests. \$\endgroup\$ – Shaggy Mar 20 at 18:15
  • 1
    \$\begingroup\$ @Shaggy 8 bytes :P \$\endgroup\$ – Oliver Mar 20 at 22:41
1
\$\begingroup\$

Python 2, 47 bytes

n=input()
s='X '*n
exec"print s[:n];s=s[1:];"*n

Try it online!


Python 2, 47 bytes

n=input()
i=0
exec"i^=1;print'X'*i+n/2*' X';"*n

Try it online!

Trims trailing space.

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

C# (Visual C# Interactive Compiler), 63 bytes

x=>{for(int i=x++*x;--i>0;)Write(" X\n"[i%x<1?2:(i/x+i%x)%2]);}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ This gives an incorrect output for odd inputs. \$\endgroup\$ – Kevin Cruijssen Mar 21 at 10:28
  • \$\begingroup\$ OK - Think I got it :) Thanks for pointing that out. \$\endgroup\$ – dana Mar 21 at 10:47
1
\$\begingroup\$

PowerShell, 39 bytes

param($n)1..$n|%{' X'*$n|% S*g($_%2)$n}

Try it online!

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

Unexpanded Sinclair ZX-81/Timex TS-1000/1500 and compatibles, ~96 Tokenized BASIC bytes

 1 LET X$="X X X X X X X X X X X "
 2 INPUT I
 3 FOR X=SGN PI TO I
 4 PRINT X$( TO I)
 5 LET X$=X$(VAL "2" TO )+X$(SGN PI)
 6 NEXT X

There are some limitations here; as the Sinclair ZX-81 by default only allows 32 character per line, and only 22 lines per screen by default, I have limited the maximum "chess board" to that, 22 x 22.

I is used here as a user input variable to determine the required "size" of the chess board. Once entered into the loop, the X$ string is essentially shifted to the right by one character in line 5.

Enjoy typing!

ZX81 Chess Board Generator Simulator

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

MATL, 9 bytes

:&+o~88*c

Try it online!

ASCII c 88 multiplied * by not ~ the parity o of the broadcast self-addition &+ of the range : from 1 to the input.

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

C64/128 and C64Mini (and compatibles), 68 tonekized BASIC bytes

 0fOi=0to11:a$=a$+"V ":nE:inputi:fOx=1toi:?leF(a$,i):a$=rI(a$,21)+leF(a$,1):nE

I've limited the potential size of the chess board pattern to a width of 22 because whilst the C64 might have 40 text columns, it only has 25 rows and therefore going over 22 will mean that some of the pattern is lost because when exiting a symbolic listing the C64 with typically add a carriage return, then the ready. prompt, and another carriage return where the cursor will reside.

To explain, here is the non-obfuscated symbolic listing:

 0 for i=0 to 11
 1  let a$=a$+"V "
 2 next i
 3 input i
 4 for x=1 to i
 5  print left$(a$, i)
 6  let a$=right$(a$, 21)+left$(a$, 1)
 7 next x

First the chess board patters is built in the a$ variable with the shifted (or uppercase) v representing an X.

The user is then asked for an input of a numeric value. This will work with positive Integers with a greater value than zero, though will work also with positive floating point numbers greater or equal to one.

A for/next loop is set up to the Integer value of the number entered, printing out the pattern to the length of the number entered.

The a$ variable is then shuffled one character to ensure the chess board effect in the loop.

Enjoy programming!

Commodore C64 Chess Board pattern

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

Scala, 84 bytes

for(i<-1 to a;s=Seq.fill((a+1)/2)("X ").mkString)println(if(i%2==1)s else s.reverse)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Think you can drop a couple of the whitespaces for 80 chars... But doesn't look like this works for input of 1 \$\endgroup\$ – streetster May 20 at 21:04
  • \$\begingroup\$ @streetster thanks - this special case cost me 4 bytes;( \$\endgroup\$ – pme May 21 at 6:49
1
\$\begingroup\$

K (oK), 19 bytes

Solution:

{x$(x;x+~2!x)#"X "}

Try it online!

Explanation:

Reshape the input with an odd-length so that the X_ wraps to create the checkerboard, and then truncate to the correct width:

{x$(x;x+~2!x)#"X "} / the solution
{                 } / lambda taking implicit input x
              "X "  / string "X "
   ( ;      )#      / reshape to (length;width)
         2!x        / x mod 2
        ~           / not (0=>1, 1=>0)
      x+            / add to x
    x               / length is input
 x$                 / pad width to x
\$\endgroup\$
0
\$\begingroup\$

Haskell 89 74 charaters

main=do n<-readLn;mapM_(putStrLn.take n).take n.cycle$map cycle["X "," X"]
\$\endgroup\$
0
\$\begingroup\$

PHP, 88 80 78

for($x=0;$x++<$i=$argv[1];){for($y=0;$y++<$i;)echo($y+$x)%2?" ":"X";echo"\n";}
\$\endgroup\$
  • \$\begingroup\$ There is no opening <?php tag, would this still work? \$\endgroup\$ – Shaun Bebbers May 15 at 9:16
  • 1
    \$\begingroup\$ I'm assuming you are running it through the PHP interpreter, not as part of a web-page. \$\endgroup\$ – moteutsch May 16 at 12:39
  • \$\begingroup\$ Thanks, an explanation would be nice to add (i.e., how to run this) \$\endgroup\$ – Shaun Bebbers May 16 at 13:41
0
\$\begingroup\$

cQuents, 17 bytes

|
&"X "Tn/2,Z[1:]

Try it online!

Explanation

|
                     Separate terms on newline
 &                   Given input n, output first n terms in sequence
                     Odd terms equal
  "X "                               "X "
      *              implicit             repeated                  times
      T                                            ceiling of 
       n/2                                                    n / 2         
          ,          Even terms equal
           Z                          previous term
            [1:]                                    with the first character removed (Python slice notation)
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 10 bytes

Eθ⭆θ§X ⁺ιλ

Try it online! Link is to verbose version of code.

Explanation

Eθ           Map over first input (predefined variable). Since it's a number, maps over 0 to input (excluding upper bound) instead
   ⭆θ        String map over first input
      §  ⁺ιλ  At (cyclic) index i + l (current loop variable for inner and outer loop respectively)
       X      The string "X ". Note there is a space after the "X"
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 13 bytes

„X ׂεI∍}I∍»

Can definitely be golfed a bit more..

Try it online or verify some more test cases.

Explanation:

„X ×           # Repeat string "X " the (implicit) input amount of times
    ‚         # Pair it with its reverse
      εI∍}     # Shorten both strings to a length equal to the input
          I∍   # Extend this list of strings to a length equal to the input
            »  # Join the strings by newlines (and output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Oracle SQL, 84 bytes

SQL> select rpad(substr(rpad('_',n,'x_'),mod(level,2)+1),n,'_')from t connect by level<=n
  2  /

RPAD(SUBSTR(RPAD('_',N,'X_'),MOD(LEVEL,2)+1),N,'_')
--------------------------------------------------------------------------------
x_x_x_x_x_
_x_x_x_x_x
x_x_x_x_x_
_x_x_x_x_x
x_x_x_x_x_
_x_x_x_x_x
x_x_x_x_x_
_x_x_x_x_x
x_x_x_x_x_
_x_x_x_x_x

10 rows selected.
\$\endgroup\$
0
\$\begingroup\$

MUMPS (58 56 88 86 88 bytes)

Assuming you can execute this code in an interpreter:

(ANSI M)

  r v,! f i=1:1:v d  w !
  . f j=1:1:v w $s(i#2:$s(j#2=1:"X",1:" "),1:$s(j#2:" ",1:"X"))

We could bring this down to 82 bytes if we allow a new line at the beginning of the output:

 r v,! f i=1:1:v w ! f j=1:1:v w $s(i#2:$s(j#2=1:"X",1:" "),1:$s(j#2=1:" ",1:"X"))

Bonus

We can take advantage of the fact that M doesn't have any reserved keywords to give further headache:

s s=8 f f=1:1:s w ! f w=1:1:s w $s(f#2:$s(w#2=1:"X",1:" "),1:$s(w#2=1:" ",1:"X"))

Caché ObjectScript (a superset of ANSI M) (83 bytes):

r v,! f i=1:1:v{f j=1:1:v{w $s(i#2:$s(j#2=1:"X",1:" "),1:$s(j#2=1:" ",1:"X"))} w !}

Explanation:

both cases are essentially the same. Accept input "v" from user. While looping from 1 to v, execute an inner loop such that if i#2 isn't zero, write "X" if j#2 isn't zero and " " in all other cases (i.e. when j#2 is zero). if i#2 is zero, do the opposite. After completing the inner loop, start a new line.

dang, realized I forgot to alternate lines *added another version of ANSI M and an ObjectScript version

\$\endgroup\$
  • \$\begingroup\$ How would I run this, for example on this site: tio.run/#mumps \$\endgroup\$ – mbomb007 May 15 at 14:55
  • \$\begingroup\$ I can't figure out how to give user input in that site you mention, but if you want, you can set the size at the beginning: ` s v=8 f i=1:1:v w ! f j=1:1:v w $s(i#2:$s(j#2=1:"X",1:" "),1:$s(j#2=1:" ",1:"X"))` Though the ObjectScript version doesn't work on tio.run (proprietary to Intersystems) \$\endgroup\$ – João the Clown May 15 at 18:36
  • \$\begingroup\$ Input is given as Stdin in the input box, or passed as an argument. How is your program currently taking input? \$\endgroup\$ – mbomb007 May 15 at 19:36
  • \$\begingroup\$ In an interactive M session, Stdin is set to be the user's terminal. Something might be wrong with the setup for tio.run. w $ZIO is the translated I/O device and that throws an error. If I try to explicitly say to use Stdin via u 0 or u $Principal, I also get an error. \$\endgroup\$ – João the Clown May 15 at 20:11
  • \$\begingroup\$ output of $ZIO: *** Z function or variable not found in or near line 1 Global arrays closed. Termination message: 100 Software Error \$\endgroup\$ – João the Clown May 15 at 20:13
0
\$\begingroup\$

Jelly, 17 bytes

⁾X ẋs2UÐeẋ³ŒHḢƊ€Y

Try it online!

This almost certainly can be golfed further; 17 bytes seems like too many.

Explanation:

⁾X ẋs2UÐeẋ³ŒHḢƊ€Y    Example input: 3
⁾X ẋ                 Repeat the string "X " times the input: "X X X "
    s2               Split it into chunks of length 2: ["X ", "X ", "X "]
      UÐe            Upend (reverse) the even indexes: ["X ", " X", "X "]
              Ɗ€     Apply to each...
         ẋ³          ...repeat each chunk times the input...:  ["X X X ", " X X X", "X X X "]
           ŒH        ...approximately split it into halves...: [["X X", " X "], [" X ", "X X"], ["X X", " X "]]
             Ḣ       ...take the head (first item) of each: ["X X", " X ", "X X"]
              Y      Join with newlines
                     Implicit output
\$\endgroup\$
0
\$\begingroup\$

Stax, 6 bytes

òeφ→α♪

Run and debug it

Unpacked, ungolfed, and commented, it looks like this.

cK      map over the cartesian square of [1..n] * [1..n]
        implicitly output from each row on separate lines
  +     add the coordinates
  .X @  index into string "X "

Run this one

\$\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.