Building a chess using magic bitboard... how do I know if the movement is valid 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
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.