user10914: OK I have read a lot of things on the web most of them using Java that doesn't have unsigned int. I am working on Objective-C that has unsigned int.
Lets consider the following scenario.
The board is like this:

A1 is on the lower left and is the least significant bit and H8 is on the top right and is the most significant bit.
I have constructed the bitmasks for the movements of all pieces for all positions on the board.
Suppose the following simple situation:

the game will start. All pieces are on their initial positions.
the user tries to move the knight on G1 to E2.

Obviously this movement is impossible because there is a pawn on E2.
How do I discover if a movement is valid?
Ok I have the bitmask of all pieces that is this:
1111111111111111000000000000000000000000000000001111111111111111
and I have the bitmask of the possible movements for the knight at G1 that is
00000000
00000000
00000000
00000000
00000000
00000X0X
0000X000
00000000

or in bits
0000000000001000000001010000000000000000000000000000000000000000
if I AND these two
1111111111111111000000000000000000000000000000001111111111111111
0000000000001000000001010000000000000000000000000000000000000000
I get
0000000000001000000000000000000000000000000000000000000000000000
all I know is that there is a piece at that point but I am not seeing how this tells me if that is a valid move or not.
Another question is this: suppose this move would let my king under attack. Obviously the move would be technically possible under the knight rules of movement but impossible because I cannot do a move that puts my king under attack.
How do I get these informations? Thanks

user9684: Make a bitboard of all the white pieces.  If the AND of the possible knight move and the white pieces is TRUE, then the move is illegal--attempting to capture your own piece.
The simplest way is to make the move and check if the King is in check.
The method I use is a little more complicated.  First I check is the King is in check now.  If the King is in check, I can delete all non-king moves which do not finish on the line the check is coming from.  If the King is not in check, I only have to check the line if the piece moves from the line.  The code seems harder to write, but the validating routine runs less lines.
To explain the latter case better.  If the King on f1 is not in check, and I move the Knight on f3, I only have to examine if a check was masked by this Knight.  If a Bishop moved from d3, I only have to examine if the opponent's Bishop or Queen is checking from c4, b5 or a6.

user825: Let me answer your questions for @MikeJones:


You will need to store the generated moves in a bitboard. The following code example should work:

unsigned int moves = .... // This gives a bitboard of where your knight can move
unsigned int whitePieces = .... // This gives a bitboard of where your white pieces are

if (moves & whitePieces) {
    // If your code reaches here, one of your moves is invalid because we can't capture our own piece
} else {
    // If your code reaches here, your moves are pseudo-legal
}

Note that I say pseudo-legal, because it's only legal if the move itself doesn't expose your own king. You'll need some logic to check it and this brings us to the second point.



