whether or not a piece has moved and how many times

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

smcracraft
Posts: 737
Joined: Wed Mar 08, 2006 8:08 pm
Location: Orange County California
Full name: Stuart Cracraft

whether or not a piece has moved and how many times

Post by smcracraft »

Curious about how others have implemented calculation of how many times each piece has moved since the beginning of the game.

The concern is when a piece has been captured in a make move and then uncaptured and updating the array.

How did you solve this?
MOBMAT
Posts: 385
Joined: Sat Feb 04, 2017 11:57 pm
Location: USA

Re: whether or not a piece has moved and how many times

Post by MOBMAT »

smcracraft wrote: Tue Jun 12, 2018 6:08 am Curious about how others have implemented calculation of how many times each piece has moved since the beginning of the game.

The concern is when a piece has been captured in a make move and then uncaptured and updating the array.
If you are just interested in keeping track of which move was made so you can "un-make" it, then you will already have that information available in your tree search. after all, you can't make a move without knowing what it is. when you return from the deeper search, the move that was made will still be on the stack at the original level. The count wouldn't matter.

I use to think that keeping track of the number of times a move was made would be useful for the opening and perhaps keeping track of castling options, but there are other ways to handle those cases more efficiently.

For castling, i keep 4 bits in a byte to indicate if castling is still possible for each of the two ways for each color. This is a pretty common way to do it if you are viewing some open source code. If a move is made from the original king square (E1 for white) or either of the rook squares (A1 or H1), then clear the appropriate bits. same goes for black. Also, if a move is made to one of the rook squares, the appropriate bit is also cleared. These bits are passed down to the next depth and used to guide the code as to whether or not castling is possible (bit set, good to go). They do though have to be set properly at the beginning of the game, or if a new FEN is used to set up a position. I've found that sometimes you can't trust the FEN and the castling flags might not even be present. so I use the positions of the pieces as the guide. if the pieces on the board indicate castling is possible, and the FEN castling flags aren't present, I go with what the pieces say.

Back to keeping track of the number of times a piece has moved, it is kind of a waste of instructions. A good evaluation will kind of, sort of take care of that for you. Others may have other ideas.
i7-6700K @ 4.00Ghz 32Gb, Win 10 Home, EGTBs on PCI SSD
Benchmark: Stockfish15.1 NNUE x64 bmi2 (nps): 1277K
smcracraft
Posts: 737
Joined: Wed Mar 08, 2006 8:08 pm
Location: Orange County California
Full name: Stuart Cracraft

Re: whether or not a piece has moved and how many times

Post by smcracraft »

Well, that wasn't really the answer to my question now, was it?
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: whether or not a piece has moved and how many times

Post by Joost Buijs »

Also my engine only keeps track of whether the king or the rooks have moved, maybe a waste of cache or memory but I use 4 bits in a 64 bit word to keep track of this because this maps directly to the rook locations on the bitboards.
Joost Buijs
Posts: 1563
Joined: Thu Jul 16, 2009 10:47 am
Location: Almere, The Netherlands

Re: whether or not a piece has moved and how many times

Post by Joost Buijs »

smcracraft wrote: Tue Jun 12, 2018 7:44 am Well, that wasn't really the answer to my question now, was it?
To me the answer seemed very clear, I don't think that there are many engines (if any) keeping track of how many times a piece has moved because this is rather useless information.
AlvaroBegue
Posts: 931
Joined: Tue Mar 09, 2010 3:46 pm
Location: New York
Full name: Álvaro Begué (RuyDos)

Re: whether or not a piece has moved and how many times

Post by AlvaroBegue »

smcracraft wrote: Tue Jun 12, 2018 6:08 am Curious about how others have implemented calculation of how many times each piece has moved since the beginning of the game.

The concern is when a piece has been captured in a make move and then uncaptured and updating the array.

How did you solve this?
Why do you think anyone has ever solved this? Why is this an interesting problem?
User avatar
Evert
Posts: 2929
Joined: Sat Jan 22, 2011 12:42 am
Location: NL

Re: whether or not a piece has moved and how many times

Post by Evert »

smcracraft wrote: Tue Jun 12, 2018 6:08 am Curious about how others have implemented calculation of how many times each piece has moved since the beginning of the game.

The concern is when a piece has been captured in a make move and then uncaptured and updating the array.

How did you solve this?
I'd do it the same way I handle the 50-move counter, or castling rights.
Question remains, why do you want this information? If it's for something evaluation or move ordering related, you're probably on the wrong track.
User avatar
hgm
Posts: 27787
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: whether or not a piece has moved and how many times

Post by hgm »

None of my engines keep track of that. But it seems trivial to do so: just keep a counter for each piece, as one of the infos in the piece list. Every MakeMove() increments the counter for the moved piece, every UnMake() decrements it. For castling you would have to do that for both K and R, as they are both moved.
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: whether or not a piece has moved and how many times

Post by syzygy »

smcracraft wrote: Tue Jun 12, 2018 7:44 am Well, that wasn't really the answer to my question now, was it?
You question is "How did you solve this?"

The only answer I can give is "I did not solve it because I have no need whatsoever to know the number of times a piece has been moved since the beginning of the game".

As far as I can tell, the number of times a piece has been moved has no relevance to the game of chess. I doubt that any chess engine exists that keep track of this number.

But if you ask me "how would you solve this?" then my answer would be: just don't zero the number for a piece when the piece is captured. Then when you uncapture it, the number is still there.
syzygy
Posts: 5557
Joined: Tue Feb 28, 2012 11:56 pm

Re: whether or not a piece has moved and how many times

Post by syzygy »

syzygy wrote: Tue Jun 12, 2018 8:29 pm But if you ask me "how would you solve this?" then my answer would be: just don't zero the number for a piece when the piece is captured. Then when you uncapture it, the number is still there.
If this is impractical, just save the number of times the captured piece had been moved in the undo stack in the same way as you save the captured piece itself.