## Is it time for another new move generator?

Discussion of chess software programming and technical issues.

Moderators: hgm, Harvey Williamson, bob

Forum rules
This textbox is used to restore diagrams posted with the [d] tag before the upgrade.
Michael Sherwin
Posts: 3040
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

### Is it time for another new move generator?

Yep!

Code: Select all

``````// Non Promotion Moves only, No Captures - a la Crafty
// NOT BITBOARD
void GenMoves&#40;) &#123;
s32 fs, ts, type, i, vec, stop, vic;
u32 vecBits;
u64 pieces;

pieces = picesOfColor&#91;wtm&#93;;
do &#123;
fs = FirstBit64&#40;pieces&#41;;
pieces &= clrBit64&#40;fs&#41;;
type = board&#91;fs&#93;;
vecBits = vectors&#91;fs&#93;&#91;type&#93;;                     // an index bit for each possible vector
do &#123;
i = FirstBit32&#40;vecBits&#41;;
vecBits &= clrBit32&#40;i&#41;;
vec = offset&#91;i&#93;;                            // the offset to generate the next square
stop = theEnd&#91;i&#93;&#91;type&#93;&#91;fs&#93;;                 // the stop value is for off of board
for&#40;ts = fs + vec; ts != stop; ts += vec&#41; &#123; // or for a limited range piece
vic = board&#91;ts&#93;;
if&#40;vic&#41; break;                         // No Captures
&#125;
&#125; while&#40;vecBits&#41;;
&#125; while &#40;pieces&#41;;
&#125;``````

This routine does it all, no seperate move code for any of the piece types including castling.
And it's fast!
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

hristo

### Re: Is it time for another new move generator?

Michael Sherwin wrote:Yep!

Code: Select all

``````// Non Promotion Moves only, No Captures - a la Crafty
// NOT BITBOARD
void GenMoves&#40;) &#123;
s32 fs, ts, type, i, vec, stop, vic;
u32 vecBits;
u64 pieces;

pieces = picesOfColor&#91;wtm&#93;;
do &#123;
fs = FirstBit64&#40;pieces&#41;;
pieces &= clrBit64&#40;fs&#41;;
type = board&#91;fs&#93;;
vecBits = vectors&#91;fs&#93;&#91;type&#93;;                     // an index bit for each possible vector
do &#123;
i = FirstBit32&#40;vecBits&#41;;
vecBits &= clrBit32&#40;i&#41;;
vec = offset&#91;i&#93;;                            // the offset to generate the next square
stop = theEnd&#91;i&#93;&#91;type&#93;&#91;fs&#93;;                 // the stop value is for off of board
for&#40;ts = fs + vec; ts != stop; ts += vec&#41; &#123; // or for a limited range piece
vic = board&#91;ts&#93;;
if&#40;vic&#41; break;                         // No Captures
&#125;
&#125; while&#40;vecBits&#41;;
&#125; while &#40;pieces&#41;;
&#125;``````

This routine does it all, no seperate move code for any of the piece types including castling.
And it's fast!
I have been using vectors for generating moves for a long time. Currently it takes ~22 CPU cycles to generate a move, where the moves are proper captures (including enpassant), promotions (all) and castling -- what is not validated within the move generator is whether the move would leave the king in check or if a castling move crosses attacked square.

For sliding pieces I use independent vectors that represent the direction in which they slide.
For instance the Knight moves from any given square are stored in a single vector, where the Bishop uses four vectors.
(The Queen borrows the vectors from the Rook and the Bishop)

In the end the size of the vector lookup table is ~9K (this includes all pieces and all squares of origin) I'm sure that those who are 'size' conscious can squeeze the vector table to less than 4K.

Regards.

Aleks Peshkov
Posts: 870
Joined: Sun Nov 19, 2006 8:16 pm
Location: Russia

### Re: Is it time for another new move generator?

Michael Sherwin wrote:

Code: Select all

``````stop = theEnd&#91;i&#93;&#91;type&#93;&#91;fs&#93;;                 // the stop value is for off of board
for&#40;ts = fs + vec; ts != stop; ts += vec&#41; &#123; // or for a limited range piece
vic = board&#91;ts&#93;;
if&#40;vic&#41; break;                         // No Captures
&#125;``````
Looks like a bad mailbox generator: you test each destination square twice: ts != stop and board[ts] != 0 during scanning board for each direction, even for non-sliding piece.
I see you find a way to exclude empty loops for impossible directions for some "from" squares near border. Doing so you may be possibly be able to unroll a first move from each direction and avoid loops with exactly one iteration for nonsliders.

Aleks Peshkov
Posts: 870
Joined: Sun Nov 19, 2006 8:16 pm
Location: Russia

### Re: Is it time for another new move generator?

Code: Select all

``````ts = fs;
do &#123;
ts += vec;
if &#40;board&#91;ts&#93;) break;
&#125; while &#40;ts != last&#41; //last legal square``````

Karlo Bala
Posts: 301
Joined: Wed Mar 22, 2006 9:17 am

### Re: Is it time for another new move generator?

Aleks Peshkov wrote:

Code: Select all

