Since I worked on a chess game a couple of years back, I have a list of things that is expected for the gameplay to work, but I could need some extra advice to simplify the methods I used in the past.
I used rays (a 2d vector) to check where a piece could move and attack, but I found the code to be a bit too jerky (for example had to make exceptions for knight moves). Is there a general method to check for moves and attacks?
I have also heard about bit-boards, but how do they work, what are the purpose of bitboards? I use 8x8 array to describe what color and what kind of piece is at a specified location. Is this the same as bitboard? Can I use bitboard to check for valid moves. What about the En Passant rule?
Im not asking for:
The list from my past project is the following:
I want my code to be as small as possible, and need the key methods for checking the validity of the moves and attacks. I ask for no sourcecode, only how the methods work.
Thanks.
I will try to give "some extra advice to simplify the methods" you may have used, which is not that easy, as you did not provide code for these methods.
Bit-boards are indeed a solution. Each of the 64 bits represents a square on the board.
Depending on the programming language you use, 64 bits can be represented in a single primitive value (long
), allowing for very fast operations,
like a bit-wise AND of two bitboards.
You would have several of them, and they can serve different purposes:
See also Best way to design chess game
I use 8x8 array to describe what color and what kind of piece is at a specified location. Is this the same as bitboard?
That is also a possible approach, but it is quite different and not so efficient:
Can I use bitboard to check for valid moves. What about the En Passant rule?
Yes, you can use bitboards to check for valid moves. In that case the bitboard has to be 15x15, where the piece is assumed to be present in the center square. Bits are set to 1 if the piece can move there (if there is no obstruction). You would still have to do some operations to map this to an actual 8x8 bitboard that represents the current position of an actual piece on the board, and then find out which of those represent valid moves (by using fast AND operations). For pawn capturing moves, you would use separate bitboards. For "en passant" this will not work that well, as you would need extra logic to interpret the bits, so you might as well apply the logic without the use of a bitboard.
Bitboards do not solve all problems, but make some easier. You still need to implement the logic for castling, en passant, promotion, pinning, check, stalemate, mate, ...etc.
You may also want to add to the chess state: