Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I am currently implementing something quite similar to checkers. So, I have this table game and there are both white and black pieces. Where there are neither white or black pieces, you don't have pieces.

I'm currently doing the GetValidMoves() method that'll return all the current moves one can do with the current board.

I am thus wondering what might be the best way to represent the board. The naive approach would be to have a matrix with 0's 1's and 2's (for no piece, white piece, and black piece).

Another idea would be to instead of a matrix representation of the board, have 2 lists(or any other data structure): one for black pieces, other for white.

I am implementing this game to test some AI algorithms, so my main concern is speed. I will basically put 2 AI players playing each other, for each turn each player should have a list of all his valid moves and then he'll choose what move to do, this always happening until the game ends(some player wins or there's a tie).

PS: I am not asking about the AI algorithm, I just want to know what would be the best data structure to handle the board, so that it makes it easy to

  1. Look for all the valid moves for the current player

  2. Do a move

  3. Verify the game is not over (it is over when one player lost all his pieces or one player reached the other side of the board).

closed

1 Answer

0 votes
by (108k points)
selected by
 
Best answer

A common way of doing that is with a long type's binary representation for each player.

(since there are 64 squares on the board). Consider a bitmap two 64-bit unsigned ints, one for white and one for black. Then you can represent moves and board positions as a function from (W x B) -> (W x B) where W, B represents the set of possible white and possible black positions respectively.

Then most of the board position stuff can be done with integer arithmetic, which is about as fast as you can get.

Here's a very good (but rather general) wiki article.

The usage is simple - for example, if you want to check if all pieces can move to say up and right, shift the player's pieces representation 7 bits to the left, and check if there are opponent pieces there, then shift them 7 bits left again, and check if those squares are clear...

I used it in a Reversi competition once and can say the implementation wasn't too hard.

If you are looking to learn more about Artificial Intelligence then you visit Artificial Intelligence Tutorial. Also, if you are appearing for job profiles of AI Engineer or AI Expert then you can prepare for the interviews on Artificial Intelligence Interview Questions.

Browse Categories

...