Chessboard pattern

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

Pyth, 13 chars

Note: Pyth is much too new to be eligible to win. However, it was a fun golf and I thought I'd share it.

VQ<*QX*d2N\XQ

Try it here.

How it works:

Q = eval(input())
VQ                     for N in range(Q):
<         Q                                                        [:Q]
*Q                                    (Q*                        )
X*d2N\X                                assign_at(" "*2, N, "X")

Basically, this uses X to generate "X " or " X" alternately, then repeats that string Q times, and takes its first Q characters. This is repeated Q times.

How does the X (assign at) function work? It takes the original string, " " in this case, an assignment location, N in this case, and a replacement character, "X" in this case. Since Pyth's assignments are modular, this replaces the space at location N%2 with an X, and returns the resultant string, which is therefore "X " on the first, third, etc. lines, and " X" on the others.

• ... but APL isn't. Thanks for bumping. Wonder if OP will re-accept... – Adám Feb 4 '16 at 22:02
• "Pyth is much too new to be eligible to win" I don't understand this and reaccept this one. – steenslag Feb 4 '16 at 22:25
• @steenslag To explain, there is a standard loophole that languages newer than the question are ineligible. This is to prevent languages specifically designed to do well at a specific challenge. You're free to do what you want with your challenge, of course. – isaacg Feb 4 '16 at 23:20

Golfscript - 17 chars

~:N,{"X "N*>N<n}%

Analysis

~ convert input to an int
:N store in the variable N
,{...} for each value of [0...N-1]
"X "N* repeat "X " to give a string of N*2 characters
> take the substring starting from the loop index...
N< ...ending N characters later
n put a newline a the end of each string

Perl, 41 40

for$i(1..$_){say substr" X"x$_,$i%2,$_} Perl 5.10 or later, run with perl -nE 'code' (n counted in code size) Sample output:$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<5
X X X
X X
X X X
X X
X X X

Bash: 60 characters

