Current world's smallest chess program

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
hgm
Posts: 27796
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Current world's smallest chess program

Post by hgm »

klx wrote: Sat Aug 28, 2021 10:09 am Ancient post, but the choice of not counting whitespace characters is a bit odd to me. I suppose you could encode all your logic in a string consisting of space and tab characters for a very "short" entry.
Not counting whitespace is a rule for the "International Obfuscated C Code Contest". This no doubt to leave the contestants freedom to determine the layout of the program without affecting the length. They even don't count semicolons there; I cannot imagine why they would do that.

For micro-Max I use a slightly different counting, as the purpose was not obfuscation per se. So I did want the program to have a nicely readable layout, rather than just be a single line of text. So I discard linefeeds, comments and spaces that could have been deleted without altering the meaning of the program. In your proposal all tabs and spaces would be coding characters, so they would have to be counted.
AndreAdrian
Posts: 2
Joined: Tue Aug 24, 2021 5:22 pm
Full name: Andre Adrian

Re: Current world's smallest chess program

Post by AndreAdrian »

klx wrote: Sat Aug 28, 2021 10:09 am I suppose you could encode all your logic in a string consisting of space and tab characters for a very "short" entry.
Yes, you could. But then you need a parser for this 1bit coding. And this parser needs source code. Often solving a problem that will solve the real problem is just a detour that brings you nothing - no reduction in speed or space (program size). A good example is the first implementation of Tiny Basic and the second one. The first had this two levels approach (an intermediate language to solve the Basis interpreter problem), the second was Z80 assembler solving the problem in a straightforward way.
Ras
Posts: 2487
Joined: Tue Aug 30, 2016 8:19 pm
Full name: Rasmus Althoff

Re: Current world's smallest chess program

Post by Ras »

AndreAdrian wrote: Thu Aug 26, 2021 5:29 pmNow I made a port to ESP32, ESP32-S2, ESP32-C3, called ESP32-Max, these are 32-bit CPUs with up to 4MByte RAM.
My CT800 engine runs on a Cortex-M4 microcontroller with 192kB RAM and 1MB ROM (266kB used), plus an embedded opening book, various time control options, and a mate search mode. Using that would also give you a 500 Elo uplift from Micro-Max. Licence is GPLv3+.
Rasmus Althoff
https://www.ct800.net
User avatar
xr_a_y
Posts: 1871
Joined: Sat Nov 25, 2017 2:28 pm
Location: France

Re: Current world's smallest chess program

Post by xr_a_y »

I'm currently trying to write a small chess program, it is mixed source python and C and is 5.2kb for now. I'm trying to make it smaller to enter the 4k TCEC bonus event but it is far from easy !
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: Current world's smallest chess program

Post by klx »

AndreAdrian wrote: Sat Aug 28, 2021 12:31 pm
klx wrote: Sat Aug 28, 2021 10:09 am I suppose you could encode all your logic in a string consisting of space and tab characters for a very "short" entry.
Yes, you could. But then you need a parser for this 1bit coding. And this parser needs source code. Often solving a problem that will solve the real problem is just a detour that brings you nothing - no reduction in speed or space (program size).
A parser is like 50 characters, depending on the language and requirements. Here's a quick C example, decoding a binary string of tabs and spaces into shellcode and executing it. This shellcode simply returns the number 100, but you could just as well encode all of Stockfish there if you wanted, without changing the "code size".

Code: Select all

#include <stdio.h>

int main() {
    char c[3], s[24] = "	 		     		  	  		    		";

    for (int n=0; n<24; ++n)
        c[n/8] = c[n/8]*2*!!(n&7)+s[n]%2;

    int x = ((char(*)())(void*)c)();
    printf("%d\n", x);
}
[Moderation warning] This signature violated the rule against commercial exhortations.
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Current world's smallest chess program

Post by amanjpro »

klx wrote: Mon Aug 30, 2021 11:15 am
AndreAdrian wrote: Sat Aug 28, 2021 12:31 pm
klx wrote: Sat Aug 28, 2021 10:09 am I suppose you could encode all your logic in a string consisting of space and tab characters for a very "short" entry.
Yes, you could. But then you need a parser for this 1bit coding. And this parser needs source code. Often solving a problem that will solve the real problem is just a detour that brings you nothing - no reduction in speed or space (program size).
A parser is like 50 characters, depending on the language and requirements. Here's a quick C example, decoding a binary string of tabs and spaces into shellcode and executing it. This shellcode simply returns the number 100, but you could just as well encode all of Stockfish there if you wanted, without changing the "code size".

Code: Select all

#include <stdio.h>

int main() {
    char c[3], s[24] = "	 		     		  	  		    		";

    for (int n=0; n<24; ++n)
        c[n/8] = c[n/8]*2*!!(n&7)+s[n]%2;

    int x = ((char(*)())(void*)c)();
    printf("%d\n", x);
}
I like your claim, can you write a working parser in 50 characters? instead of writing this toy counter?
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: Current world's smallest chess program

Post by klx »

amanjpro wrote: Mon Aug 30, 2021 12:12 pm I like your claim, can you write a working parser in 50 characters? instead of writing this toy counter?
I just did above? Removing whitespace the parser is:

Code: Select all

for(int n=0;n<24;++n)c[n/8]=c[n/8]*2*!!(n&7)+s[n]%2;
Ok so that's 52 characters, but it's trivial to make it shorter, here's one approach:

Code: Select all

n;

...

for(;s[n];++n)c[n/8]+=c[n/8]+s[n]%2;
38 characters
[Moderation warning] This signature violated the rule against commercial exhortations.
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Current world's smallest chess program

Post by amanjpro »

klx wrote: Mon Aug 30, 2021 1:51 pm
amanjpro wrote: Mon Aug 30, 2021 12:12 pm I like your claim, can you write a working parser in 50 characters? instead of writing this toy counter?
I just did above? Removing whitespace the parser is:

Code: Select all

for(int n=0;n<24;++n)c[n/8]=c[n/8]*2*!!(n&7)+s[n]%2;
Ok so that's 52 characters, but it's trivial to make it shorter, here's one approach:

Code: Select all

n;

...

for(;s[n];++n)c[n/8]+=c[n/8]+s[n]%2;
38 characters
What does your parser do? I didn't get your code
amanjpro
Posts: 883
Joined: Sat Mar 13, 2021 1:47 am
Full name: Amanj Sherwany

Re: Current world's smallest chess program

Post by amanjpro »

It doesn't translate from whitespace language to C++ unless I'm missing something
klx
Posts: 179
Joined: Tue Jun 15, 2021 8:11 pm
Full name: Emanuel Torres

Re: Current world's smallest chess program

Post by klx »

amanjpro wrote: Mon Aug 30, 2021 3:45 pm It doesn't translate from whitespace language to C++ unless I'm missing something
It translates from "binary whitespace" (tab / space) to machine code and runs it. This example is just "mov al, 100; ret;" but we can put any code there. The final code then becomes something like:

Code: Select all

n;main(){char s[]="	 		     		  	  		    		";
while(s[n])s[n++/8]+=s[n/8]+s[n]%2;((int(*)())(void*)s)();}
This is 79 non-whitespace characters.

I shall call this program Emanuelito -- The World's by Far Tiniest Chess Engine.

By slapping Stockfish in there we obtain ELO rating 3550, in 79 characters of C code. ¡Wow!
[Moderation warning] This signature violated the rule against commercial exhortations.