# Lua: Render a chess board in smallest code

I'm creating a simple challenge here as a first test of the community's reactions. Please let me know if this challenge is the kind of thing you guys like or what I can improve... :)

The task is this: Render a chess board in Lua in smallest code possible.

Here is an example solution: http://tinybrain.de/145

As you can see, there is definitely room for getting smaller than 657 bytes (# of bytes are printed on the page). All the information you need for coding should be on the page or, hopefully, on tinybrain.de. Mailed questions totally welcome too. Lua API overview: http://tinybrain.de:8080/tb/debug.php?cmd=144

I am not demanding your chess board to have the exact same colors or even the same size. It should look like a chess board, that's all.

You need to register on TinyBrain.de to submit and test code. (Sorry, hope that is OK... It's free and I'm not a data peddler, so no harm there... :)

Edit: For testing your code locally (without any registration or Internet connection), you can download the TinyBrain runtime (magic.jar). Here is the link: http://tinybrain.de:8080/magic.jar

• Does it have to be rendered as an image? Your question doesn't specify that. – Cel Skeggs Jan 12 '14 at 21:38
• Yes, that was the original idea... What's the alternative? Render it as ASCII? Well, why not. We can collect those solutions too. :) In that case, just use print() statement(s)! – Stefan Reich Jan 12 '14 at 21:52
• Could you bring in the examples from tinybrain.de and make the source code for magic.jar available (I am a bit hesitant to run unkown binaries on my machine that I've downloaded from the internet). – user12166 Feb 16 '16 at 19:54

For a fancy ASCII art solution:

print("/"..("-"):rep(32).."\\")for y=0,15 do print("|"..("....    "):rep(5):sub(1+(y-y%2)*2%8):sub(1,32).."|")end print("\\"..("-"):rep(32).."/")


At 145 characters.

For a small ASCII art solution:

for i=1,8 do print(("# "):rep(9):sub(i,i+7))end


At 47 characters.

• Great job :) Is it the shortest ASCII version? Using "rep" is pretty smart! – Stefan Reich Jan 12 '14 at 22:26
• (Typo in the second solution gives a ninth row.) – breadbox Jan 12 '14 at 22:42
• @Stefan I'm unsure if it's the shortest solution, but it's probably close. – Cel Skeggs Jan 12 '14 at 22:44
• @breadbox fixed. – Cel Skeggs Jan 12 '14 at 22:48

Starting with col6y's "small ASCII" solution (47):

for i=1,8 do print(("# "):rep(9):sub(i,i+7))end


sometimes brute force is better (43):

for i=1,4 do print("# # # # \n # # # #")end


:)

• You don't really need the trailing space after the newline, and Lua has sugar for calling functions with a string literal: print"# # # #\n # # # #". Though, you could save yet more chars anyway by using string.rep instead: print(("# # # #\n # # # #\n"):rep(4)) clocks in at 37. – FireFly Jan 17 '14 at 12:02
• Nice. Wish I'd noticed that. – Cel Skeggs May 9 '15 at 20:21

## 38

Combining 14921's and col6y's ideas:

print(("# # # # \n # # # #\n"):rep(4))


## 83 characters

This snippet takes advantage of the description's offer to omit one of the dimensions, and to use a single integer to represent the pixel color.

w=256
p={}
for i=0,65535 do p[i+1]=5595493-((i-i%32)/32+(i-i%8192)/8192)%2*26214 end


(The character count doesn't include the newline on line 2, which I've included solely for legibility.)

The logic is pretty straightforward. The expression (i-i%32)/32 is equivalent to math.floor(i/32) -- in other words, integer division. Written in C's bitwise operators (which Lua lacks), the logic would look like this:

p[i] = (((i >> 5) ^ (i >> 13)) & 1) ? dark : light


This solution should produce identical output as the example given in the description.

You can shave off several characters by changing the colors to pure black and white, assuming -1 can be used to represent the white color:

w=256
p={}
for i=0,65535 do p[i+1]=((i-i%32)/32+(i-i%8192)/8192)%2-1 end


If I also change the size to be 64x64 (which the description permits), I can bring it down to 65 characters:

w=64
p={}
for i=0,4095 do p[i+1]=((i-i%8)/8+(i-i%512)/512)%2-1 end


However, the description doesn't seem to have any objective limits as to the chessboard's appearance. (Rookie mistake.) Let's go ahead and use the color 0x000000 for the white squares, and 0x000001 for the dark squares. And let's have the chessboard squares be 1x1. I can then bring it down to 48 characters:

w=8
p={}
for i=0,63 do p[i+1]=(i+(i-i%8)/8)%2 end


Or, if we forgo graphics entirely and stick to ASCII, we can use this 54-character solution and still be legible:

for i=1,8 do print(('* * * * *'):sub(1+i%2,8+i%2))end

• Oh! I see we have someone with some good knowledge of Lua. Really nice. It's a very good job I'd say. – Stefan Reich Jan 12 '14 at 22:17
• I do see an opportunity for improvement. 2 bytes can probably be saved both on the graphics version and on the text version. With the text version, I'm sure it's possible, my idea for graphics version would have to be tested. – Stefan Reich Jan 12 '14 at 22:20
• Oh, and another thing: -1 works as white in the pixel array. A white that is good as invisible is REALLY stretching it a bit... ^^ – Stefan Reich Jan 12 '14 at 22:22
• @StefanReich -1 for white you say? I'll change that at once. – breadbox Jan 12 '14 at 22:27

27-29 letters :-þ supersmall diagonal but not ascii ..

print(("♦♦♦♦♦♦♦♦\n"):rep(5));


3D version 35 letters :)

print(("◄►◄►◄►◄►◄►◄►◄►\n"):rep(8));


extra nice version :)

print(("▇   ▇   ▇   ▇\n  ▇   ▇   ▇   ▇\n"):rep(4));