yes X|fmt -w80|paste -d '' <(yes '
') -|head -$1|cut -c1-$1

The table size is passed as command-line parameter, for example bash chesstable.sh 8.

Java 10, lambda function, 9287 84 bytes

n->{for(int i=0;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}

Try it online here.

Thanks to ceilingcat for golfing 4 bytes and to Kevin Cruijssen for golfing 3 more.

Ungolfed version:

n -> { // void lambda taking an int as argument
for(int i = 0; i < n*n; ) // loop over the entire square
System.out.print(((i%n + i/n) % 2 < 1 ? "X" : " ") // print an 'X' or a space depending on which line&column we're on
+ (++i % n < 1 ? "\n" : "")); // print a newline at the end of a line
}

Java 8, full program, 155 139 bytes

interface M{static void main(String[]a){int i=0,n=new Byte(a[0]);for(;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}}

Try it online here.

Ungolfed version:

interface M {
static void main(String[] a) {
int i = 0, // iterator variable for the loop
n = new Byte(a[0]); // take the argument and convert it to an int
for(; i < n*n; ) // loop over the entire square
System.out.print( ((i%n + i/n) % 2 < 1 ? "X" : " ") // print an 'X' or a space depending on which line&column we're on
+(++i % n < 1 ? "\n" : "") ); // print a newline at the end of a line
}
}
}
• @ceilingcat Thanks! I managed to shave off one more byte, and applied your approach to my full program as well. 16 bytes saved there. – O.O.Balance Apr 19 '18 at 8:34
• Doing two s+= is 2 bytes shorter than those parenthesis: n->{var s="";for(int i=0;i<n*n;s+=++i%n<1?"\n":"")s+=(i%n+i/n)%2<1?"X":" ";return s;} – Kevin Cruijssen Apr 19 '18 at 9:21
• Or 1 more byte off (84 in total) by printing directly (with the parenthesis back again xD): n->{for(int i=0;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}. Try it online. – Kevin Cruijssen Apr 19 '18 at 9:23
• Suggest "X ".charAt(i%n+i/n&1) instead of ((i%n+i/n)%2<1?"X":" ") – ceilingcat Apr 12 '19 at 17:14

Japt, 8 7 bytes

ÆîSi'XY

Run it online

7 6 bytes if we can replace X with x:

ÆîSixY

Run it online

Alternative 8 byte solution:

Æî"X "éY

Run it online

• Ah, you edited while I was in chat! Was gonna suggest the same thing. – Shaggy Mar 20 '19 at 22:58

APL (Dyalog Extended), 12 bytesSBCS

Anonymous tacit prefix function. Requires ⎕IO←0 (zero-based indexing).

'X '⊇⍨2|⍳+⍀⍳

Try it online!

ɩndices 0…n–1

+⍀ plus table with that horizontally and vertically:

ɩndices 0…n–1

2| division remainder when divided by two

'X '⊇⍨ use that matrix to index into the string

Scala, 68 bytes

def^(n:Int)=for(a<-1 to n)println(("x_"*n).substring(n%2+1,n+n%2+1))

or

def^(n:Int)=for(a<-1 to n)println(("x_"*n).substring(a%2+1).take(n))

Try it online!

Brainfuck, 140 bytes

-[+[+<]>>+]>++++[<++++++++>-]<<<<<<,[->+>+>+<<<]>>[->[->.<[->>.>]<<<]<[<<<]>>>>>[-<+>]>[-<+>]<<[->>+<<]<<[-<+>]<[->+>>+<<<]++++++++++.[-]>>]

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.

Try it online

C, 83 bytes

i,j;main(n){for(scanf("%d",&n);i++<n;puts(""))for(j=0;j<n;)putchar(i+j++&1?88:32);}

Try it online

Basic C64, 89 bytes

1 INPUTN:FORI=1TON;FORJ=1TON:IFI+JAND1THENPRINT" ";:GOTO3
2 PRINT"X";
3 NEXT:PRINT"":NEXT

• You may use the on...go to command conditionally, like ON-(I+JAND1)GOTO3:?"X";: where it is zero, it will fall through to the following statement, in this case, if (I + J AND 1) === 0 then it will print the X. This allows you to pack in more statements per line and save bytes. – Shaun Bebbers May 15 '19 at 15:58
• – ceilingcat Sep 14 '19 at 2:44

Scala, 40 and 54

The number of characters is 40 for a function, 54 for a complete program.

The solution giving only a function body is:

("X "*n)sliding n take n foreach println

Try it online

The solution giving a complete program is:

val n=readInt;("X "*n)sliding n take n foreach println

You can run it using the following command line.

scala -e 'val n=readInt;("X "*n)sliding n take n foreach println' <<< 8

where 8 is the input.

• Welcome to PP&CG and nice first answer. There's a nice site called Try It Online that allows you to get easy byte counts and share runs. Check the other Scala answer to see an example of it. It's not required mind you, just nice to have. – Veskah May 17 '19 at 15:21
• Thanks @Veskah for the suggestion. – jseteny May 17 '19 at 15:32
• Nice solution changing this to a function and using map you get 34 Characters: ("X "*n)sliding n take n map println – pme May 21 '19 at 15:23
• @pme Thanks for the suggestion, but there is no output if I replace foreach with map. However I change it to a function as you suggested. – jseteny May 23 '19 at 10:23
• you are right - sorry seems map is lazy;(. – pme May 23 '19 at 12:49

brainfuck, 249 bytes

>-[-[-<]>>+<]>->+++>-[<+>---]+>>>>>>+[[-]>,[>++++++[<-------->-]<<<[->>++++++++++<<]>+>[-<<+>>]]<]<[<+<+>>-]<<[->[->+<<<+<[<<<.>>>->-<]>[<<<.>>+>-]>>]++++++++++.[-]>[<+>>+<-]>>++<[>->+<[>]>[<+>-]<<[<]>-]>[-]>-[<<<<<<<[>+<[-]]+>[<->-]>>>>>>[-]]<<<<<]

Try it online!

Could definitely be golfed more.

C (gcc), 908883 82 bytes

-2 thanks to ceilingcat

And -5 more by ceilingcat. Iterate backwards for extra confusion!

Another -1 by ceilingcat. Iterate using negative integers for extra confusion!

m;main(n,v)int**v;{for(n*=m=~(n=atoi(v[1]));n++;)putchar(n%m?n%m-n/m&1?88:32:10);}

Try it online!

Ungolfed

m;

main(n, v) int**v;
{
for (n *= m = ~(n = atoi(v[1])); n++;)
putchar(n % m ? n % m - n / m & 1 ? 88 : 32 : 10);
}

Python - 127 characters

from sys import*
r=stdout.write
s=int(raw_input())
[[r((x+y+1)%2 and"x"or" ")for x in range(s)]and r("\n")for y in range(s)]

C - 92 86

i,j;main(n){for(scanf("%d",&n);i<n;puts(""),i++)for(j=0;j<n;j++)putchar("X "[i+j&1]);}

Q, 33

{$[1=x mod 2;x;x-1]cut(x*x)#"X "} • {(x;x-1-x mod 2)#"X "} for 22... ah no, has the same bug as yours - doesnt have 4 X's on the odd rows for input 8. – streetster May 20 '19 at 21:19 Ruby 58 i=ARGV[0].to_i 1.upto(i*i){|n|print n%i==0?"\n":' x'[n%2]} PHP - 136 chars (without whitespace) Allows for x and y function input. Also supports odd inputs now. If you style the output to have 0.65 em line-height and change this ▒█ and █░ to □■ and ■□ then it comes out looking like a real (square) chessboard. Code: function gen_cb($x,$y) {$c=0;
$y*=2; for($i=0;$i<$y;$i++){ for($j=0;$j<$x;$j++){ echo$c%2==0 ? "░█" : "█░";
}
echo "<br/>";

CJam, 18 bytes

I probably could have just ported the GolfScript answer, but here is a different approach. (And CJam is not eligible for winning anyway.)

l~,_f{f{+2%S'X?}N}

Test it here.

The idea is to iterate over the 2D grid with x and y indices on the stack, using the f{f{...}} trick. Given x and y, we can simply determine black and white as (x+y)%2 and use that to pick between the character X and a string containing a space.