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

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

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

14条回答
  •  梦毁少年i
    2021-01-31 10:36

    I would use a multidimensional array so that each element in an array is a grid reference to a square on the board.

    Thus

    board = arrary(A = array (1,2,3,4,5,5,6,7,8),
                   B = array (12,3,.... etc...
                   etc...          
                   )
    

    Then board[A][1] is then the board square A1.

    In reality you would use numbers not letters to help keep your maths logic for where pieces are allowed to move to simple.

提交回复
热议问题