Check detection and move generation using bitboards

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Lasse Hansen
Posts: 27
Joined: Wed May 28, 2008 1:07 pm
Location: Porsgrunn, Norway

Re: Check detection and move generation using bitboards

Post by Lasse Hansen »

In the sample code for queen moves, after the quasi-legal attack board is generated, there is code to detect if the piece is within the range of the king as a 'superpiece' (except knight moves).

Code: Select all

      B = SetBit(fr) & MaskRook[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
      if (B) { // Possible rook pin
         B = Atk & MaskRook[fr] & MaskRook[ksqr];
         if (B & (Pieces[!wtm][Rook] | Pieces[!wtm][Queen])) {
            Atk = B;
            goto RollQueen;
         }
      }
If in range, (B not empty), then I look at the possibly pinned queen as a rook, to detect if any opponent rooks or queens is within the range of the queen and the king, using MaskRook. If so, the piece is pinned and the attack board Atk holds the legal moves within the pin. Of course, if the piece was a knight, the code would just say 'continue' because of no legal moves.

Code: Select all

	Pcs = Pieces[wtm][Knight];
	while (Pcs) {
		fr = FirstOne(B=Pcs&-Pcs);								
		Atk = KnightAttacks[fr];
		Atk ^= Atk & Pieces[wtm][ALL];
		Pcs ^= B;

		B = SetBit(fr) & MaskRook[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible rook pin
			B = RookAttacks(fr) & MaskRook[ksqr];
			if (B & (Pieces[!wtm][Rook] | Pieces[!wtm][Queen])) continue; 
		}
		B = SetBit(fr) & MaskBishop[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible bishop pin
			B = BishopAttacks(fr) & MaskBishop[ksqr];
			if (B & (Pieces[!wtm][Bishop] | Pieces[!wtm][Queen])) continue; 
		}
		fr = (fr<<6)+(((wtm<<3)+Knight)<<12);
		while (Atk) {
			to=FirstOne(B=Atk&-Atk);
			*M++=fr+to+(Board[to]<<16);
			Atk ^= B;
		}
	}
Regards, Lasse
User avatar
Lasse Hansen
Posts: 27
Joined: Wed May 28, 2008 1:07 pm
Location: Porsgrunn, Norway

Re: Check detection and move generation using bitboards

Post by Lasse Hansen »

From Bob:

Code: Select all

How can you be sure all those "last ply" moves are legal? Perft does not count illegal moves...
Some sample code for the last ply in my perft function.

Code: Select all

	Pcs = Pieces[wtm][Rook];
	while (Pcs) {
		fr = FirstOne(C=Pcs&-Pcs);								
		Atk = RookAttacks(fr);
		Atk ^= Atk & Pieces[wtm][ALL];
		Pcs ^= C;
		B = SetBit(fr) & MaskRook[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible rook pin
			B = Atk & MaskRook[ksqr];
			if (B & (Pieces[!wtm][Rook] | Pieces[!wtm][Queen])) {
				nSafeMoves += PopCount(B); // Rook moves in pin direction
				continue; // Rook cannot be pinned to both rooks and bishops
			} 
		}
		B = SetBit(fr) & MaskBishop[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible bishop pin
			B = BishopAttacks(fr) & MaskBishop[ksqr];
			if (B & (Pieces[!wtm][Bishop] | Pieces[!wtm][Queen])) {
				continue; // A rook pinned to bishop has no moves
			} 
		}
		nSafeMoves += PopCount(Atk); // All moves are legal
	}


	// Double pawn step
	Pcs = (wtm<<3)+Pawn;
	Atk = (Pieces[wtm][Pawn]<<8)>>(wtm<<4);
	Atk &= ~(Pieces[BLACK][ALL] | Pieces[WHITE][ALL]);
	B=Atk; // 1-step move pattern
	Atk &= RANK_A6H6 << 3*8*wtm;
	Atk = ((Atk<<8)>>(wtm<<4)) & ~(Pieces[BLACK][ALL] | Pieces[WHITE][ALL]);
	C = (Atk>>16)<<(wtm<<5);
	nSafeMoves += PopCount(C);
	Atk = C & PosInfo[ply].KingVulnAtk[wtm];
	while (Atk) { // Dirty moves
		fr = FirstOne(C=Atk&-Atk);
		to = fr + 16 - (wtm<<5);
		*M++=(fr<<6)+to+(Pcs<<12);
		Atk ^= C;
		nSafeMoves--;
	}	
I use popcount for moves that has to be legal, those not I roll out to movelist for later legality verification.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Check detection and move generation using bitboards

Post by bob »

Sven Schüle wrote:
Lasse Hansen wrote:One nice thing is that when not in check, all queen, rook, bishop and knight moves are legal by default, pinned or not.
Could you explain this, please? To my understanding, moving a pinned knight is always illegal, as well as for instance moving a rook pinned by a bishop or a bishop pinned by a rook, etc. So maybe I did not get your point in that sentence above.

Sven
You can also have a bishop pinned by a bishop, so it can only move on one diagonal, ditto for a rook or queen...
User avatar
Lasse Hansen
Posts: 27
Joined: Wed May 28, 2008 1:07 pm
Location: Porsgrunn, Norway

Re: Check detection and move generation using bitboards

Post by Lasse Hansen »

Bob wrote:

You can also have a bishop pinned by a bishop, so it can only move on one diagonal, ditto for a rook or queen...

Yes, my bishop move generation at leaf nodes is:

Code: Select all

	Pcs = Pieces[wtm][Bishop];
	while (Pcs) {
		fr = FirstOne(C=Pcs&-Pcs);								
		Atk = BishopAttacks(fr);
		Atk ^= Atk & Pieces[wtm][ALL];
		Pcs ^= C;
		B = SetBit(fr) & MaskBishop[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible bishop pin
			B = Atk & MaskBishop[ksqr];
			if (B & (Pieces[!wtm][Bishop] | Pieces[!wtm][Queen])) {
				nSafeMoves += PopCount(B); 
				continue; 
			} 
		}
		B = SetBit(fr) & MaskRook[ksqr] & PosInfo[ply].KingVulnAtk[wtm];
		if (B) { // Possible rook pin
			B = RookAttacks(fr) & MaskRook[ksqr];
			if (B & (Pieces[!wtm][Rook] | Pieces[!wtm][Queen])) {
				continue; 
			} 
		}
		nSafeMoves += PopCount(Atk); // All moves are legal
	}
This generates all legal moves for a bishop, pinned to king by a queen, rook or bishop, or not pinned at all.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A reminder

Post by bob »

sje wrote:A reminder:

A FEN record always has EXACTLY SIX fields. No program is obligated to interpret an invalid FEN record, and no program should generate an invalid FEN record.

Also, the ordering of the castling availability is KQkq.

A sample:

Code: Select all

r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1
Another reminder. :)

