Permission to use code
Have to put up these mails somewhere public - and thought why not my blog !!
Please ignore this unless you are interested in reading what is written below :)
--------- Mail from Stallman ---------------------
Return-path:
Received: from conversion-daemon.biff-mail1.india.sun.com by
biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
id <0HZ000L01JBJL3@biff-mail1.india.sun.com> (original mail from rms@gnu.org)
for mm132998@biff-mail1.India.Sun.COM; Wed, 09 Jun 2004 05:01:09 +0530 (IST)
Received: from sundivya.India.Sun.COM (sundivya.India.Sun.COM [129.158.227.31])
by biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
with ESMTP id <0HZ0004AJJZXB3@biff-mail1.india.sun.com> for
mm132998@biff-mail1.India.Sun.COM (ORCPT mridul@sun.com); Wed,
09 Jun 2004 05:01:09 +0530 (IST)
Received: from sunmail1brm.Central.Sun.COM
(sunmail1brm.Central.Sun.COM [129.147.62.17]) by sundivya.India.Sun.COM
(8.12.10+Sun/8.12.8/ENSMAIL,v2.2) with ESMTP id i58NV6pA022263 for
; Wed, 09 Jun 2004 05:01:07 +0530 (IST)
Received: from nwkea-mail-2.sun.com (nwkea-mail-2.Sun.COM [192.18.42.14])
by sunmail1brm.Central.Sun.COM (8.11.7p1+Sun/8.11.7/ENSMAIL,v2.2)
with ESMTP id i58NUuT22087 for; Tue,
08 Jun 2004 17:31:02 -0600 (MDT)
Received: from relay22.sun.com
(relay22.sun.com [192.12.251.34] (may be forged)) by nwkea-mail-2.sun.com
(8.12.10/8.12.9) with ESMTP id i58NUuNf006587 for; Tue,
08 Jun 2004 16:30:56 -0700 (PDT)
Received: from mms24es.sun.com (mms24es.sun.com [150.143.232.74])
by relay22i.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 23:30:55 +0000 (Z)
Received: from mms24bas.mms.us.syntegra.com
(mms24bas.mms.us.syntegra.com [192.12.251.70]) by mms24es.sun.com with ESMTP
for mridul@sun.com; Tue, 08 Jun 2004 23:30:55 +0000 (Z)
Received: from fencepost.gnu.org (fencepost.gnu.org [199.232.76.164])
by relay24.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 23:30:55 +0000 (Z)
Received: from rms by fencepost.gnu.org with local (Exim 4.34)
id 1BXq3S-0001mm-J7; Tue, 08 Jun 2004 19:30:54 -0400
Date: Tue, 08 Jun 2004 19:30:54 -0400
From: Richard Stallman
Subject: Re: diep move generator
In-reply-to: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
Sender: rms@gnu.org
To: Vincent Diepeveen
Cc: Mridul.Muralidharan@Sun.COM, cracraft@cox.net, Simon@wretched.demon.co.uk
Reply-to: rms@gnu.org
Message-id:
Content-type: TEXT/PLAIN
References: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
Original-recipient: rfc822;mridul@sun.com
I think we can now use your code.
We should permanently save your email message
as an indication of your decision. Stuart and Mridul,
can you please do that? You can put it in a web page
for purposes of showing what Vincent said to us.
--------- End of Mail from Stallman --------------
--------- Mail from Vincent Diepeveen ------------
Return-path:
Received: from conversion-daemon.biff-mail1.india.sun.com by
biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
id <0HZ000A01C1XT0@biff-mail1.india.sun.com>
(original mail from diep@xs4all.nl) for mm132998@biff-mail1.India.Sun.COM;
Wed, 09 Jun 2004 02:17:45 +0530 (IST)
Received: from sundivya.India.Sun.COM (sundivya.India.Sun.COM [129.158.227.31])
by biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
with ESMTP id <0HZ0004ZTCFKB3@biff-mail1.india.sun.com> for
mm132998@biff-mail1.India.Sun.COM (ORCPT mridul@sun.com); Wed,
09 Jun 2004 02:17:45 +0530 (IST)
Received: from sunmail2.sfbay.sun.com
(sunmail2.SFBay.Sun.COM [129.149.246.180]) by sundivya.India.Sun.COM
(8.12.10+Sun/8.12.8/ENSMAIL,v2.2) with ESMTP id i58KldpA005230 for
; Wed, 09 Jun 2004 02:17:40 +0530 (IST)
Received: from nwkea-mail-1.sun.com (nwkea-mail-1.Sun.COM [192.18.42.13])
by sunmail2.sfbay.sun.com (8.11.7p1+Sun/8.11.7/ENSMAIL,v2.2)
with ESMTP id i58K1rK14278 for; Tue,
08 Jun 2004 13:47:36 -0700 (PDT)
Received: from relay24.sun.com
(ip192-12-251-74.block6.us.syntegra.com [192.12.251.74])
by nwkea-mail-1.sun.com (8.12.10/8.12.9) with ESMTP id i58K0icK004901 for
; Tue, 08 Jun 2004 13:01:52 -0700 (PDT)
Received: from mms22es.sun.com (mms22es.sun.com [150.143.232.34])
by relay24i.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 20:01:52 +0000 (Z)
Received: from mms22bas.mms.us.syntegra.com
(mms22bas.mms.us.syntegra.com [192.12.251.30]) by mms22es.sun.com with ESMTP
for mridul@sun.com; Tue, 08 Jun 2004 20:01:51 +0000 (Z)
Received: from smtp-out3.xs4all.nl (smtp-out3.xs4all.nl [194.109.24.13])
by relay22.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 20:01:50 +0000 (Z)
Received: from venezia (213-84-245-71.adsl.xs4all.nl [213.84.245.71])
by smtp-out3.xs4all.nl (8.12.10/8.12.10) with SMTP id i58K1iUQ089743; Tue,
08 Jun 2004 22:01:44 +0200 (CEST)
Date: Tue, 08 Jun 2004 22:05:22 +0100
From: Vincent Diepeveen
Subject: diep move generator
Sender: diep@xs4all.nl
X-Sender: diep@pop.xs4all.nl
To: Mridul Muralidharan
Cc: rms@gnu.org, cracraft@cox.net, Simon@wretched.demon.co.uk
Message-id: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
MIME-version: 1.0
X-Mailer: Windows Eudora Pro Version 3.0 (32)
Content-type: text/plain; charset=us-ascii
Original-recipient: rfc822;mridul@sun.com
Hello Mridul,
Here another reship of just the code i have given away for free to already
at least another 100 persons without any restrictions. Feel free to use it
for gnuchess6. Let me know whether this is ok
/* Move generator as released to the free internet.
Copyright (C) 1994-2004 Vincent Diepeveen
This file is part of DIEP.
Move generator is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Move generator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with other GNU software; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
board[64] is a general array. values 0..6 like in empty, pawn ... king
snelboard[64] is having 13 values like also bbishop and such for blackbishop
color[64] is having 3 values, white, black and neutral
First the generator code itself then the remaining definitions:
#if MSVC
__inline
#endif
void GenMovesI(RecursionBlock *rb,int sq) {
/* Generate all moves for a piece ==> must inside movelist!
*/
int SRsq,piece,u,xside,*s,*t,side,*v,*w;
side = rb->side;
xside = rb->xside;
SRsq = (sq<<6);
piece = board[sq];
if( piece == pawn ) {
v = ipiecepos[side][sq];
w = ipawndir[side][sq];
u = *v++;
if( row(u) != 0 && row(u) != 7 ) {
if( color[u] == neutral) {
rb->zetend->zet = (SRsq|u);
rb->zetend++;
if( (u=*v) != 128 && color[u] == neutral ) { /* indien u == sq dan
false */
rb->zetend->zet = (SRsq|u);
rb->zetend++;
}
}
u = *w++;
if( color[u] == xside ) {/* ppos bevat geen 100, maar sq. */
rb->zetend->zet = (SRsq|u|move_captures);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == xside ) {
rb->zetend->zet = (SRsq|u|move_captures);
rb->zetend++;
}
}
else {
if( color[u] == neutral) {
rb->zetend->zet = (SRsq|u|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_pbishop);
rb->zetend++;
}
u = *w++;
if( color[u] == xside) {/* captures */
rb->zetend->zet = (SRsq|u|move_captures|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pbishop);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == xside) {
rb->zetend->zet = (SRsq|u|move_captures|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pbishop);
rb->zetend++;
}
}
}
else {
t = cancapside[side];
if( sweep[piece] ) {
int *vh,*wh,uh;
s = andscan[0];
vh = ipiecepos[piece][sq];
wh = iskippos[sq];
uh = *vh++;
do {
int p1=snelbord[uh],sh=wh[uh]; /* local variables is faster for
GCC/MSVC */
vh += (s[p1]&sh);
if( color[uh] != side ) {
rb->zetend->zet = (SRsq|uh|t[p1]);
rb->zetend++;
}
} while( (uh=*vh++) != 128 );
/*vi = gentable[piece-3][sq];
s = doorscan[0];
u = vi[sq];
do {
if( color[u] != side ) {
cappiece = snelbord[u];
rb->zetend->zet = (SRsq|u|t[cappiece]);
rb->zetend++;
u = vi[(s[cappiece]|u)];
}
else {
u = vi[(64|u)];
}
} while( u != 128 );*/
}
else {
v = ipiecepos[piece][sq];
u = *v++;
do {
if( color[u] != side ) {
rb->zetend->zet = (SRsq|u|t[snelbord[u]]);
rb->zetend++;
}
} while( (u=*v++) != 128 );
}
}
}
void MoveListI(RecursionBlock *rb,struct Move *CFEP) {
/* Generate all semi-legal moves. Check is ignored.
*/
int f,to,from,u,*psq,*pend,*w;
to = CFEP->zet&63;
rb->zetend = rb->SemiLegals;
if( board[to] == pawn ) { /* for enpassant */
from = (CFEP->zet>>6)&63;
if( to-from == 16 || from-to == 16 ) {
f = (to+from)>>1;
w = ipawndir[rb->xside][f];
u = *w++;
if( color[u] == rb->side && board[u] == pawn ) {
rb->zetend->zet = ((u<<6)|f|move_captures|move_enpassant);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == rb->side && board[u] == pawn ) {
rb->zetend->zet = ((u<<6)|f|move_captures|move_enpassant);
rb->zetend++;
}
}
}
psq = PieceList[rb->side];
pend = PieceList[rb->side]+PieceMax[rb->side];
if( !castld[rb->side] ) {
u = *psq;
if( castle(rb->side,u,u+2) ) {
rb->zetend->zet = ((u<<6)|(u+2)|move_castles);
rb->zetend++;
}
if( castle(rb->side,u,u-2) ) {
rb->zetend->zet = ((u<<6)|(u-2)|move_castles);
rb->zetend++;
}
}
#if BOUNDSCHECKING
pend++;
do {
pend--;
if( *pend != 64 )
GenMovesI(rb,*pend);
} while( pend > psq );
#else
do {
if( *pend != 64 )
GenMovesI(rb,*pend);
} while( --pend >= psq );
#endif
} /* End MoveList() */
#define sq_a1 0
#define sq_b1 1
#define sq_c1 2
#define sq_d1 3
#define sq_e1 4
#define sq_f1 5
#define sq_g1 6
#define sq_h1 7
#define sq_a2 8
#define sq_b2 9
#define sq_c2 10
#define sq_d2 11
#define sq_e2 12
#define sq_f2 13
#define sq_g2 14
#define sq_h2 15
#define sq_a3 16
#define sq_b3 17
#define sq_c3 18
#define sq_d3 19
#define sq_e3 20
#define sq_f3 21
#define sq_g3 22
#define sq_h3 23
#define sq_a4 24
#define sq_b4 25
#define sq_c4 26
#define sq_d4 27
#define sq_e4 28
#define sq_f4 29
#define sq_g4 30
#define sq_h4 31
#define sq_a5 32
#define sq_b5 33
#define sq_c5 34
#define sq_d5 35
#define sq_e5 36
#define sq_f5 37
#define sq_g5 38
#define sq_h5 39
#define sq_a6 40
#define sq_b6 41
#define sq_c6 42
#define sq_d6 43
#define sq_e6 44
#define sq_f6 45
#define sq_g6 46
#define sq_h6 47
#define sq_a7 48
#define sq_b7 49
#define sq_c7 50
#define sq_d7 51
#define sq_e7 52
#define sq_f7 53
#define sq_g7 54
#define sq_h7 55
#define sq_a8 56
#define sq_b8 57
#define sq_c8 58
#define sq_d8 59
#define sq_e8 60
#define sq_f8 61
#define sq_g8 62
#define sq_h8 63
#define white 0
#define black 1
#define neutral 2
#define no_piece 0 /* 0b0000 */
#define pawn 1 /* 0b0001 */
#define knight 2 /* 0b0010 */
#define bishop 3 /* 0b0011 */
#define rook 4 /* 0b0100 */
#define queen 5 /* 0b0101 */
#define king 6 /* 0b0110 */
#define bpawn 9 /* 0b1001 */
#define bknight 10 /* 0b1010 */
#define bbishop 11 /* 0b1011 */
#define brook 12 /* 0b1100 */
#define bqueen 13 /* 0b1101 */
#define bking 14 /* 0b1110 */
int
cancapside[2][16] = { /* `move_captures' voor side */
{ 0,-1,-1,-1,-1,-1,-1,0,0,move_captures,move_captures,
move_captures,move_captures,move_captures,move_captures,0
},
{ 0,move_captures,move_captures,move_captures,move_captures,
move_captures,move_captures,0,0,-1,-1,-1,-1,-1,-1,0
}
},
#define move_escapes 0x00200000
#define move_extended 0x00100000
#define move_nullmoves 0x00080000
#define move_checks 0x00040000
#define move_captures 0x00020000
#define move_enpassant 0x00010000
#define move_castles 0x00008000
#define move_basicmove 0x00007fff
int andscan[1][16] = {
{ 0,63,63,63,63,63,63,63, /* standaard: niet door stukken heen! */
63,63,63,63,63,63,63,63
},
const int
nunmap[144] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, -1, -1,
-1, -1, 8, 9, 10, 11, 12, 13, 14, 15, -1, -1,
-1, -1, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1,
-1, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1,
-1, -1, 32, 33, 34, 35, 36, 37, 38, 39, -1, -1,
-1, -1, 40, 41, 42, 43, 44, 45, 46, 47, -1, -1,
-1, -1, 48, 49, 50, 51, 52, 53, 54, 55, -1, -1,
-1, -1, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
void InitGens(void) {
/* vul tabellen voor een 8x8 bord */
int
paardspring[8] = {15,-15,17,-17,6,-6,10,-10},
koningvlucht[8] = {7,-7,9,-9,8,-8,1,-1},
alldirec[3][8] = { /* delta richtingen op een 8x8 bord */
{7,-7,9,-9,0,0,0,0},
{8,-8,1,-1,0,0,0,0},
{7,-7,9,-9,8,-8,1,-1}
},
nsq,sq,flag,i,j,next_sq,rij,piece,np,lijn,m,ms,c,t,old_sq,
skipm[16],
rij8[2] = {7,0},
rij2[2] = {1,6},
slalinks[2] = {7,-9},
slarechts[2] = {9,-7},
veuren[2] = {8,-8},
/* 12x12 bord delta-coordinaten */
numberpiece[3] = {bishop,rook,queen},
numberfields[3] = {4,4,8},
direc[3][10] = { /* richtingen op een 12x12 bord */
{11,-11,13,-13,0,0,0,0,0,0}, /* loper */
{12,-12,1 ,-1 ,0,0,0,0,0,0}, /* toren */
{11,-11,13,-13,12,-12,1 ,-1,0,0} /* dame */
},
vlucht[8] = {11,-11,13,-13,12,-12,1 ,-1}, /* koning */
spring[8] = {23,-23,25,-25,10,-10,14,-14};/* paard */
for( i = 0 ; i < 3 ; i++ )
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 128 ; m++ )
gentable[i][sq][m] = 128;
for( i = 0 ; i < 7 ; i++ )
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 64 ; m++ )
looprichting[i][sq][m] = 0;
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 64 ; m++ )
iskippos[sq][m] = scanfurther[sq][m] = 0;
for( i = 0 ; i < 7 ; i++ ) /* default value */
for( sq = 0 ; sq < 64 ; sq++ ) {
for( m = 0 ; m < 32 ; m++ ) {
ipiecepos[i][sq][m] = 128;
}
}
/* pionnen */
for( sq = 0 ; sq < 64 ; sq++ ) {
lijn = sq&7;
rij = sq/8;
nsq = 26 + rij*12 + lijn;
for( c = white ; c <= black ; c++ ) { /* pion: wit en zwart */
if( rij == rij8[c] )
ipawndir[c][sq][0] = 128;
else {
m = 0;
if( lijn != 7 ) {
ipawndir[c][sq][m] = (sq+slarechts[c]);
looprichting[c][sq][sq+slarechts[c]] = slarechts[c];
m++;
}
if( lijn != 0 ) {
ipawndir[c][sq][m] = (sq+slalinks[c]);
looprichting[c][sq][sq+slalinks[c]] = slalinks[c];
m++;
}
ipawndir[c][sq][m] = 128;
m = 0;
ipiecepos[c][sq][m] = (sq+veuren[c]);
m++;
if( rij == rij2[c] )
ipiecepos[c][sq][m] = (sq+2*veuren[c]);
}
}
m = 0;
old_sq = sq;
for( i = 0; i < 8; i++ ) {/* paard */
t = nsq+spring[i];
if( nunmap[t] >= 0 ) {
ipiecepos[knight][sq][m] = nunmap[t];
m++;
looprichting[knight][sq][nunmap[t]] = paardspring[i];
old_sq = nunmap[t];
}
}
ipiecepos[knight][sq][m] = 128;
m = 0;
old_sq = sq;
for( i = 0; i < 8; i++ ) { /* koning */
t = nsq+vlucht[i];
if( nunmap[t] >= 0 ) {
ipiecepos[king][sq][m] = nunmap[t];
m++;
looprichting[king][sq][nunmap[t]] = koningvlucht[i];
old_sq = nunmap[t];
}
}
ipiecepos[king][sq][m] = 128;
for( np = 0 ; np < 3 ; np++ ) { /* loper,toren,dame */
piece = numberpiece[np];
m = 0;
ms = 0;
flag = 0;
j = 0;
while( nunmap[nsq+direc[np][j]] == -1 )
j++;
gentable[np][sq][sq] = nunmap[nsq+direc[np][j]];
for( i = 0; i < numberfields[np]; i++ ) { /* voor alle richtingen */
int sqdone=0;
t = nsq;
t = t+direc[np][i];
j = i+1;
while( nunmap[nsq+direc[np][j]] == -1 )
j++;
next_sq = nunmap[nsq+direc[np][j]];
while( nunmap[t] != -1 ) {
sqdone++;
if( nunmap[t+direc[np][i]] != -1 )
gentable[np][sq][nunmap[t]] = nunmap[t+direc[np][i]];
else {
if( next_sq == sq )
gentable[np][sq][nunmap[t]] = 128;
else
gentable[np][sq][nunmap[t]] = next_sq;
}
looprichting[piece][sq][nunmap[t]] = alldirec[np][i];
if( next_sq == sq )
gentable[np][sq][(nunmap[t]|64)] = 128;
else {
gentable[np][sq][(nunmap[t]|64)] = next_sq;
}
ipiecepos[piece][sq][m] = nunmap[t];
m++;
t = t+direc[np][i];
flag = 1;
}
if( sqdone >= 2 && np <= 1 ) {
/* alleen als er bij from-to daarna nog velden af
* te gaan zijn dan is het interessant. Anders 0.
* Dame hoeft niet want toren,loper al gedaan */
int my12sq/*,yesdeze=false*/;
/* begonnen met nsq (12x12) + delta (direc[np][i])
* */
//mprint("%s -> ",M2A[sq]);
my12sq = nsq+direc[np][i];
/*if( sq == sq_b4 && np == 0 && nunmap[my12sq] == sq_c3 ) {
yesdeze=true;
printf("Aloha! missing hier! sq_done = %i\n",sqdone);
} */
do {
/* 8x8 bord opslaan */
sqdone--;
/*if( yesdeze ) {*/
//printf("sq=%s to=%s
sqdone=%i\n",M2A[sq],M2A[nunmap[my12sq]],sqdone);
/*}*/
scanfurther[sq][nunmap[my12sq]] = ((alldirec[np][i]<<3)|sqdone);
//mprint("%s(%ix %i)
",M2A[nunmap[my12sq]],sqdone,alldirec[np][i]);
my12sq += direc[np][i];
} while( sqdone >= 2 );
//mprint("\n");
}
if( flag ) {
flag = 0;
skipm[ms] = m;
ms++;
}
}
ipiecepos[piece][sq][m] = 128;
m = 0;
ms = 0;
t = ipiecepos[piece][sq][m];
if( np == 2 ) { /* skippos alleen voor de dame vullen dat is genoeg */
do {
iskippos[sq][t] = (skipm[ms]-m)-1;
m++;
t = ipiecepos[piece][sq][m];
if( m == skipm[ms] )
ms++;
} while( t != 128 );
}
}
}
/*for( sq = 0 ; sq < 64 ; sq++ )
for( i = 0 ; i < 64 ; i++ ) {
andpos[sq][i] = skippos[sq][i]-1;
if( andpos[sq][i] < 0 )
andpos[sq][i] = 0;
}*/
}
--------- End of Mail from Vincent Diepeveen -----
Have to put up these mails somewhere public - and thought why not my blog !!
Please ignore this unless you are interested in reading what is written below :)
--------- Mail from Stallman ---------------------
Return-path:
Received: from conversion-daemon.biff-mail1.india.sun.com by
biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
id <0HZ000L01JBJL3@biff-mail1.india.sun.com> (original mail from rms@gnu.org)
for mm132998@biff-mail1.India.Sun.COM; Wed, 09 Jun 2004 05:01:09 +0530 (IST)
Received: from sundivya.India.Sun.COM (sundivya.India.Sun.COM [129.158.227.31])
by biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
with ESMTP id <0HZ0004AJJZXB3@biff-mail1.india.sun.com> for
mm132998@biff-mail1.India.Sun.COM (ORCPT mridul@sun.com); Wed,
09 Jun 2004 05:01:09 +0530 (IST)
Received: from sunmail1brm.Central.Sun.COM
(sunmail1brm.Central.Sun.COM [129.147.62.17]) by sundivya.India.Sun.COM
(8.12.10+Sun/8.12.8/ENSMAIL,v2.2) with ESMTP id i58NV6pA022263 for
Received: from nwkea-mail-2.sun.com (nwkea-mail-2.Sun.COM [192.18.42.14])
by sunmail1brm.Central.Sun.COM (8.11.7p1+Sun/8.11.7/ENSMAIL,v2.2)
with ESMTP id i58NUuT22087 for
08 Jun 2004 17:31:02 -0600 (MDT)
Received: from relay22.sun.com
(relay22.sun.com [192.12.251.34] (may be forged)) by nwkea-mail-2.sun.com
(8.12.10/8.12.9) with ESMTP id i58NUuNf006587 for
08 Jun 2004 16:30:56 -0700 (PDT)
Received: from mms24es.sun.com (mms24es.sun.com [150.143.232.74])
by relay22i.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 23:30:55 +0000 (Z)
Received: from mms24bas.mms.us.syntegra.com
(mms24bas.mms.us.syntegra.com [192.12.251.70]) by mms24es.sun.com with ESMTP
for mridul@sun.com; Tue, 08 Jun 2004 23:30:55 +0000 (Z)
Received: from fencepost.gnu.org (fencepost.gnu.org [199.232.76.164])
by relay24.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 23:30:55 +0000 (Z)
Received: from rms by fencepost.gnu.org with local (Exim 4.34)
id 1BXq3S-0001mm-J7; Tue, 08 Jun 2004 19:30:54 -0400
Date: Tue, 08 Jun 2004 19:30:54 -0400
From: Richard Stallman
Subject: Re: diep move generator
In-reply-to: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
Sender: rms@gnu.org
To: Vincent Diepeveen
Cc: Mridul.Muralidharan@Sun.COM, cracraft@cox.net, Simon@wretched.demon.co.uk
Reply-to: rms@gnu.org
Message-id:
Content-type: TEXT/PLAIN
References: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
Original-recipient: rfc822;mridul@sun.com
I think we can now use your code.
We should permanently save your email message
as an indication of your decision. Stuart and Mridul,
can you please do that? You can put it in a web page
for purposes of showing what Vincent said to us.
--------- End of Mail from Stallman --------------
--------- Mail from Vincent Diepeveen ------------
Return-path:
Received: from conversion-daemon.biff-mail1.india.sun.com by
biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
id <0HZ000A01C1XT0@biff-mail1.india.sun.com>
(original mail from diep@xs4all.nl) for mm132998@biff-mail1.India.Sun.COM;
Wed, 09 Jun 2004 02:17:45 +0530 (IST)
Received: from sundivya.India.Sun.COM (sundivya.India.Sun.COM [129.158.227.31])
by biff-mail1.india.sun.com
(iPlanet Messaging Server 5.2 HotFix 1.24 (built Dec 19 2003))
with ESMTP id <0HZ0004ZTCFKB3@biff-mail1.india.sun.com> for
mm132998@biff-mail1.India.Sun.COM (ORCPT mridul@sun.com); Wed,
09 Jun 2004 02:17:45 +0530 (IST)
Received: from sunmail2.sfbay.sun.com
(sunmail2.SFBay.Sun.COM [129.149.246.180]) by sundivya.India.Sun.COM
(8.12.10+Sun/8.12.8/ENSMAIL,v2.2) with ESMTP id i58KldpA005230 for
Received: from nwkea-mail-1.sun.com (nwkea-mail-1.Sun.COM [192.18.42.13])
by sunmail2.sfbay.sun.com (8.11.7p1+Sun/8.11.7/ENSMAIL,v2.2)
with ESMTP id i58K1rK14278 for
08 Jun 2004 13:47:36 -0700 (PDT)
Received: from relay24.sun.com
(ip192-12-251-74.block6.us.syntegra.com [192.12.251.74])
by nwkea-mail-1.sun.com (8.12.10/8.12.9) with ESMTP id i58K0icK004901 for
Received: from mms22es.sun.com (mms22es.sun.com [150.143.232.34])
by relay24i.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 20:01:52 +0000 (Z)
Received: from mms22bas.mms.us.syntegra.com
(mms22bas.mms.us.syntegra.com [192.12.251.30]) by mms22es.sun.com with ESMTP
for mridul@sun.com; Tue, 08 Jun 2004 20:01:51 +0000 (Z)
Received: from smtp-out3.xs4all.nl (smtp-out3.xs4all.nl [194.109.24.13])
by relay22.sun.com with ESMTP for mridul@sun.com; Tue,
08 Jun 2004 20:01:50 +0000 (Z)
Received: from venezia (213-84-245-71.adsl.xs4all.nl [213.84.245.71])
by smtp-out3.xs4all.nl (8.12.10/8.12.10) with SMTP id i58K1iUQ089743; Tue,
08 Jun 2004 22:01:44 +0200 (CEST)
Date: Tue, 08 Jun 2004 22:05:22 +0100
From: Vincent Diepeveen
Subject: diep move generator
Sender: diep@xs4all.nl
X-Sender: diep@pop.xs4all.nl
To: Mridul Muralidharan
Cc: rms@gnu.org, cracraft@cox.net, Simon@wretched.demon.co.uk
Message-id: <3.0.32.20040608220522.00f0c508@pop.xs4all.nl>
MIME-version: 1.0
X-Mailer: Windows Eudora Pro Version 3.0 (32)
Content-type: text/plain; charset=us-ascii
Original-recipient: rfc822;mridul@sun.com
Hello Mridul,
Here another reship of just the code i have given away for free to already
at least another 100 persons without any restrictions. Feel free to use it
for gnuchess6. Let me know whether this is ok
/* Move generator as released to the free internet.
Copyright (C) 1994-2004 Vincent Diepeveen
This file is part of DIEP.
Move generator is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
Move generator is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with other GNU software; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
board[64] is a general array. values 0..6 like in empty, pawn ... king
snelboard[64] is having 13 values like also bbishop and such for blackbishop
color[64] is having 3 values, white, black and neutral
First the generator code itself then the remaining definitions:
#if MSVC
__inline
#endif
void GenMovesI(RecursionBlock *rb,int sq) {
/* Generate all moves for a piece ==> must inside movelist!
*/
int SRsq,piece,u,xside,*s,*t,side,*v,*w;
side = rb->side;
xside = rb->xside;
SRsq = (sq<<6);
piece = board[sq];
if( piece == pawn ) {
v = ipiecepos[side][sq];
w = ipawndir[side][sq];
u = *v++;
if( row(u) != 0 && row(u) != 7 ) {
if( color[u] == neutral) {
rb->zetend->zet = (SRsq|u);
rb->zetend++;
if( (u=*v) != 128 && color[u] == neutral ) { /* indien u == sq dan
false */
rb->zetend->zet = (SRsq|u);
rb->zetend++;
}
}
u = *w++;
if( color[u] == xside ) {/* ppos bevat geen 100, maar sq. */
rb->zetend->zet = (SRsq|u|move_captures);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == xside ) {
rb->zetend->zet = (SRsq|u|move_captures);
rb->zetend++;
}
}
else {
if( color[u] == neutral) {
rb->zetend->zet = (SRsq|u|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_pbishop);
rb->zetend++;
}
u = *w++;
if( color[u] == xside) {/* captures */
rb->zetend->zet = (SRsq|u|move_captures|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pbishop);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == xside) {
rb->zetend->zet = (SRsq|u|move_captures|move_pqueen);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pknight);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_prook);
rb->zetend++;
rb->zetend->zet = (SRsq|u|move_captures|move_pbishop);
rb->zetend++;
}
}
}
else {
t = cancapside[side];
if( sweep[piece] ) {
int *vh,*wh,uh;
s = andscan[0];
vh = ipiecepos[piece][sq];
wh = iskippos[sq];
uh = *vh++;
do {
int p1=snelbord[uh],sh=wh[uh]; /* local variables is faster for
GCC/MSVC */
vh += (s[p1]&sh);
if( color[uh] != side ) {
rb->zetend->zet = (SRsq|uh|t[p1]);
rb->zetend++;
}
} while( (uh=*vh++) != 128 );
/*vi = gentable[piece-3][sq];
s = doorscan[0];
u = vi[sq];
do {
if( color[u] != side ) {
cappiece = snelbord[u];
rb->zetend->zet = (SRsq|u|t[cappiece]);
rb->zetend++;
u = vi[(s[cappiece]|u)];
}
else {
u = vi[(64|u)];
}
} while( u != 128 );*/
}
else {
v = ipiecepos[piece][sq];
u = *v++;
do {
if( color[u] != side ) {
rb->zetend->zet = (SRsq|u|t[snelbord[u]]);
rb->zetend++;
}
} while( (u=*v++) != 128 );
}
}
}
void MoveListI(RecursionBlock *rb,struct Move *CFEP) {
/* Generate all semi-legal moves. Check is ignored.
*/
int f,to,from,u,*psq,*pend,*w;
to = CFEP->zet&63;
rb->zetend = rb->SemiLegals;
if( board[to] == pawn ) { /* for enpassant */
from = (CFEP->zet>>6)&63;
if( to-from == 16 || from-to == 16 ) {
f = (to+from)>>1;
w = ipawndir[rb->xside][f];
u = *w++;
if( color[u] == rb->side && board[u] == pawn ) {
rb->zetend->zet = ((u<<6)|f|move_captures|move_enpassant);
rb->zetend++;
}
if( (u=*w) != 128 && color[u] == rb->side && board[u] == pawn ) {
rb->zetend->zet = ((u<<6)|f|move_captures|move_enpassant);
rb->zetend++;
}
}
}
psq = PieceList[rb->side];
pend = PieceList[rb->side]+PieceMax[rb->side];
if( !castld[rb->side] ) {
u = *psq;
if( castle(rb->side,u,u+2) ) {
rb->zetend->zet = ((u<<6)|(u+2)|move_castles);
rb->zetend++;
}
if( castle(rb->side,u,u-2) ) {
rb->zetend->zet = ((u<<6)|(u-2)|move_castles);
rb->zetend++;
}
}
#if BOUNDSCHECKING
pend++;
do {
pend--;
if( *pend != 64 )
GenMovesI(rb,*pend);
} while( pend > psq );
#else
do {
if( *pend != 64 )
GenMovesI(rb,*pend);
} while( --pend >= psq );
#endif
} /* End MoveList() */
#define sq_a1 0
#define sq_b1 1
#define sq_c1 2
#define sq_d1 3
#define sq_e1 4
#define sq_f1 5
#define sq_g1 6
#define sq_h1 7
#define sq_a2 8
#define sq_b2 9
#define sq_c2 10
#define sq_d2 11
#define sq_e2 12
#define sq_f2 13
#define sq_g2 14
#define sq_h2 15
#define sq_a3 16
#define sq_b3 17
#define sq_c3 18
#define sq_d3 19
#define sq_e3 20
#define sq_f3 21
#define sq_g3 22
#define sq_h3 23
#define sq_a4 24
#define sq_b4 25
#define sq_c4 26
#define sq_d4 27
#define sq_e4 28
#define sq_f4 29
#define sq_g4 30
#define sq_h4 31
#define sq_a5 32
#define sq_b5 33
#define sq_c5 34
#define sq_d5 35
#define sq_e5 36
#define sq_f5 37
#define sq_g5 38
#define sq_h5 39
#define sq_a6 40
#define sq_b6 41
#define sq_c6 42
#define sq_d6 43
#define sq_e6 44
#define sq_f6 45
#define sq_g6 46
#define sq_h6 47
#define sq_a7 48
#define sq_b7 49
#define sq_c7 50
#define sq_d7 51
#define sq_e7 52
#define sq_f7 53
#define sq_g7 54
#define sq_h7 55
#define sq_a8 56
#define sq_b8 57
#define sq_c8 58
#define sq_d8 59
#define sq_e8 60
#define sq_f8 61
#define sq_g8 62
#define sq_h8 63
#define white 0
#define black 1
#define neutral 2
#define no_piece 0 /* 0b0000 */
#define pawn 1 /* 0b0001 */
#define knight 2 /* 0b0010 */
#define bishop 3 /* 0b0011 */
#define rook 4 /* 0b0100 */
#define queen 5 /* 0b0101 */
#define king 6 /* 0b0110 */
#define bpawn 9 /* 0b1001 */
#define bknight 10 /* 0b1010 */
#define bbishop 11 /* 0b1011 */
#define brook 12 /* 0b1100 */
#define bqueen 13 /* 0b1101 */
#define bking 14 /* 0b1110 */
int
cancapside[2][16] = { /* `move_captures' voor side */
{ 0,-1,-1,-1,-1,-1,-1,0,0,move_captures,move_captures,
move_captures,move_captures,move_captures,move_captures,0
},
{ 0,move_captures,move_captures,move_captures,move_captures,
move_captures,move_captures,0,0,-1,-1,-1,-1,-1,-1,0
}
},
#define move_escapes 0x00200000
#define move_extended 0x00100000
#define move_nullmoves 0x00080000
#define move_checks 0x00040000
#define move_captures 0x00020000
#define move_enpassant 0x00010000
#define move_castles 0x00008000
#define move_basicmove 0x00007fff
int andscan[1][16] = {
{ 0,63,63,63,63,63,63,63, /* standaard: niet door stukken heen! */
63,63,63,63,63,63,63,63
},
const int
nunmap[144] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, 0, 1, 2, 3, 4, 5, 6, 7, -1, -1,
-1, -1, 8, 9, 10, 11, 12, 13, 14, 15, -1, -1,
-1, -1, 16, 17, 18, 19, 20, 21, 22, 23, -1, -1,
-1, -1, 24, 25, 26, 27, 28, 29, 30, 31, -1, -1,
-1, -1, 32, 33, 34, 35, 36, 37, 38, 39, -1, -1,
-1, -1, 40, 41, 42, 43, 44, 45, 46, 47, -1, -1,
-1, -1, 48, 49, 50, 51, 52, 53, 54, 55, -1, -1,
-1, -1, 56, 57, 58, 59, 60, 61, 62, 63, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
void InitGens(void) {
/* vul tabellen voor een 8x8 bord */
int
paardspring[8] = {15,-15,17,-17,6,-6,10,-10},
koningvlucht[8] = {7,-7,9,-9,8,-8,1,-1},
alldirec[3][8] = { /* delta richtingen op een 8x8 bord */
{7,-7,9,-9,0,0,0,0},
{8,-8,1,-1,0,0,0,0},
{7,-7,9,-9,8,-8,1,-1}
},
nsq,sq,flag,i,j,next_sq,rij,piece,np,lijn,m,ms,c,t,old_sq,
skipm[16],
rij8[2] = {7,0},
rij2[2] = {1,6},
slalinks[2] = {7,-9},
slarechts[2] = {9,-7},
veuren[2] = {8,-8},
/* 12x12 bord delta-coordinaten */
numberpiece[3] = {bishop,rook,queen},
numberfields[3] = {4,4,8},
direc[3][10] = { /* richtingen op een 12x12 bord */
{11,-11,13,-13,0,0,0,0,0,0}, /* loper */
{12,-12,1 ,-1 ,0,0,0,0,0,0}, /* toren */
{11,-11,13,-13,12,-12,1 ,-1,0,0} /* dame */
},
vlucht[8] = {11,-11,13,-13,12,-12,1 ,-1}, /* koning */
spring[8] = {23,-23,25,-25,10,-10,14,-14};/* paard */
for( i = 0 ; i < 3 ; i++ )
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 128 ; m++ )
gentable[i][sq][m] = 128;
for( i = 0 ; i < 7 ; i++ )
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 64 ; m++ )
looprichting[i][sq][m] = 0;
for( sq = 0 ; sq < 64 ; sq++ )
for( m = 0 ; m < 64 ; m++ )
iskippos[sq][m] = scanfurther[sq][m] = 0;
for( i = 0 ; i < 7 ; i++ ) /* default value */
for( sq = 0 ; sq < 64 ; sq++ ) {
for( m = 0 ; m < 32 ; m++ ) {
ipiecepos[i][sq][m] = 128;
}
}
/* pionnen */
for( sq = 0 ; sq < 64 ; sq++ ) {
lijn = sq&7;
rij = sq/8;
nsq = 26 + rij*12 + lijn;
for( c = white ; c <= black ; c++ ) { /* pion: wit en zwart */
if( rij == rij8[c] )
ipawndir[c][sq][0] = 128;
else {
m = 0;
if( lijn != 7 ) {
ipawndir[c][sq][m] = (sq+slarechts[c]);
looprichting[c][sq][sq+slarechts[c]] = slarechts[c];
m++;
}
if( lijn != 0 ) {
ipawndir[c][sq][m] = (sq+slalinks[c]);
looprichting[c][sq][sq+slalinks[c]] = slalinks[c];
m++;
}
ipawndir[c][sq][m] = 128;
m = 0;
ipiecepos[c][sq][m] = (sq+veuren[c]);
m++;
if( rij == rij2[c] )
ipiecepos[c][sq][m] = (sq+2*veuren[c]);
}
}
m = 0;
old_sq = sq;
for( i = 0; i < 8; i++ ) {/* paard */
t = nsq+spring[i];
if( nunmap[t] >= 0 ) {
ipiecepos[knight][sq][m] = nunmap[t];
m++;
looprichting[knight][sq][nunmap[t]] = paardspring[i];
old_sq = nunmap[t];
}
}
ipiecepos[knight][sq][m] = 128;
m = 0;
old_sq = sq;
for( i = 0; i < 8; i++ ) { /* koning */
t = nsq+vlucht[i];
if( nunmap[t] >= 0 ) {
ipiecepos[king][sq][m] = nunmap[t];
m++;
looprichting[king][sq][nunmap[t]] = koningvlucht[i];
old_sq = nunmap[t];
}
}
ipiecepos[king][sq][m] = 128;
for( np = 0 ; np < 3 ; np++ ) { /* loper,toren,dame */
piece = numberpiece[np];
m = 0;
ms = 0;
flag = 0;
j = 0;
while( nunmap[nsq+direc[np][j]] == -1 )
j++;
gentable[np][sq][sq] = nunmap[nsq+direc[np][j]];
for( i = 0; i < numberfields[np]; i++ ) { /* voor alle richtingen */
int sqdone=0;
t = nsq;
t = t+direc[np][i];
j = i+1;
while( nunmap[nsq+direc[np][j]] == -1 )
j++;
next_sq = nunmap[nsq+direc[np][j]];
while( nunmap[t] != -1 ) {
sqdone++;
if( nunmap[t+direc[np][i]] != -1 )
gentable[np][sq][nunmap[t]] = nunmap[t+direc[np][i]];
else {
if( next_sq == sq )
gentable[np][sq][nunmap[t]] = 128;
else
gentable[np][sq][nunmap[t]] = next_sq;
}
looprichting[piece][sq][nunmap[t]] = alldirec[np][i];
if( next_sq == sq )
gentable[np][sq][(nunmap[t]|64)] = 128;
else {
gentable[np][sq][(nunmap[t]|64)] = next_sq;
}
ipiecepos[piece][sq][m] = nunmap[t];
m++;
t = t+direc[np][i];
flag = 1;
}
if( sqdone >= 2 && np <= 1 ) {
/* alleen als er bij from-to daarna nog velden af
* te gaan zijn dan is het interessant. Anders 0.
* Dame hoeft niet want toren,loper al gedaan */
int my12sq/*,yesdeze=false*/;
/* begonnen met nsq (12x12) + delta (direc[np][i])
* */
//mprint("%s -> ",M2A[sq]);
my12sq = nsq+direc[np][i];
/*if( sq == sq_b4 && np == 0 && nunmap[my12sq] == sq_c3 ) {
yesdeze=true;
printf("Aloha! missing hier! sq_done = %i\n",sqdone);
} */
do {
/* 8x8 bord opslaan */
sqdone--;
/*if( yesdeze ) {*/
//printf("sq=%s to=%s
sqdone=%i\n",M2A[sq],M2A[nunmap[my12sq]],sqdone);
/*}*/
scanfurther[sq][nunmap[my12sq]] = ((alldirec[np][i]<<3)|sqdone);
//mprint("%s(%ix %i)
",M2A[nunmap[my12sq]],sqdone,alldirec[np][i]);
my12sq += direc[np][i];
} while( sqdone >= 2 );
//mprint("\n");
}
if( flag ) {
flag = 0;
skipm[ms] = m;
ms++;
}
}
ipiecepos[piece][sq][m] = 128;
m = 0;
ms = 0;
t = ipiecepos[piece][sq][m];
if( np == 2 ) { /* skippos alleen voor de dame vullen dat is genoeg */
do {
iskippos[sq][t] = (skipm[ms]-m)-1;
m++;
t = ipiecepos[piece][sq][m];
if( m == skipm[ms] )
ms++;
} while( t != 128 );
}
}
}
/*for( sq = 0 ; sq < 64 ; sq++ )
for( i = 0 ; i < 64 ; i++ ) {
andpos[sq][i] = skippos[sq][i]-1;
if( andpos[sq][i] < 0 )
andpos[sq][i] = 0;
}*/
}
--------- End of Mail from Vincent Diepeveen -----
0 Comments:
Post a Comment
<< Home