[Question]
  [
**Input:**
a positive number \$n\$, smaller than \$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. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest code in bytes wins
---
**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
```
  
[Answer]
## 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
[Answer]
# 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.](https://pyth.herokuapp.com/)
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.
[Answer]
## 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
$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<8
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
```
[Answer]
### Python, 48 Characters
```
x,i=input(),0
exec'print(x*"X ")[i:i+x];i^=1;'*x
```
[Answer]
## Python, 76 characters
```
n=input()
p='X '*n
print n/2*(p[:n]+'\n'+p[1:n+1]+'\n'),
if n&1:print p[:n]
```
[Answer]
## Scala - 141 95 characters
```
var a=args(0).toInt
for(y<-1 to a;x<-1 to a)print((if((x+y)%2<1)"X"else" ")+("\n"*(x/a)take 1))
```
Usage: `scala filename N` where n is your input to the program.
[Answer]
## APL (16)
Assuming `⎕IO=0` (i.e. zero-indexed arrays, it is a setting)
```
' X'[=/¨2⊤¨⍳2⍴⎕]
```
Explanation:
* `⍳2⍴⎕`: read a number N, and create a N×N matrix containing (0,0) to (N-1,N-1).
* `2⊤¨`: get the least significant bit of each number in the matrix. (So now we have (0,0), (0,1), (0,0)... (1,0), (1,1), (1,0)...)
* `=/¨`: for each pair, see if the two numbers are equal. (Now we have 1 0 1 0 1 0 ...)
* `' X'[`...`]`: put a space for each 0 and an X for each 1.
[Answer]
## Ruby ~~45~~ 42
```
(x=gets.to_i).times{|i|puts ("X "*x)[i,x]}
```
Demo: <http://ideone.com/Mw25e>
[Answer]
# Brainfuck, 140 bytes
```
-[+[+<]>>+]>++++[<++++++++>-]<<<<<<,[->+>+>+<<<]>>[->[->.<[->>.>]<<<]<[<<<]>>>>>[-<+>]>[-<+>]<<[->>+<<]<<[-<+>]<[->+>>+<<<]++++++++++.[-]>>]
```
[Answer]
## C++ - 253 obfuscated characters
```
#include <iostream.h>
int main(int i,char*c[]=0)
{
  char a=i,b=i>>8;i&32512?((i>>16&255)<a)?(cout<<b)?main((i^30720)+65536):0:(cout<<endl)?(((b=(i>>24)+1)<a)?main((i&2130706559)+((b&1)?16785408:16799744)):0):0:main((i>=2?atoi(1[c]):8)|22528);
}
```
[Answer]
# JavaScript, 169
```
function b(w){var i=w,j=w,r='';while(i--){while(j--){if((i+j)%2)r+=' ';else r+='X'}j=w;r+="\n"}return r}do{n=parseInt(prompt('Number'))}while(isNaN(n)||n<1);alert(b(n));
```
[Answer]
# k (26 chars)
26 For bare function:
```
{-1',/x#',x#'("X ";" X");}
```
Or a further 7 to take input from stdin
```
{-1',/x#',x#'("X ";" X");}"I"$0:0
```
[Answer]
# 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`.
[Answer]
# Java 10, lambda function, ~~92~~ ~~87~~ 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](https://tio.run/##lY9BawIxEIXP2V8xBITExa1bEKSr7cGzJy9C20PMRju6TpYkCkX2t2@z2x5swYKnYd6b983MXp3VyNaG9uWhrU@bCjXoSnkPS4UEl4QhBeO2ShtYfBh9MG5jlSs7h50tltC3Ik4BySJhTcJ@MD6oEEs/dIwwsQoOaff6DsrtvOwJv5D6uplDS6Pny9a6no3zcYEzGlIhV58@mGNmTyGrIzAIIXBAKT6QHDzO8he@5k8cuExFmkajU94oSlzKomkZi0ey61XZ9wd5dz37C69IyBuB6b2BfPJfokma9gs).
Thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat) for golfing 4 bytes and to [Kevin Cruijssen](https://codegolf.stackexchange.com/users/52210/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](https://tio.run/##FYtPC4IwFMC/yhgIWyuzTtGUoHsnL4F5GDrlGb6JvgwRP/ta19@fzszm4AaLXf32HpDs2JjKssc6kSGo2OygZr0BFDmNgG1RGrmGjkGW7DFD@2X3hawwRVJK3bhRaEhxh1rmy0S2j92H4iGcJISACBUcUUbn9HTjT37ljEsllAriT14YEJdSb5v3/vID).
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
        }
    }
}
```
[Answer]
# Japt, ~~8~~ 7 bytes
```
ÆîSi'XY
```
[Run it online](https://ethproductions.github.io/japt/?v=1.4.6&code=xu5TaSdYWQ==&input=OAoKLVI=)
**~~7~~ 6 bytes** if we can replace `X` with `x`:
```
ÆîSixY
```
[Run it online](https://ethproductions.github.io/japt/?v=1.4.6&code=xu5TaXhZ&input=OAoKLVI=)
---
Alternative **8 byte** solution:
```
Æî"X "éY
```
[Run it online](https://ethproductions.github.io/japt/?v=1.4.6&code=xu4iWCAi6Vk=&input=OAoKLVI=)
[Answer]
# [Python 2](https://docs.python.org/2/), 47 bytes
```
n=input()
s='X '*n
exec"print s[:n];s=s[1:];"*n
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P882M6@gtERDk6vYVj1CQV0rjyu1IjVZqaAoM69EoTjaKi/Wuti2ONrQKtZaSSvv/39TAA "Python 2 – Try It Online")
---
**[Python 2](https://docs.python.org/2/), 47 bytes**
```
n=input()
i=0
exec"i^=1;print'X'*i+n/2*' X';"*n
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P882M6@gtERDkyvT1oArtSI1WSkzztbQuqAoM69EPUJdK1M7T99IS10hQt1aSSvv/39TAA "Python 2 – Try It Online")
Trims trailing space.
[Answer]
# [APL (Dyalog Extended)](https://github.com/abrudz/dyalog-apl-extended), 12 [bytes](https://codegolf.meta.stackexchange.com/a/9429/43319 "When can APL characters be counted as 1 byte each?")[SBCS](https://github.com/abrudz/SBCS ".dyalog files using a single byte character set")
Anonymous tacit prefix function. Requires `⎕IO←0` (zero-based indexing).
```
'X '⊇⍨2|⍳+⍀⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qG@qp/@jtgkG/9UjFNQfdbU/6l1hVPOod7P2o94GIPU/DSj5qLcPoq6r@dB640dtE4G84CBnIBni4Rn8P03BkEtXV5crTcEUSlsAAA "APL (Dyalog Extended) – 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
[Answer]
# 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!](https://tio.run/##NcrBCoJAEADQe18xCMJMobgdIw8d@wlj1FEqmV3cCQTx2zcvXh8vdjxx8u1HOgMPsphoH@ERAqypl6FBvT3VqB78jHwvHJgHpTC/1SZFzJZXdlYq46@NtuOInF8vjkrjr6ASpaM26Cqi05b@ "Scala – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), 9 bytes
```
:&+o~88*c
```
[Try it online!](https://tio.run/##y00syfn/30pNO7/OwkIr@f9/CwA "MATL – 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.
[Answer]
## Javascript, 67 bytes
```
for(j=n=readline()|0;j--;)console.log(' X'.repeat(n).substr(j%2,n))
```
[Try it online](https://tio.run/##y0osSyxOLsosKNEts/j/Py2/SCPLNs@2KDUxJSczL1VDs8bAOktX11ozOT@vOD8nVS8nP11DXSFCXa8otSA1sUQjT1OvuDSpuASoT9VIJ09T8/9/QzMuAA)
## 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](https://tio.run/##S9ZNT07@/z9TJ8s6NzEzTyNPszotv0ijODkxL01DSTVFSUctT9M6U1vbJs@6oLSkWENJSVMTpCLL1sA6CyioCRRNzkgs0lBSiFCKztTO0tZWM4zVtK79/98CAA)
## Basic C64, 89 bytes
```
1 INPUTN:FORI=1TON;FORJ=1TON:IFI+JAND1THENPRINT" ";:GOTO3
2 PRINT"X";
3 NEXT:PRINT"":NEXT
```
[![enter image description here](https://i.stack.imgur.com/5eaOo.png)](https://i.stack.imgur.com/5eaOo.png)
[Answer]
# 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](https://tio.run/##PccxCgIxEAXQfk7x2Sqx02oRLCytbWzHZEajcTZsUgji2aMLYvV4NXDmPp1vEhqO/ChZIM8mFiv2peBFFEWhzrY4WPPYobvhhGFlvuYUk11gaHyXLzrNwuGKMidr2bq6tadfiNRt/sPS0dO7fwA)
 
**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.
[Answer]
# [brainfuck](https://github.com/TryItOnline/brainfuck), 249 bytes
```
>-[-[-<]>>+<]>->+++>-[<+>---]+>>>>>>+[[-]>,[>++++++[<-------->-]<<<[->>++++++++++<<]>+>[-<<+>>]]<]<[<+<+>>-]<<[->[->+<<<+<[<<<.>>>->-<]>[<<<.>>+>-]>>]++++++++++.[-]>[<+>>+<-]>>++<[>->+<[>]>[<+>-]<<[<]>-]>[-]>-[<<<<<<<[>+<[-]]+>[<->-]>>>>>>[-]]<<<<<]
```
[Try it online!](https://tio.run/##RY9BCsMwDAQfpCjnHIQ@InRoA4VS6KHQ97uzjqHrWNiSPNrcP7fn@/E9X2OkFys60wieZkYqCO5tOWVV3rmViqjCl9I7IspzVaQAYwkSRnZHBzSd1UsrHz2kirhDh8KTdWMuVvqP2zVafnilEgNKNolXfmJlnSvbBZJKPd78A34nVVJmlnuM4wc "brainfuck – Try It Online")
Could definitely be golfed more.
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~90~~ ~~88~~ ~~83~~ 82 bytes
-2 thanks to [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat)
And -5 more by [ceilingcat](https://codegolf.stackexchange.com/users/52904/ceilingcat). Iterate backwards for extra confusion!
Another -1 by [ceilingcat](https://codegolf.stackexchange.com/users/52904/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!](https://tio.run/##S9ZNT07@/z/XOjcxM08jT6dMMzOvREurzLo6Lb9II0/LNte2TiPPNrEkP1OjLNowVlPTOk9b21qzoLQkOSMRqEI11x6IdfP0c9UM7S0srIyNrAwNNK1r////bwEA "C (gcc) – 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);
}
```
[Answer]
## 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)]
```
[Answer]
## 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]);}
```
[Answer]
## Python
 **48 Chars**
**EDIT:** Kinda Wrong...There's an extra space at the end...but thats not visible. If you change the space to "O" (or any nonwhitespace char) then modify `[i%2:n]` to `[i%2:n+i%2]`. for the correct version.
```
n=input()
i=0;
while i<n:print('X '*n)[i%2:n];i+=1
```
[Answer]
# Q, 33
```
{$[1=x mod 2;x;x-1]cut(x*x)#"X "}
```
[Answer]
## Ruby 58
```
i=ARGV[0].to_i
1.upto(i*i){|n|print n%i==0?"\n":' x'[n%2]}
```
[Answer]
## 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/>";
$c++;
}
}
gen_cb(7,7);
```
## Output:
```
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
```
[Answer]
## PHP, 87
```
for($x=0;$x<$m=$argv[1];$x++){echo"\n";for($y=0;$y<$m;$y++)echo($y^($x%2))%2?' ':'X';}
```
]