Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

chess

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

like image 742
slm Avatar asked Sep 02 '08 16:09

slm


People also ask

How do you write a chess code?

Chess notation uses abbreviations for each piece, using capitalized letters. King = K, Queen = Q, Bishop = B, Knight = N, Rook = R, Pawn = no notation. Capturing an enemy piece sees an “x” placed between the piece moved and the square the captured piece was upon.

What is the best algorithm for chess?

A general strategy in game algorithms is the minimax strategy, augmented with alpha-beta pruning. The minimax algorithm finds the best move, and alpha-beta pruning prevents it from going into branches of the game tree that cannot produce a better result than previous branches already have.

What programming language is used in chess?

Chess programming is dominated by the C and C++ languages. The strongest engine in a non-C language is currently Booot written by Alex Morozov in Delphi. Critter was also originally written in Delphi, but was rewritten in C++ after running into too many 64-bit bugs in the Delphi compiler.


1 Answers

For a serious chess engine, using bitboards is an efficient way to represent a chess board in memory. Bitboards are faster than any array based representation, specially in 64-bit architectures where a bitboard can fit inside a single CPU register.

Bitboards

Basic idea of bitboards is to represent every chess piece type in 64 bits. In C++/C# it will be ulong/UInt64. So you'll maintain 12 UInt64 variables to represent your chess board: two (one black and one white) for each piece type, namely, pawn, rook, knight, bishop, queen and king. Every bit in a UInt64 will correspond to a square on chessboard. Typically, the least significant bit will be a1 square, the next b1, then c1 and so on in a row-major fashion. The bit corresponding to a piece's location on chess board will be set to 1, all others will be set to 0. For example, if you have two white rooks on a2 and h1 then the white rooks bitboard will look like this:

0000000000000000000000000000000000000000000000000000000110000000 

Now for example, if you wanted to move your rook from a2 to g2 in the above example, all you need to do is XOR you bitboard with:

0000000000000000000000000000000000000000000000000100000100000000 

Bitboards have a performance advantage when it comes to move generation. There are other performance advantages too that spring naturally from bitboards representation. For example you could use lockless hash tables which are an immense advantage when parallelising your search algorithm.

Further Reading

The ultimate resource for chess engine development is the Chess Programming Wiki. I've recently written this chess engine which implements bitboards in C#. An even better open source chess engine is StockFish which also implements bitboards but in C++.

like image 150
bytefire Avatar answered Sep 23 '22 03:09

bytefire