One can be draconian, or flexible. For example, how do you enter castle short?

o-o, O-O, or 0-0? Last one is zero-zero.

I learned YEARS ago to accept all of those, otherwise parsing book moves from PGN collections was basically impossible. My take has always been to take whatever is recognizable...
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: A reminder

Post by sje »

bob wrote:
sje wrote:A FEN record always has EXACTLY SIX fields. No program is obligated to interpret an invalid FEN record, and no program should generate an invalid FEN record.
One can be draconian, or flexible. For example, how do you enter castle short?

o-o, O-O, or 0-0? Last one is zero-zero.
Flexible on input, draconian on output.

Anyway, for years nearly all chess data is generated via a program, either an engine or a GUI. So guessing at non-standard move input isn't all that important nowadays.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: A reminder

Post by bob »

sje wrote:
bob wrote:
sje wrote:A FEN record always has EXACTLY SIX fields. No program is obligated to interpret an invalid FEN record, and no program should generate an invalid FEN record.
One can be draconian, or flexible. For example, how do you enter castle short?

o-o, O-O, or 0-0? Last one is zero-zero.
Flexible on input, draconian on output.

Anyway, for years nearly all chess data is generated via a program, either an engine or a GUI. So guessing at non-standard move input isn't all that important nowadays.
Problem is, there are MILLIONS of games that were not produced by a computer. Can't do much about the ones with illegal moves, of course, but incorrect syntax can be handled. Things like 1.e4 rather than 1. e4, for example, seems to be pretty common.
Sven
Posts: 4052
Joined: Thu May 15, 2008 9:57 pm
Location: Berlin, Germany
Full name: Sven Schüle

