Move Table Generator

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Move Table Generator

Post by ZirconiumX »

I thought I'd put on the forum the source code for my slider move table generator, if for no other reason than to be able to find it if my computer dies.

Code: Select all

#include <stdio.h>

#define MKSQ&#40;r,f&#41; (&#40;r*8&#41;+f&#41;
#define END&#40;idx&#41; MoveTable&#91;idx&#93; = 64, idx++

unsigned int MoveTable&#91;1968&#93;;
unsigned int MoveTableLookup&#91;2&#93;&#91;64&#93;&#91;4&#93;; // &#91;Rook?&#93;&#91;Square&#93;&#91;Direction&#93;

int FillMoveTable&#40;int rank, int file, int rankinc, int fileinc, int idx&#41;
&#123;
	int destrank, destfile;
	for &#40;destrank = rank + rankinc, destfile = file + fileinc;
			destrank >= 0 && destrank <= 7 &&
			destfile >= 0 && destfile <= 7;
			destrank += rankinc, destfile += fileinc&#41; &#123;
		MoveTable&#91;idx&#93; = MKSQ&#40;destrank, destfile&#41;;
		idx++;
	&#125;
	return idx;
&#125;

void InitialiseMoveTable&#40;)
&#123;
	int idx, rank, file;
	idx = 0;
	for &#40;rank = 0; rank < 8; rank++) &#123;
		for &#40;file = 0; file < 8; file++) &#123;
			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;0&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1, +1, idx&#41;;
			END&#40;idx&#41;;			

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;1&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1, -1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;2&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1, +1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;3&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1, -1, idx&#41;;
			END&#40;idx&#41;;
		&#125;
	&#125;
	for &#40;rank = 0; rank < 8; rank++) &#123;
		for &#40;file = 0; file < 8; file++) &#123;
			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;0&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1,  0, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;1&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1,  0, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;2&#93; = idx;
			idx = FillMoveTable&#40;rank, file,  0, +1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;3&#93; = idx;
			idx = FillMoveTable&#40;rank, file,  0, -1, idx&#41;;
			END&#40;idx&#41;;
		&#125;
	&#125;
&#125;

int main&#40;)
&#123;
	InitialiseMoveTable&#40;);
	int i, j;
	for &#40;i = 0; i < 4; i++) &#123;
		j = MoveTableLookup&#91;1&#93;&#91;31&#93;&#91;i&#93;;
		while &#40;MoveTable&#91;j&#93; != 64&#41; &#123;
			printf&#40;"%d\n", MoveTable&#91;j&#93;);
			j++;
		&#125;
	&#125;
	return 0;
&#125;
Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
User avatar
vittyvirus
Posts: 646
Joined: Wed Jun 18, 2014 2:30 pm
Full name: Fahad Syed

Re: Move Table Generator

Post by vittyvirus »

ZirconiumX wrote:I thought I'd put on the forum the source code for my slider move table generator, if for no other reason than to be able to find it if my computer dies.

Code: Select all

#include <stdio.h>

#define MKSQ&#40;r,f&#41; (&#40;r*8&#41;+f&#41;
#define END&#40;idx&#41; MoveTable&#91;idx&#93; = 64, idx++

unsigned int MoveTable&#91;1968&#93;;
unsigned int MoveTableLookup&#91;2&#93;&#91;64&#93;&#91;4&#93;; // &#91;Rook?&#93;&#91;Square&#93;&#91;Direction&#93;

int FillMoveTable&#40;int rank, int file, int rankinc, int fileinc, int idx&#41;
&#123;
	int destrank, destfile;
	for &#40;destrank = rank + rankinc, destfile = file + fileinc;
			destrank >= 0 && destrank <= 7 &&
			destfile >= 0 && destfile <= 7;
			destrank += rankinc, destfile += fileinc&#41; &#123;
		MoveTable&#91;idx&#93; = MKSQ&#40;destrank, destfile&#41;;
		idx++;
	&#125;
	return idx;
&#125;

void InitialiseMoveTable&#40;)
&#123;
	int idx, rank, file;
	idx = 0;
	for &#40;rank = 0; rank < 8; rank++) &#123;
		for &#40;file = 0; file < 8; file++) &#123;
			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;0&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1, +1, idx&#41;;
			END&#40;idx&#41;;			

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;1&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1, -1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;2&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1, +1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;0&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;3&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1, -1, idx&#41;;
			END&#40;idx&#41;;
		&#125;
	&#125;
	for &#40;rank = 0; rank < 8; rank++) &#123;
		for &#40;file = 0; file < 8; file++) &#123;
			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;0&#93; = idx;
			idx = FillMoveTable&#40;rank, file, +1,  0, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;1&#93; = idx;
			idx = FillMoveTable&#40;rank, file, -1,  0, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;2&#93; = idx;
			idx = FillMoveTable&#40;rank, file,  0, +1, idx&#41;;
			END&#40;idx&#41;;

			MoveTableLookup&#91;1&#93;&#91;MKSQ&#40;rank, file&#41;&#93;&#91;3&#93; = idx;
			idx = FillMoveTable&#40;rank, file,  0, -1, idx&#41;;
			END&#40;idx&#41;;
		&#125;
	&#125;
