Stockfish Chess 8 - en passant rule

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

Norbert Raimund Leisner
Posts: 1643
Joined: Tue May 20, 2008 4:57 pm
Location: Augsburg - Germany

Stockfish Chess 8 - en passant rule

Post by Norbert Raimund Leisner »

Hello,

how did the SF team accomplish the problem with "en passant" capture of a pawn?

see https://en.wikipedia.org/wiki/En_passant

cf. https://www.fide.com/FIDE/handbook/LawsOfChess.pdf

3.7
a. The pawn may move forward to the unoccupied square immediately in front of it on the same file, or
b. on its first move the pawn may move as in 3.7.a or alternatively it may advance two squares along the same file provided both squares are unoccupied, or
c. the pawn may move to a square occupied by an opponent’s piece, which is diagonally in front of it on an adjacent file, capturing that piece.
d. A pawn attacking a square crossed by an opponent’s pawn which has advanced two squares in one move from its original square may capture this opponent’s pawn as though the latter had been moved only one square. This capture is only legal on the move following this advance and is called an ‘en passant’ capture.

I ask you because StuckFish https://github.com/MaxCarlson/StuckFish has obviously no knowledge about "en passant" (in passing)
FIDE rule.

[White "ChessBrainVB"]
[Black "StuckFish"]
[Result "1-0"]
[TimeControl "40/240:40/240:40/240"]
[Opening "French"]
[ECO "C10"]
[Variation "3.Nc3 Nc6"]
[Termination "rules infraction"]
[PlyCount "23"]
[WhiteType "program"]
[BlackType "program"]

1.e4 e6 2. d4 d5 3. Nc3 Nc6 4. Nf3 Bb4 5. e5 Nge7 6. Bd3 b6 7. O-O Bxc3 8. bxc3 h6 9.Nh4 Bb7 10. Qg4 g5 11. Nf3 f5 12. exf6 1-0

A sceenshot of the concerning "en passant" section (>source code) would probably be useful in this case.

Best wishes,
Norbert
nitrocan
Posts: 17
Joined: Fri Sep 15, 2017 11:52 pm

Re: Stockfish Chess 8 - en passant rule

Post by nitrocan »

Here is Stockfish's implementation:

https://github.com/official-stockfish/S ... sition.cpp

Namely

Code: Select all

      // Set en-passant square if the moved pawn can be captured
      if (   (int(to) ^ int(from)) == 16
          && &#40;attacks_from<PAWN>&#40;to - pawn_push&#40;us&#41;, us&#41; & pieces&#40;them, PAWN&#41;))
      &#123;
          st->epSquare = &#40;from + to&#41; / 2;
          k ^= Zobrist&#58;&#58;enpassant&#91;file_of&#40;st->epSquare&#41;&#93;;
      &#125;
and

Code: Select all

          if &#40;type_of&#40;m&#41; == ENPASSANT&#41;
          &#123;
              capsq -= pawn_push&#40;us&#41;;

              assert&#40;pc == make_piece&#40;us, PAWN&#41;);
              assert&#40;to == st->epSquare&#41;;
              assert&#40;relative_rank&#40;us, to&#41; == RANK_6&#41;;
              assert&#40;piece_on&#40;to&#41; == NO_PIECE&#41;;
              assert&#40;piece_on&#40;capsq&#41; == make_piece&#40;them, PAWN&#41;);

              board&#91;capsq&#93; = NO_PIECE; // Not done by remove_piece&#40;)
          &#125;
EDIT: Similar code can be found in Stuckfish:

https://github.com/MaxCarlson/StuckFish ... boards.cpp
Tony P.
Posts: 216
Joined: Sun Jan 22, 2017 8:30 pm
Location: Russia

Re: Stockfish Chess 8 - en passant rule

Post by Tony P. »

I'm not a specialist in Stuckfish's source code, but a quick look at it reveals that its zobristh.cpp (the Zobrist hash implementation that's apparently different from Stockfish's) contains the following comment (line 146):

//EnPassant and castling stuff add later

Besides, the author (MaxCarlson) states in the titles of the latest commits to a lot of files, 'Not working commit. Added EP stuff, still non functional though.'

So it appears that, in its current state, Stuckfish doesn't save the info about the possibility of an e.p. move correctly into the TT.

In the game in question, it might have encountered the position arising after Black's move 11 in some earlier search and saved it into the TT without extending the search (perhaps the possibility of e.p. doesn't automatically trigger q-search in Stuckfish, which is a bug), 'forgetting' that e.p. is possible there, and then, after this position appeared on the board, Stuckfish retrieved it from the TT, mistakenly thinking that e.p. is impossible.

You've been unlucky to test Stuckfish at the moment when its e.p. handling is yet broken as of the last commit that happened 10 hours ago. Wait until the author fixes the 'e.p. stuff'.
Tony P.
Posts: 216
Joined: Sun Jan 22, 2017 8:30 pm
Location: Russia

Re: Stockfish Chess 8 - en passant rule

Post by Tony P. »

There is code for e.p. move making, though (see the below code snippet from bitboards.cpp, lines 311-331), and the e.p. square is saved in the game state variable 'st', but for some reason, the code isn't working correctly yet. The zobrist.zEnPassant function is definitely not working yet.

Code: Select all

if &#40;m.piece == PAWN&#41; &#123;

		//if the pawn move is two forward,
		//and there is an enemy pawn positioned to en passant
		if (&#40;m.to ^ m.from&#41; == 16 
			&& &#40;psuedoAttacks&#40;PAWN, color, m.from + pawn_push&#40;color&#41;) & pieces&#40;them, PAWN&#41;)) &#123;
			
			st->epSquare = &#40;m.from + m.to&#41; / 2;
			zobrist.zobristKey ^= zobrist.zEnPassant&#91;file_of&#40;st->epSquare&#41;&#93;;

		&#125; 
		//en passant
		else if &#40;m.flag == 'E') &#123;
			int ePawnSq = ep_square&#40;) + pawn_push&#40;them&#41;;
			//remove the pawn captured by ep
			removePiece&#40;PAWN, them, ePawnSq&#41;;

			zobrist.zobristKey ^= zobrist.zArray&#91;them&#93;&#91;PAWN&#93;&#91;ePawnSq&#93;;
			bInfo.PawnKey      ^= zobrist.zArray&#91;them&#93;&#91;PAWN&#93;&#91;ePawnSq&#93;;
			bInfo.sideMaterial&#91;them&#93; -= SORT_VALUE&#91;PAWN&#93;;
		&#125;
Anyway, my point is that the malfunctioning has been caused by the refactoring that MaxCarlson has been doing, but the original Stockfish code of course handles e.p. correctly.