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

后端 未结 14 2284
夕颜
夕颜 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:29

    There are of course a number of different ways to represent a chessboard, and the best way will depend on what is most important to you.

    Your two main choices are between speed and code clarity.

    If speed is your priority then you must use a 64 bit data type for each set of pieces on the board (e.g. white pawns, black queens, en passant pawns). You can then take advantage of native bitwise operations when generating moves and testing move legality.

    If clarity of code is priority then forget bit shuffling and go for nicely abstracted data types like others have already suggested. Just remember that if you go this way you will probably hit a performance ceiling sooner.

    To start you off, look at the code for Crafty (C) and SharpChess (C#).

提交回复
热议问题