Search code examples
algorithmchess

Simple algorithms for chess gamestate


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:

  • Artificial Intelligence (A.I) algorithm(s)
  • Sourcecode

The list from my past project is the following:

  • Attacking map (2 dimensional raycast). The state of the castling flag is dependent on it.
  • Castling flag
  • 2nd or 7th row flag for pawn movement.
  • En Passant flag
  • Rays (2 dimensional raycast) to check where each piece can move and attack

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.


Solution

  • 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:

    • Positions: a bitboard per piece type and color: a bit is 1 if the corresponding square has that piece. The bitboard for the white king would have exactly one bit set to 1, but the bitboard for white pawns could have up to eight bits set to 1.
    • Moves: a bitboard per piece: a bit is 1 if the piece can move to the corresponding square.
    • Attacks: a bitboard per piece: a bit is 1 if the piece attacks the corresponding square.
    • ...etc

    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:

    • You only have/need one such 8x8 array, where each cell has rich information (a byte?). Total memory size probably >= 64 bytes.
    • You would need twelve 8x8 bitboards, one per piece type and color. Total memory size = 12 longs, i.e. 48 bytes

    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:

    • Count for repetition of position, in order to detect the draw
    • Count of moves without pawn moves or captures, in order to detect the draw