mercoledì 4 aprile 2007

Writing a chess engine 4: To write a F.E.N. parser

Now you have choosen a way to store in memory a chess position, but you need a way to test it! You'll need a way to insert simply a chess position in your engine too: you need a FEN parser!


What does FEN means? FEN is "Forsyth-Edwards Notation"; it is a standard for describing chess positions using the ASCII character set. A single FEN record uses one text line of variable length composed of six datafields. A text file composed exclusively of FEN data records should have a file namewith the suffix ".fen".

Why we need FEN? Having a standard position notation is particularly important for chessprogrammers as it allows them to share position databases. For example, there exist standard position notation databases with many of the classical benchmark tests for chess playing programs, and by using a common position notation format many hours of tedious data entry can be saved. I think it is useful write a parser that transform a FEN string into an intermediate chess position data structure, then you can map this data struct into your own data struct: in this way if you change you data struct you haven't to change this code. An example of data struct can be:
typedef struct {
char piece[64]; /*pezzo*/
char col[64]; /*colore*/
char side; /*tratto*/
unsigned char castle; /*arrocco*/
char enp; /*en passant*/
char ply; /*ply*/
char nm; /*numero mosse*/
}FEN_POSITION;

A FEN string has six fields. Each field is composed only of non-blankprinting ASCII characters. Adjacent fields are separated by a single ASCIIspace character. This is the FEN string of starting position:

rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1


Data Field 1: Piece placement data

The first field represents the placement of the pieces on the board. The board contents are specified starting with the eighth rank and ending with the firstrank. For each rank, the squares are specified from file a to file h. Whitepieces are identified by uppercase SAN piece letters ("PNBRQK") and blackpieces are identified by lowercase SAN piece letters ("pnbrqk"). Empty squares are represented by the digits one through eight; the digit used represents thecount of contiguous empty squares along a rank. A solidus character "/" is used to separate data of adjacent ranks.

Data Field 2: Active color

The second field represents the active color. A lower case "w" is used ifWhite is to move; a lower case "b" is used if Black is the active player.

Data Field 3: Castling availability

The third field represents castling availability. This indicates potentialfuture castling that may of may not be possible at the moment due to blockingpieces or enemy attacks. If there is no castling availability for either side,the single character symbol "-" is used. Otherwise, a combination of from oneto four characters are present. If White has kingside castling availability,the uppercase letter "K" appears. If White has queenside castlingavailability, the uppercase letter "Q" appears. If Black has kingside castlingavailability, the lowercase letter "k" appears. If Black has queensidecastling availability, then the lowercase letter "q" appears. Those letterswhich appear will be ordered first uppercase before lowercase and secondkingside before queenside. There is no white space between the letters.

Data Field 4: En passant target square

The fourth field is the en passant target square. If there is no en passanttarget square then the single character symbol "-" appears. If there is an enpassant target square then is represented by a lowercase file characterimmediately followed by a rank digit. Obviously, the rank digit will be "3"following a white pawn double advance (Black is the active color) or else bethe digit "6" after a black pawn double advance (White being the active color).An en passant target square is given if and only if the last move was a pawnadvance of two squares. Therefore, an en passant target square field may havea square name even if there is no pawn of the opposing side that mayimmediately execute the en passant capture.

Data Field 5: Halfmove clock

The fifth field is a nonnegative integer representing the halfmove clock. Thisnumber is the count of halfmoves (or ply) since the last pawn advance orcapturing move. This value is used for the fifty move draw rule.

Data Field 6: Fullmove number

The sixth and last field is a positive integer that gives the fullmove number.This will have the value "1" for the first move of a game for both White andBlack. It is incremented by one immediately after each move by Black.

This code transforms a fen string filling FEN_POSITION struct.

(to be continued: moves generator)

Many of you have mailed me to say thank you about this column. Thank you too! But there's a better way to say "Thanks" by adding a link to this blog in your blog/website



Nessun commento:

Posta un commento

Per favore non dimenticate di inserire il vostro nome o nickname e un riferimento al vostro blog... non amo i commenti anonimi per cui firmatevi. Per evitare spam i commenti ai post più vecchi di 15 giorni sono moderati: non verranno, dunque, visualizzati immediatamente.

Related Posts with Thumbnails