Duck
Duck

Reputation: 35953

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:

enter image description here

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:

  1. the game will start. All pieces are on their initial positions.
  2. 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

Upvotes: 1

Views: 1062

Answers (1)

Myridium
Myridium

Reputation: 904

If you only have a bitmask of all pieces then you simply do not have any way of knowing whether you're trying to move the knight onto an opponent's piece or your own. You will have to use a separate bitmask for white pieces and for the black pieces.

You already have the bitmask K of all possible moves of the knight before considering the locations of other pieces. Just bitwise AND this with the NOT of the where the white pieces are: M = K && !W

Now as far as I can think, the only way that a move of the knight would endanger its king is if the knight was previously blocking an opponent's attack. Specifically, only if it was blocking the attack of a rook or a bishop. This is only possible if the knight was positioned in one of the eight cardinal directions from the location of the king. So check to see if there is a straight, unblocked line from the king to knight in one of the eight cardinal directions. If there isn't, then the move could not possibly endanger the king, so you needn't do any more checks. If there is, then loop through the positions of M to check to see if the king will be endangered after each of them.

In the special case of the knight, since it travels in an L shape, there is no way that it can move from a position where it was blocking this attack to another position where it continued to thwart it. So in the special case of the knight, you should do the following:

  1. Check if there is an uninterrupted straight line from the king to the knight. If not, go to 4.
  2. Check if there is an opponent rook or bishop along this line whose only obstacle to taking the king is the knight you are trying to move. If not, go to 4.
  3. If you've reached this point, then moving the knight would result in check. It cannot be moved. Set M = 0 and continue with the program.
  4. Set M = K && !W and continue with the program.

I should note that this was written assuming that the king did not begin in check. Adjust as necessary.

Upvotes: 1

Related Questions