views:

312

answers:

3

would you create 3 list(of coordinate) for

  • empty position
  • black position
  • white position

or just looping though the array when needed and play with the result every time?

what would be best? (speed wise)

+6  A: 

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.

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

(Originally posted here)

Ed Guiness
I have something similar to sharpchess
Fredou
With modern computers, are you really likely to experience noticable delays with any reasonable implementation?
John Fouhy
@John - the tiniest differences in speed are significant when a routine is called billions of times. It's not just about user-perception at the GUI.
Ed Guiness
+2  A: 

What you're looking for is a board representation. The Chess Programming Wiki has a very detailed section on the topic (definitely worth reading through if you're serious about writing an AI), while Wikipedia offers a good overview on the subject.

It's important to be very thoughtful when choosing the appropiate board representation - they all offer their own unique advantages (and pitfalls) - largely to do with speed/perform of certain operations such as performing moves and evaluating the board state (typically ranging from O(1) to O(n) time complexity depending on the method and task). As far as I know, there is still no consensus on the "best" board representation, though some are generally preferred over others nowadays (bitboards are almost a must-have for example). This is why it is common for most strong chess AIs to use several (up to 4 or 5 even) different board representations when searching for moves.

Noldorin
A: 

I would suggest an array of 64 items such as:

byte [64] Squares;

This way you only need to represent a chess board position by a single byte, it’s much faster.

When dealing with a single index to reference chess board positions there are certain things that one must know to make life easier. For example how do you know that two positions are both on the same row or column? There is an easy trick to figure that out.

Row

To figure out the row of a position you divide the position by 8 and take the integer portion of the result. For example position 63 divided by 8 is 7.875 which equals row 7. Position 3 divided by 8 is 0.375 so 0. In C# by casting to an integer you will always get just the integer portion of the number, hence:

Row = (int)(position / 8)

Column

To figure out the column of a position you use the modulus operator by performing position modulus 8. For example position 24 modulus 8 is column 0. Position 15 modulus 8 is 7, hence

Column = position % 8

Armed with these two concepts we can convert any position on our 64 square board to a column and row.

If you would like to learn more about creating your own chess engine have a look at http://www.chessbin.com

Adam Berent