&#125;

int main&#40;)
&#123;
	InitialiseMoveTable&#40;);
	int i, j;
	for &#40;i = 0; i < 4; i++) &#123;
		j = MoveTableLookup&#91;1&#93;&#91;31&#93;&#91;i&#93;;
		while &#40;MoveTable&#91;j&#93; != 64&#41; &#123;
			printf&#40;"%d\n", MoveTable&#91;j&#93;);
			j++;
		&#125;
	&#125;
	return 0;
&#125;
Matthew:out
Thanks Matthew. But why don't you comment, or at least elaborate your code?
ZirconiumX
Posts: 1334
Joined: Sun Jul 17, 2011 11:14 am

Re: Move Table Generator

Post by ZirconiumX »

Commenting takes time, and I like my code to be as self documenting as possible. You will note the variable names have been carefully chosen to maximise readability, and whitespace is added to seperate the code into chunks.

Matthew:out
Some believe in the almighty dollar.

I believe in the almighty printf statement.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: Move Table Generator

Post by AlvaroBegue »

ZirconiumX wrote:Commenting takes time, and I like my code to be as self documenting as possible. You will note the variable names have been carefully chosen to maximise readability, and whitespace is added to seperate the code into chunks.
#define MKSQ(r,f) ((r*8)+f)
I see...
zullil
Posts: 6442
Joined: Tue Jan 09, 2007 12:31 am
Location: PA USA
Full name: Louis Zulli

Re: Move Table Generator

Post by zullil »

AlvaroBegue wrote:
ZirconiumX wrote:Commenting takes time, and I like my code to be as self documenting as possible. You will note the variable names have been carefully chosen to maximise readability, and whitespace is added to seperate the code into chunks.
#define MKSQ(r,f) ((r*8)+f)
I see...
(rank, file) ---> square index

MKSQ means "Make Square"?
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Move Table Generator

Post by bob »

zullil wrote:
AlvaroBegue wrote:
ZirconiumX wrote:Commenting takes time, and I like my code to be as self documenting as possible. You will note the variable names have been carefully chosen to maximise readability, and whitespace is added to seperate the code into chunks.
#define MKSQ(r,f) ((r*8)+f)
I see...
(rank, file) ---> square index

MKSQ means "Make Square"?
His point was, "not exactly 'self-documenting'"

I've never seen ANY C code that couldn't be made more readable with the judicious use of comments. What might be perfectly readable/understandable by the author is likely anything but to the person perusing the source.
zullil
Posts: 6442
Joined: Tue Jan 09, 2007 12:31 am
Location: PA USA
Full name: Louis Zulli

Re: Move Table Generator

Post by zullil »

bob wrote:
zullil wrote:
AlvaroBegue wrote:
ZirconiumX wrote:Commenting takes time, and I like my code to be as self documenting as possible. You will note the variable names have been carefully chosen to maximise readability, and whitespace is added to seperate the code into chunks.
#define MKSQ(r,f) ((r*8)+f)
I see...
(rank, file) ---> square index

MKSQ means "Make Square"?
His point was, "not exactly 'self-documenting'"
Yeah. Was just speculating about MKSQ.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: Move Table Generator

Post by lucasart »

ZirconiumX wrote:I thought I'd put on the forum the source code for my slider move table generator, if for no other reason than to be able to find it if my computer dies.
github
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
Angrim
Posts: 97
Joined: Mon Jun 25, 2012 10:16 pm
Location: Forks, WA
Full name: Ben Nye

Re: Move Table Generator

Post by Angrim »

bob wrote: His point was, "not exactly 'self-documenting'"

I've never seen ANY C code that couldn't be made more readable with the judicious use of comments. What might be perfectly readable/understandable by the author is likely anything but to the person perusing the source.
My personal engine code, which I have no intention of ever letting another human see, is fairly heavily commented.
A fact that I have been very grateful for as I am returning to work on it after taking almost a decade off from serious engine development!
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Move Table Generator

Post by bob »

Angrim wrote:
bob wrote: His point was, "not exactly 'self-documenting'"

I've never seen ANY C code that couldn't be made more readable with the judicious use of comments. What might be perfectly readable/understandable by the author is likely anything but to the person perusing the source.
My personal engine code, which I have no intention of ever letting another human see, is fairly heavily commented.
A fact that I have been very grateful for as I am returning to work on it after taking almost a decade off from serious engine development!
I think that makes perfect sense, from years of experience. Comments are far easier to read than code. The only down-side is they MUST be kept current. Nothing worse than comments that say one thing while the code does something else. Had my share of that problem as well.