1

I'm writing a small chess engine in LISP. I was trying to read some PGN file, at first it seemed like a piece of cake. But then I realized something.

Most of the PGN files, don't tell which is white and which is black. I mean if white plays e4 an later d5, how does the engine knows if he has to take or not? I could name the black pieces bN for example but by doing so i won't be able to import PGN easily.

Should i increment by 2? is that how PGN parsers do it? Say the first three moves in a match are e4 e6 f4, should i create a recursive function to read x + 2? to skip whites move?

  • i didn't ask on SO because i'm not interested in a particular piece of code, i just want to understand the idea – Lynob Nov 15 '13 at 15:12
  • Do you say, you can not determine that it's white's turn to play the next move or black's turn? – deepmax Nov 15 '13 at 15:28
  • @MM. thats right – Lynob Nov 15 '13 at 18:46
  • PGN is not enough for a chess engine. How are you going to handle a starting position where black is to move? – Rafiek Nov 15 '13 at 18:53
  • Pretty much the only sane way to do it is to keep the current board position in memory and then update it as you make moves. A move like Nd5 means nothing without context of the current position. – Andrew Nov 15 '13 at 19:17
6

There is a detailed standard of PGN available, for instance here: PGN standard at the Wayback Machine.

You will never see "e4 e6 f4" in a PGN file, since it is mandatory to mention the move number before white's move: "1.e4 e6 2.f4".

  • aha! so you say if there's a period before the move then it's white's move, else it's black's move! I didn't notice the period! – Lynob Nov 17 '13 at 18:34
  • 1
    Well, the moves alternate. So if the previous move was a white move, the next one is a black move. If the line starts with a black move (like in or after a variation), the move number is followed by three periods ("1... e5"). But you knew that, because you read the spec carefully, right? That's why I went and looked it up for you. Don't just believe what I write from memory, I'm often slightly wrong. – RemcoGerlich Nov 18 '13 at 9:07
4

Chess engines normally don't read PGN. When they "start" they read a position that is encoded in a FEN notation. FEN is a powerful way to describe a chess position. From this position a sequence of moves can be given in a string.

The most common way of communicating with a chess engine is via the UCI protocol: http://wbec-ridderkerk.nl/html/UCIProtocol.html

More on FEN: http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation

However, if you are really interested in implementing an engine that can read PGN you should read the specs: http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm

Especially the section about the moves is probably interesting for you: http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm#8.2

0

In a standard PGN you have an initial position, it's the chess starting position or it has a FEN to setup the position. After that you can follow the move sequence one after another.

If it's the starting position then white has to move, otherwise it has a FEN and a standard FEN contains who must move.

Another way is looking at moves, if the first written move is like N. Kg2 then it's white's turn. If the first written move is line N. ... Rh5 then it's black's turn.

Also, it's popular (I'm not sure) to write the first move by number 1 such as 1. Kg2 or 1. ... Rh5. So, the numbers can help to follow the sequences.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.