Anything new about the ChessV Project?

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

User avatar
smrf
Posts: 484
Joined: Mon Mar 13, 2006 11:08 am
Location: Klein-Gerau, Germany

Re: Anything new about the ChessV Project?

Post by smrf »

Well, I think A and Q are distributed on differently colored squares already by the numbering scheme. But the covering of every pawn and the avoiding of neighbored bishops are done by filtering after randomizing the position.

Of course the filtering could be disabled e.g. by introducing a new menu option. But I experienced the raising of some unacceptable high advantage for the beginning white side in some special starting arrays. Especially when too much Bishop compatible pieces e.g. are very far left and the opposite King is standing far right. Thus I decided to avoid a lot of such extremes by default. Nevertheless there still some dangerous arrays are possible.
User avatar
hgm
Posts: 27837
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Anything new about the ChessV Project?

Post by hgm »

Matthias Gemuh wrote:I can recode my shuffling according to what your more complex shuffling demands, so just go ahead and fully define a standard.
OK, the code below seems to work. To convert the random number to a position, I peel off bits from the low end, by taking remainder, and use that to place the next piece or piece pair. For CRC the piece order is

B(light), B(dark), C, A, Q, N(pair) and fill remaining holes with KRK.

Both B are assigned with 5 possibilities, on their respective colors.
C then with 8 possibilities on any color,
A with 7
Q with 6
the N pair with 20 (= 5*4/2)

for a total of 25*8*7*6*20 = 168,000 shuffles

When I shuffle in variant capablanca, rather then caparandom, I place K and R first in their orthoox position, (f1, a1 and j1), after which the order is:

light B 3 possibilities (b1, d1, h1)
dark B 4 possibilities (c1, e1, g1, i1)
C with 5
A with 4
Q with 3
and the holes filled with Knights

This gives 12*5*4*3 = 720 possibilities.

Code: Select all

// [HGM] shuffle: a more general way to suffle opening setups, applicable to arbitrry variants.
// All positions will have equal probability, but the current method will not provide a unique
// numbering scheme for arrays that contain 3 or more pieces of the same kind.
#define DARK 1
#define LITE 2
#define ANY 3

int squaresLeft[4];
int piecesLeft[(int)BlackPawn];
long long int seed, nrOfShuffles;