Mike's solution for checking king attacks will work but it's slow. You should do what Stockfish is doing (I don't see a better and faster way). In position.cpp, Stockfish does this:

If the moving piece is the king, then the move is only legal if the destination is not under attacked by the opponent.

if (type_of(piece_on(from)) == KING)
    return type_of(m) == CASTLING || !(attackers_to(to_sq(m)) & pieces(~us));

This can be easily implemented by AND, because you will always need to keep a bitboard for white pieces and black pieces.



If the moving piece is not the king, the move is only legal if it's not pinned or it's moving along the ray towards or away from the king:

return !(pinned & from)
    ||  aligned(from, to_sq(m), square(us));

You can construct a pinned bitboard by going through all the pieces, and ask which of them is blocking the enemy attack to the king. Something like this will work:

unsigned int enemyAttacks = ... // Bitboard of where the enemy pieces can attack the king
unsigned int myPieces = .... // Bitboard of where my pieces and pawns are

if (enemyAttacks & myPieces) {
    // One of our pieces is blocking the attack
}




10914: <p>OK I have read a lot of things on the web most of them using Java that doesn't have unsigned int. I am working on Objective-C that has unsigned int.</p>

<p>Lets consider the following scenario.</p>

<p>The board is like this:</p>

<p><a href="https://i.stack.imgur.com/wvDkn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wvDkn.jpg" alt="enter image description here"></a></p>

<p><code>A1</code> is on the lower left and is the least significant bit and <code>H8</code> is on the top right and is the most significant bit.</p>

<p>I have constructed the bitmasks for the movements of all pieces for all positions on the board.</p>

<p>Suppose the following simple situation:</p>

<ol>
<li>the game will start. All pieces are on their initial positions.</li>
<li>the user tries to move the knight on <code>G1</code> to <code>E2</code>.</li>
</ol>

<p>Obviously this movement is impossible because there is a pawn on <code>E2</code>.</p>

<p>How do I discover if a movement is valid?</p>

<p>Ok I have the bitmask of all pieces that is this:</p>

<p><code>1111111111111111000000000000000000000000000000001111111111111111</code></p>

<p>and I have the bitmask of the possible movements for the knight at <code>G1</code> that is</p>

<pre><code>00000000
00000000
00000000
00000000
00000000
00000X0X
0000X000
00000000
</code></pre>

<p>or in bits</p>

<p><code>0000000000001000000001010000000000000000000000000000000000000000</code></p>

<p>if I <code>AND</code> these two</p>

<p><code>1111111111111111000000000000000000000000000000001111111111111111</code>
<code>0000000000001000000001010000000000000000000000000000000000000000</code></p>

<p>I get</p>

<p><code>0000000000001000000000000000000000000000000000000000000000000000</code></p>

<p>all I know is that there is a piece at that point but I am not seeing how this tells me if that is a valid move or not.</p>

<p>Another question is this: suppose this move would let my king under attack. Obviously the move would be technically possible under the knight rules of movement but impossible because I cannot do a move that puts my king under attack.</p>

<p>How do I get these informations? Thanks</p>
 9684: <p>Make a bitboard of all the white pieces.  If the AND of the possible knight move and the white pieces is TRUE, then the move is illegal--attempting to capture your own piece.</p>

<p>The simplest way is to make the move and check if the King is in check.</p>

<p>The method I use is a little more complicated.  First I check is the King is in check now.  If the King is in check, I can delete all non-king moves which do not finish on the line the check is coming from.  If the King is not in check, I only have to check the line if the piece moves from the line.  The code seems harder to write, but the validating routine runs less lines.</p>

<p>To explain the latter case better.  If the King on f1 is not in check, and I move the Knight on f3, I only have to examine if a check was masked by this Knight.  If a Bishop moved from d3, I only have to examine if the opponent's Bishop or Queen is checking from c4, b5 or a6.</p>
 825: <p>Let me answer your questions for @MikeJones:</p>

<p><br></p>

<ol>
<li><p>You will need to store the generated moves in a bitboard. The following code example should work:</p>

<pre>
unsigned int moves = .... // This gives a bitboard of where your knight can move
unsigned int whitePieces = .... // This gives a bitboard of where your white pieces are

if (moves & whitePieces) {
    // If your code reaches here, one of your moves is invalid because we can't capture our own piece
} else {
    // If your code reaches here, your moves are pseudo-legal
}
</pre>

<p>Note that I say <code>pseudo-legal</code>, because it's only legal if the move itself doesn't expose your own king. You'll need some logic to check it and this brings us to the second point.</p></li>
</ol>

<p><br></p>

<ol start="2">
<li><p>Mike's solution for checking king attacks will work but it's slow. You should do what Stockfish is doing (I don't see a better and faster way). In <a href="https://github.com/official-stockfish/Stockfish/blob/master/src/position.cpp" rel="nofollow">position.cpp</a>, Stockfish does this:</p>

<ul>
<li><p>If the moving piece is the king, then the move is only legal if the destination is not under attacked by the opponent.</p>

<pre>
if (type_of(piece_on(from)) == KING)
    return type_of(m) == CASTLING || !(attackers_to(to_sq(m)) & pieces(~us));
</pre>

<p>This can be easily implemented by AND, because you will always need to keep a bitboard for white pieces and black pieces.</p></li>
</ul>

<p><br></p>

<ul>
<li><p>If the moving piece is not the king, the move is only legal if it's not pinned or it's moving along the ray towards or away from the king:</p>

<pre>
return !(pinned & from)
    ||  aligned(from, to_sq(m), square(us));
</pre>

<p>You can construct a pinned bitboard by going through all the pieces, and ask which of them is blocking the enemy attack to the king. Something like this will work:</p>

<pre>
unsigned int enemyAttacks = ... // Bitboard of where the enemy pieces can attack the king
unsigned int myPieces = .... // Bitboard of where my pieces and pawns are

if (enemyAttacks & myPieces) {
    // One of our pieces is blocking the attack
}
</pre></li>
</ul></li>
</ol>