Re: Check detection and move generation using bitboards

Post by Sven »

bob wrote:
Sven Schüle wrote:
Lasse Hansen wrote:One nice thing is that when not in check, all queen, rook, bishop and knight moves are legal by default, pinned or not.
Could you explain this, please? To my understanding, moving a pinned knight is always illegal, as well as for instance moving a rook pinned by a bishop or a bishop pinned by a rook, etc. So maybe I did not get your point in that sentence above.

Sven
You can also have a bishop pinned by a bishop, so it can only move on one diagonal, ditto for a rook or queen...
I know, Bob. My point was, the OP wrote "are legal by default" and I was not sure whether he meant "in general" or "with my move generator". The term "by default" was not clear. Now it is.

Sven
User avatar
sje
Posts: 4675
Joined: Mon Mar 13, 2006 7:43 pm

Re: A reminder

Post by sje »

bob wrote:
sje wrote:Flexible on input, draconian on output.

Anyway, for years nearly all chess data is generated via a program, either an engine or a GUI. So guessing at non-standard move input isn't all that important nowadays.
Problem is, there are MILLIONS of games that were not produced by a computer. Can't do much about the ones with illegal moves, of course, but incorrect syntax can be handled. Things like 1.e4 rather than 1. e4, for example, seems to be pretty common.
That may be true, but very little new ASCII chess data is created by hand. Eventually, bad PGN data will be hard to find, just like English Description Notation games are hard to find today.

The "1.e4" vs "1. e4" is a minor issue compared to getting the SAN incorrect. Unfortunately, there may still be some programs that crank out bad SAN on occasion; Stockfish was one of these until a week ago.

[d]

Moves:

Code: Select all

Bd2 Be2 Be3 Bf4 Bg5 Bh6 Kd2 Ke2 Ne2 Nf3 Nh3 Qd2 Qe2 Qf3 Qg4 Qh5 Rb1 a3 a4 b3 d4 f3 f4 g3 g4 h3 h4
Note that it's Ne2 and not Nge2.

Stockfish has been fixed, but there way be other programs with the same bug. For anyone with an interest, I've got a PGN file with a million random games for use in testing. And now that I've got a Windows 7 machine, I can test a lot of commercial software. Well, if vendors will send me evaluation copies.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Check detection and move generation using bitboards

Post by bob »

Sven Schüle wrote:
bob wrote:
Sven Schüle wrote:
Lasse Hansen wrote:One nice thing is that when not in check, all queen, rook, bishop and knight moves are legal by default, pinned or not.
Could you explain this, please? To my understanding, moving a pinned knight is always illegal, as well as for instance moving a rook pinned by a bishop or a bishop pinned by a rook, etc. So maybe I did not get your point in that sentence above.

Sven
You can also have a bishop pinned by a bishop, so it can only move on one diagonal, ditto for a rook or queen...
I know, Bob. My point was, the OP wrote "are legal by default" and I was not sure whether he meant "in general" or "with my move generator". The term "by default" was not clear. Now it is.

Sven
That was the reason I responded as well. I generally think "pseudo-legal" for efficiency reasons.