Lua: Render a chess board in smallest code

1

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

Stefan Reich

Posted 2014-01-12T19:57:18.047

Reputation: 135

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). – None – 2016-02-16T19:54:59.080

Does it have to be rendered as an image? Your question doesn't specify that. – Cel Skeggs – 2014-01-12T21:38:39.957

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 – 2014-01-12T21:52:09.837

Answers

3

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.

Cel Skeggs

Posted 2014-01-12T19:57:18.047

Reputation: 901

Great job :) Is it the shortest ASCII version? Using "rep" is pretty smart! – Stefan Reich – 2014-01-12T22:26:14.993

(Typo in the second solution gives a ninth row.) – breadbox – 2014-01-12T22:42:18.527

@Stefan I'm unsure if it's the shortest solution, but it's probably close. – Cel Skeggs – 2014-01-12T22:44:50.397

@breadbox fixed. – Cel Skeggs – 2014-01-12T22:48:07.583

3

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

:)

user14921

Posted 2014-01-12T19:57:18.047

Reputation: 31

Nice. Wish I'd noticed that. – Cel Skeggs – 2015-05-09T20:21:45.053

1You 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 – 2014-01-17T12:02:11.077

2

38

Combining 14921's and col6y's ideas:

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

shiona

Posted 2014-01-12T19:57:18.047

Reputation: 2 889

0

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

breadbox

Posted 2014-01-12T19:57:18.047

Reputation: 6 893

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 – 2014-01-12T22:17:43.527

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 – 2014-01-12T22:20:10.167

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 – 2014-01-12T22:22:29.553

@StefanReich -1 for white you say? I'll change that at once. – breadbox – 2014-01-12T22:27:30.723

0

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));

ubidev

Posted 2014-01-12T19:57:18.047

Reputation: 1