piece- to vs. from- to

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

voyagerOne
Posts: 154
Joined: Tue May 17, 2011 8:12 pm

piece- to vs. from- to

Post by voyagerOne »

Hi all.

My engine move structure is based upon piece to destination square. I noticed a majority of engines do from - to.

My question is...is one method superior over the other? If so what are the advantage/disadvantages...

Thanks in advance.
User avatar
hgm
Posts: 27811
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: piece- to vs. from- to

Post by hgm »

I don't think it matters much. In MakeMove I need both the piece and the from-square. Whether I do from=pos[piece]; or piece=board[from]; makes no difference.

I point of concern could be the encoding in the hash table. Zobrist keys are constructed such that you would get the same key after swapping two pieces of the same type. So you could in theory get a hit on a position with swapped material, and then the piece stored in the hash would not mean the same thing.
voyagerOne
Posts: 154
Joined: Tue May 17, 2011 8:12 pm

Re: piece- to vs. from- to

Post by voyagerOne »

Thanks for your feedback, I really appreciate it!

Another unorthodox method I use is:
I have a bitboard for each piece so 16 bbs not 6 for each color. I also have a unionPawn bb.

To me my program seems quite fast.
JAVA
1 Thread
Perft
Count legal Moves
20 Mn/s

I am wondering if this is an efficient way...or I should change to 6 bbs.

Thanks again.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: piece- to vs. from- to

Post by bob »

voyagerOne wrote:Thanks for your feedback, I really appreciate it!

Another unorthodox method I use is:
I have a bitboard for each piece so 16 bbs not 6 for each color. I also have a unionPawn bb.

To me my program seems quite fast.
JAVA
1 Thread
Perft
Count legal Moves
20 Mn/s

I am wondering if this is an efficient way...or I should change to 6 bbs.

Thanks again.
More common is 12, 1 for each different piece type/color. That is, white knights on one, black queens on another, etc... Not one for each individual piece, that is somewhat "overkill"
voyagerOne
Posts: 154
Joined: Tue May 17, 2011 8:12 pm

Re: piece- to vs. from- to

Post by voyagerOne »

Then how do you calculate pawn moves? In other words you need to do additional logic for each pawn, right? Otherwise pawn A will go to pawn H destination square...


The way I do it is something like this:

for i = 0 to 7

if(pawn==0)continue;

calculateAllDestinationMoves(i)

next i


move[piece][to]

move[getLSBpawn]
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: piece- to vs. from- to

Post by bob »

voyagerOne wrote:Then how do you calculate pawn moves? In other words you need to do additional logic for each pawn, right? Otherwise pawn A will go to pawn H destination square...


The way I do it is something like this:

for i = 0 to 7

if(pawn==0)continue;

calculateAllDestinationMoves(i)

next i

move[piece][to]

move[getLSBpawn]


No, you can generate all pawn moves at once. Non-captures for white pawns, assuming a1=0, h8=63,

push1 = (WhitePawns << 8) & ~Occupied. Now you have a bitboard where every white pawn has advanced forward one square. If you want the double advances, you can add: push2 = ((push1 & mask_3rd_rank) << 8) & ~Occupied and now push 2 has bits set for any pawn that could advance two squares at once. We and with the complement of Occupied since pushes can only be to empty squares.

For captures, shift left 7 or 9 and then AND with the black_occupied bitboard to only leave diagonal pawn moves that capture something as the rest would be illegal...

That's the idea of bitboards, work with "sets" rather than with "individual things."
voyagerOne
Posts: 154
Joined: Tue May 17, 2011 8:12 pm

Re: piece- to vs. from- to

Post by voyagerOne »

Right...I understand that perfectly. But my question is don't you need additional logic to make sure the right piece goes to the right destination square.

Example:
There are two pawns on the board. b2 and f2. So you generate moves for all the pawns...and there are only two available moves. b3 and f3.

My question is don't you need additional logic...so you don't move b2 to f3?
kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: piece- to vs. from- to

Post by kbhearn »

In the case of pawn moves, you always know the origin square for each type of move, so when you're serialising the set of single pawn pushes you know if you get f3 it has to be coming from the pawn on f2.

So for white you get (with a1=0, h1 = 7 numbering):
pawnpush = whitepawns << 8 & !occupied;
pawndoublepush = pawnpush << 8 & !occupied & RANK4;
pawncapa = (whitepawns & NOTA) << 7 & occupiedblack;
pawncaph = (whitepawns & NOTH) << 9 & occupiedblack;

and serialise the results with the understanding that each to square in each destination set, there can be only one from square.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: piece- to vs. from- to

Post by bob »

voyagerOne wrote:Right...I understand that perfectly. But my question is don't you need additional logic to make sure the right piece goes to the right destination square.

Example:
There are two pawns on the board. b2 and f2. So you generate moves for all the pawns...and there are only two available moves. b3 and f3.

My question is don't you need additional logic...so you don't move b2 to f3?
For pawn pushes, if you know "to", isn't "from" == "to - 8" for white? For double pushes, "from" == "to -16" for white? For captures to the left, from = to - 7, and for captures to the right, from = to - 9...
voyagerOne
Posts: 154
Joined: Tue May 17, 2011 8:12 pm

Re: piece- to vs. from- to

Post by voyagerOne »

So with your pawn bit board:

You take each individual bit and calculate its move?