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

后端 未结 14 2282
夕颜
夕颜 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

    An array would probably be fine. If you wanted more convenient means of "traversing" the board, you could easily build methods to abstract away the details of the data structure implementation.

    0 讨论(0)
  • 2021-01-31 10:44

    When creating my chess engine I originally went with the [8][8] approach, however recently I changed my code to represent the chess board using a 64 item array. I found that this implementation was about 1/3 more efficient, at least in my case.

    One of the things you want to consider when doing the [8][8] approach is describing positions. For example if you wish to describe a valid move for a chess piece, you will need 2 bytes to do so. While with the [64] item array you can do it with one byte.

    To convert from a position on the [64] item board to a [8][8] board you can simply use the following calculations:

    Row= (byte)(index / 8)

    Col = (byte)(index % 8)

    Although I found that you never have to do that during the recursive move searching which is performance sensitive.

    For more information on building a chess engine, feel free to visit my blog that describes the process from scratch: www.chessbin.com

    Adam Berent

    0 讨论(0)
提交回复
热议问题