``````ts = fs;
do &#123;
ts += vec;
if &#40;board&#91;ts&#93;) break;
&#125; while &#40;ts != last&#41; //last legal square``````
It reminds me on ingenious idea from gnuchess:

Code: Select all

``````This file contains a description of GNU's new move generation algoritm.
Copyright &#40;C&#41; 1989 Free Software Foundation, Inc.

This file is part of CHESS.

CHESS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the CHESS General Public

Everyone is granted permission to copy, modify and redistribute
CHESS, but only under the conditions described in the
supposed to have been given to you along with CHESS so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.

New move Generation algoritm&#58;

Revision&#58; 1989-09-06

Author&#58; Hans Eric Sandstroem.

This algortim is the result of an attempt to make an hardware move
generator, but since I newer had the time and resources to build
the hardware I wrote a software version and incorporated that one
into gnuchess. This was the best way I could think of sharing this
algorithm with the computer chess community.

If there is anybody out there with the time and rescources to build
a hardware move generator I will be glad to assist.

The general idea behind this algoritm is to pre calculate
a lot of data. The data that is pre calculated is every possible move
for every piece from every square disregarding any other pieces on the
board. This pre calculated data is stored in an array that looks like
this&#58;

struct sqdata &#123;
short nextpos;
short nextdir;
&#125;;
struct sqdata posdata&#91;8&#93;&#91;64&#93;&#91;64&#93;;
/* posdata&#91;piecetype&#93;&#91;fromsquare&#93;&#91;destinationsquare&#93; */
example&#58;
the first move for a queen at e8 is stored at;
posdata&#91;queen&#93;&#91;e8&#93;&#91;e8&#93;.nextpos
suppose this is e7 and e7 is occupied then the next move
will be found in;
posdata&#91;queen&#93;&#91;e8&#93;&#91;e7&#93;.nextdir

To handle the differeces between white and black pawns &#40;they move in
opposite directions&#41; an array ptype has been introduced&#58;

static const short ptype&#91;2&#93;&#91;8&#93; = &#123;
no_piece,pawn,knight,bishop,rook,queen,king,no_piece,
no_piece,bpawn,knight,bishop,rook,queen,king,no_piece&#125;;
^^^^^
And it is used like this&#58;
piecetype = ptype&#91;side&#93;&#91;piece&#93;
When generating moves for pieces that are not black pawns, piece
can be used directly in posdata. As in the example above.

Thus the only thing one has to do when generating the moves
is to check for collisions with other pieces.
the move generation to do this looks like this&#58; &#40;for non pawns&#41;
p = posdata&#91;piece&#93;&#91;sq&#93;;
u = p&#91;sq&#93;.nextpos;
do &#123;
if &#40;color&#91;u&#93; == neutral&#41; &#123;
u = p&#91;u&#93;.nextpos;
&#125;
else &#123;
u = p&#91;u&#93;.nextdir;
&#125;
&#125; while &#40;u != sq&#41;;

- I`nt this just beautiful!

The array posdata is initialized in the routine Initialize_moves.
This routine is called just once and it works so no time has been spent
on the structure of this code. GenMoves and CaptureList generates the
moves but the routines ataks, BRscan, Sqatakd, KingScan and trapped
also relies on the move generation algoritm so they have also been
rewritten.
``````
Best Regards,
Karlo Balla Jr.

bob
Posts: 20406
Joined: Mon Feb 27, 2006 6:30 pm
Location: Birmingham, AL

### Re: Is it time for another new move generator?

You should read Carl Ebeling's thesis (CM university) "All the right moves". He did pretty much this in hardware around 1984-1985 in HiTech. It was the first piece of hardware they had...

Bill Rogers
Posts: 3562
Joined: Thu Mar 09, 2006 2:54 am
Location: San Jose, California

### Re: Is it time for another new move generator?

Hi
I am not sure if this is even the same thing but I have been using indices since 1980 in my chess program and the coding looks very similiar to your vectors. I don't know if they are the same thing or not but it sure simpliflied my programming.
Bill
P.S.
My original chess program was written in Basic.

Michael Sherwin
Posts: 3040
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

### Re: Is it time for another new move generator?

Aleks Peshkov wrote:

Code: Select all

``````ts = fs;
do &#123;
ts += vec;
if &#40;board&#91;ts&#93;) break;
&#125; while &#40;ts != last&#41; //last legal square``````
Good point Aleks, I will rework it. Thanks!

To others, this is nothing like the move generator in GNUChess. I also have an improved version of that too!
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Michael Sherwin
Posts: 3040
Joined: Fri May 26, 2006 1:00 am
Location: WY, USA
Full name: Michael Sherwin

### Re: Is it time for another new move generator?

Hi Bill,

You are allowed to publish 'basic code'. We C/C++ people are not going to attack you!
I hate if statements. Pawns demand if statements. Therefore I hate pawns.

Bill Rogers
Posts: 3562
Joined: Thu Mar 09, 2006 2:54 am
Location: San Jose, California

### Re: Is it time for another new move generator?

Hi Michael
I did just that years ago. I published my first version of my little chess program. I will see if I still have a copy so you can see my move generator.
Bill