How do I model a chessboard when programming a computer to play chess?

后端 未结 14 2571
夕颜
夕颜 2021-01-31 10:13

What data structures would you use to represent a chessboard for a computer chess program?

14条回答
  •  生来不讨喜
    2021-01-31 10:41

    Initially, use an 8 * 8 integer array to represent the chess board.

    You can start programing using this notation. Give point values for the pieces. For example:

    **White**
    9 = white queen
    5 = white rook
    3 = bishop
    3 = knight
    1 = pawn
    
    **black**
    -9 = white queen
    -5 = white rook
    -3 = bishop
    -3 = knight
    -1 = pawn
    
    White King: very large positive number
    Black King: very large negative number
    

    etc. (Note that the points given above are approximations of trading power of each chess piece)

    After you develop the basic backbones of your application and clearly understand the working of the algorithms used, try to improve the performance by using bit boards.

    In bit boards, you use eight 8 -bit words to represent the boards. This representation needs a board for each chess piece. In one bit board you will be storing the position of the rook while in another you will be storing the position of the knight... etc

    Bit boards can improve the performance of your application very much because manipulating the pieces with bit boards are very easy and fast.

    As you pointed out,

    Most chessprograms today, especially those that run on a 64 bit CPU, use a bitmapped approach to represent a chessboard and generate moves. x88 is an alternate board model for machines without 64 bit CPUs.

提交回复
热议问题