int put(Board board, int pieceType, int rank, int n, int shade)
// put the piece on the (n-1)-th empty squares of the given shade
{
	int i;

	for&#40;i=BOARD_LEFT; i<BOARD_RGHT; i++) &#123;
		if&#40; (&#40;i-BOARD_LEFT&#41;&1&#41;+1 & shade && board&#91;rank&#93;&#91;i&#93; == EmptySquare && n-- == 0&#41; &#123;
			board&#91;rank&#93;&#91;i&#93; = &#40;ChessSquare&#41; pieceType;
			squaresLeft&#91;&#40;i-BOARD_LEFT&1&#41; + 1&#93;--;
			squaresLeft&#91;ANY&#93;--;
			piecesLeft&#91;pieceType&#93;--; 
			return i;
		&#125;
	&#125;
        return -1;
&#125;


void AddOnePiece&#40;Board board, int pieceType, int rank, int shade&#41;
// calculate where the next piece goes, &#40;any empty square&#41;, and put it there
&#123;
	int i;

        i = seed % squaresLeft&#91;shade&#93;;
	nrOfShuffles *= squaresLeft&#91;shade&#93;;
	seed /= squaresLeft&#91;shade&#93;;
        put&#40;board, pieceType, rank, i, shade&#41;;
&#125;

void AddTwoPieces&#40;Board board, int pieceType, int rank&#41;
// calculate where the next 2 identical pieces go, &#40;any empty square&#41;, and put it there
&#123;
	int i, n=squaresLeft&#91;ANY&#93;, j=n-1, k;

	k = n*&#40;n-1&#41;/2; // nr of possibilities, not counting permutations
        i = seed % k;  // pick one
	nrOfShuffles *= k;
	seed /= k;
	while&#40;i >= j&#41; i -= j--;
        j = n - 1 - j; i += j;
        put&#40;board, pieceType, rank, j, ANY&#41;;
        put&#40;board, pieceType, rank, i, ANY&#41;;
&#125;

void SetUpShuffle&#40;Board board, int number&#41;
&#123;
	int i, p, first=1;

	seed = number, nrOfShuffles = 1;
	if&#40;number < 0&#41; &#123; 
		srand&#40;time&#40;0&#41;); 
		for&#40;i=0; i<50; i++) seed += rand&#40;);
		seed = rand&#40;) ^ rand&#40;)<<16; 
	&#125;

	squaresLeft&#91;DARK&#93; = &#40;BOARD_RGHT - BOARD_LEFT + 1&#41;/2;
	squaresLeft&#91;ANY&#93;  = BOARD_RGHT - BOARD_LEFT;
	squaresLeft&#91;LITE&#93; = squaresLeft&#91;ANY&#93; - squaresLeft&#91;DARK&#93;;

	for&#40;p = 0; p<=&#40;int&#41;WhiteKing; p++) piecesLeft&#91;p&#93; = 0;

	for&#40;i=BOARD_LEFT; i<BOARD_RGHT; i++) &#123; // count pieces and clear board
	    p = &#40;int&#41; board&#91;0&#93;&#91;i&#93;;
	    if&#40;p < &#40;int&#41; BlackPawn&#41; piecesLeft&#91;p&#93; ++;
	    board&#91;0&#93;&#91;i&#93; = EmptySquare;
	&#125;

	if&#40;PosFlags&#40;0&#41; & F_ALL_CASTLE_OK&#41; &#123;
	    // shuffles restricted to allow normal castling put KRR first
	    if&#40;piecesLeft&#91;&#40;int&#41;WhiteKing&#93;) // King goes rightish of middle
		put&#40;board, WhiteKing, 0, &#40;BOARD_LEFT+BOARD_RGHT+1&#41;/2, ANY&#41;;
	    else if&#40;piecesLeft&#91;&#40;int&#41;WhiteUnicorn&#93;) // in Knightmate Unicorn castles
		put&#40;board, WhiteUnicorn, 0, &#40;BOARD_LEFT+BOARD_RGHT+1&#41;/2, ANY&#41;;
	    if&#40;piecesLeft&#91;&#40;int&#41;WhiteRook&#93;) // First supply a Rook for K-side castling
		put&#40;board, WhiteRook, 0, BOARD_RGHT-2, ANY&#41;;
	    if&#40;piecesLeft&#91;&#40;int&#41;WhiteRook&#93;) // Then supply a Rook for Q-side castling
		put&#40;board, WhiteRook, 0, BOARD_LEFT, ANY&#41;;
	    // in variants with super-numerary Kings and Rooks, we leave these for the shuffle
	&#125;

	if&#40;&#40;BOARD_RGHT-BOARD_LEFT & 1&#41; == 0&#41;
	    // only for even boards make effort to put pairs of colorbound pieces on opposite colors
	    for&#40;p = &#40;int&#41; WhiteKing; p > &#40;int&#41; WhitePawn; p--) &#123;
		if&#40;p != &#40;int&#41; WhiteBishop && p != &#40;int&#41; WhiteFerz && p != &#40;int&#41; WhiteAlfil&#41; continue;
		while&#40;piecesLeft&#91;p&#93; >= 2&#41; &#123;
		    AddOnePiece&#40;board, p, 0, LITE&#41;;
		    AddOnePiece&#40;board, p, 0, DARK&#41;;
		&#125;
		// Odd color-bound pieces are shuffled with the rest &#40;to not run out of paired squares&#41;
	    &#125;

	for&#40;p = &#40;int&#41; WhiteKing - 2; p > &#40;int&#41; WhitePawn; p--) &#123;
	    // Remaining pieces &#40;non-colorbound, or odd color bound&#41; can be put anywhere
	    // but we leave King and Rooks for last, to possibly obey FRC restriction
	    if&#40;p == &#40;int&#41;WhiteRook&#41; continue;
	    while&#40;piecesLeft&#91;p&#93; >= 2&#41; AddTwoPieces&#40;board, p, 0&#41;; // add in pairs, for not counting permutations
	    if&#40;piecesLeft&#91;p&#93;) AddOnePiece&#40;board, p, 0, ANY&#41;;     // add the odd piece
	&#125;

	// now everything is placed, except perhaps King &#40;Unicorn&#41; and Rooks

	if&#40;PosFlags&#40;0&#41; & F_FRC_TYPE_CASTLING&#41; &#123;
	    // Last King gets castling rights
	    while&#40;piecesLeft&#91;&#40;int&#41;WhiteUnicorn&#93;) &#123;
		i = put&#40;board, WhiteUnicorn, 0, piecesLeft&#91;&#40;int&#41;WhiteRook&#93;/2, ANY&#41;;
		initialRights&#91;2&#93;  = initialRights&#91;5&#93;  = castlingRights&#91;0&#93;&#91;2&#93; = castlingRights&#91;0&#93;&#91;5&#93; = i;
	    &#125;

	    while&#40;piecesLeft&#91;&#40;int&#41;WhiteKing&#93;) &#123;
		i = put&#40;board, WhiteKing, 0, piecesLeft&#91;&#40;int&#41;WhiteRook&#93;/2, ANY&#41;;
		initialRights&#91;2&#93;  = initialRights&#91;5&#93;  = castlingRights&#91;0&#93;&#91;2&#93; = castlingRights&#91;0&#93;&#91;5&#93; = i;
	    &#125;


	&#125; else &#123;
	    while&#40;piecesLeft&#91;&#40;int&#41;WhiteKing&#93;)    AddOnePiece&#40;board, WhiteKing, 0, ANY&#41;;
	    while&#40;piecesLeft&#91;&#40;int&#41;WhiteUnicorn&#93;) AddOnePiece&#40;board, WhiteUnicorn, 0, ANY&#41;;
	&#125;

	// Only Rooks can be left; simply place them all
	while&#40;piecesLeft&#91;&#40;int&#41;WhiteRook&#93;) &#123;
		i = put&#40;board, WhiteRook, 0, 0, ANY&#41;;
		if&#40;PosFlags&#40;0&#41; & F_FRC_TYPE_CASTLING&#41; &#123; // first and last Rook get FRC castling rights
			if&#40;first&#41; &#123;
				first=0;
				initialRights&#91;1&#93;  = initialRights&#91;4&#93;  = castlingRights&#91;0&#93;&#91;1&#93; = castlingRights&#91;0&#93;&#91;4&#93; = i;
			&#125;
			initialRights&#91;0&#93;  = initialRights&#91;3&#93;  = castlingRights&#91;0&#93;&#91;0&#93; = castlingRights&#91;0&#93;&#91;3&#93; = i;
		&#125;
	&#125;
	for&#40;i=BOARD_LEFT; i<BOARD_RGHT; i++) &#123; // copy black from white
	    board&#91;BOARD_HEIGHT-1&#93;&#91;i&#93; =  &#40;int&#41; board&#91;0&#93;&#91;i&#93; < BlackPawn ? WHITE_TO_BLACK board&#91;0&#93;&#91;i&#93; &#58; EmptySquare;
	&#125;

	if&#40;number >= 0&#41; appData.defaultFrcPosition %= nrOfShuffles; // normalize
&#125;


User avatar
Matthias Gemuh
Posts: 3245
Joined: Thu Mar 09, 2006 9:10 am

Re: Anything new about the ChessV Project?

Post by Matthias Gemuh »

hgm wrote:
Matthias Gemuh wrote:I can recode my shuffling according to what your more complex shuffling demands, so just go ahead and fully define a standard.
OK, the code below seems to work. To convert the random number to a position, I peel off bits from the low end, by taking remainder, and use that to place the next piece or piece pair. For CRC the piece order is

B(light), B(dark), C, A, Q, N(pair) and fill remaining holes with KRK.

Both B are assigned with 5 possibilities, on their respective colors.
C then with 8 possibilities on any color,
A with 7
Q with 6
the N pair with 20 (= 5*4/2)

for a total of 25*8*7*6*20 = 168,000 shuffles

When I shuffle in variant capablanca, rather then caparandom, I place K and R first in their orthoox position, (f1, a1 and j1), after which the order is:

light B 3 possibilities (b1, d1, h1)
dark B 4 possibilities (c1, e1, g1, i1)
C with 5
A with 4
Q with 3
and the holes filled with Knights

This gives 12*5*4*3 = 720 possibilities.

Code: Select all

// &#91;HGM&#93; shuffle&#58; a more general way to suffle opening setups, applicable to arbitrry variants.
// All positions will have equal probability, but the current method will not provide a unique
// numbering scheme for arrays that contain 3 or more pieces of the same kind.
#define DARK 1
#define LITE 2
#define ANY 3

int squaresLeft&#91;4&#93;;
int piecesLeft&#91;&#40;int&#41;BlackPawn&#93;;
long long int seed, nrOfShuffles;



void SetUpShuffle&#40;Board board, int number&#41;
&#123;
	int i, p, first=1;

....

&#125;



Thanks for the stuff. I will integrate it into ChessGUI soon.

Matthias.



.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
User avatar
Matthias Gemuh
Posts: 3245
Joined: Thu Mar 09, 2006 9:10 am

Re: Anything new about the ChessV Project?

Post by Matthias Gemuh »

hgm wrote:
Matthias Gemuh wrote:I can recode my shuffling according to what your more complex shuffling demands, so just go ahead and fully define a standard.
OK, the code below seems to work. To convert the random number to a position, I peel off bits from the low end, by taking remainder, and use that to place the next piece or piece pair. For CRC the piece order is

B(light), B(dark), C, A, Q, N(pair) and fill remaining holes with KRK.

Both B are assigned with 5 possibilities, on their respective colors.
C then with 8 possibilities on any color,
A with 7
Q with 6
the N pair with 20 (= 5*4/2)

for a total of 25*8*7*6*20 = 168,000 shuffles

When I shuffle in variant capablanca, rather then caparandom, I place K and R first in their orthoox position, (f1, a1 and j1), after which the order is:

light B 3 possibilities (b1, d1, h1)
dark B 4 possibilities (c1, e1, g1, i1)
C with 5
A with 4
Q with 3
and the holes filled with Knights

This gives 12*5*4*3 = 720 possibilities.

Code: Select all

// &#91;HGM&#93; shuffle&#58; a more general way to suffle opening setups, applicable to arbitrry variants.
// All positions will have equal probability, but the current method will not provide a unique
// numbering scheme for arrays that contain 3 or more pieces of the same kind.
#define DARK 1
#define LITE 2
#define ANY 3

int squaresLeft&#91;4&#93;;
int piecesLeft&#91;&#40;int&#41;BlackPawn&#93;;
long long int seed, nrOfShuffles;


void SetUpShuffle&#40;Board board, int number&#41;
&#123;
	int i, p, first=1;

	seed = number, nrOfShuffles = 1;

...

	if&#40;number >= 0&#41; appData.defaultFrcPosition %= nrOfShuffles; // normalize
&#125;




Hi HGM,

In SetUpShuffle() you refer to strange chess pieces, so I don't understand things like "(p != (int) WhiteBishop && p != (int) WhiteFerz && p != (int) WhiteAlfil)". Can you provide an indexed list of them all ?

Code: Select all

int piecesLeft&#91;&#40;int&#41;BlackPawn&#93;;
How long is this array ?

Best,
Matthias.


.
My engine was quite strong till I added knowledge to it.
http://www.chess.hylogic.de
User avatar
hgm
Posts: 27837
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: Anything new about the ChessV Project?

Post by hgm »

In WinBoard 4.3.13 the list is (low to high):

Pawn
Knight
Bishop
Rook
Queen
Ferz
Elephant (Alfil)
Archbishop (Angel)
Chancellor (Marshal)
Wazir
Commoner (Man)
Cannon
Nightrider
Cardinal
Dragon
Grasshopper
Silver
Falcon
Lance
Cobra
Unicorn
King

The Alfil and Ferz are color-bound pieces, like the Bishop, which is why I treat them with priority if there are pairs.

But you can simply skip all pieces that your GUI does not implement, as in games where they do not occur they would be skipped anyway, and games were yhey do occur the GUI could not handle at all, and there would be no reason to shuffle them. So for the variants ChessGUI implements you can simply use the order P,N,B,R,Q,A,C,K. And K and R are treated separately depending on castling flavor. So the only thing that remains is that B-pairs go first, and within the remaining group the order is C,A,Q,N.