Answers.Body: <p>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.</p>
  
  <p>The most common way of communicating with a chess engine is via the UCI protocol:
  <a href="http://wbec-ridderkerk.nl/html/UCIProtocol.html" rel="noreferrer">http://wbec-ridderkerk.nl/html/UCIProtocol.html</a></p>
  
  <p>More on FEN:
  <a href="http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation" rel="noreferrer">http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation</a></p>
  
  <p>However, if you are really interested in implementing an engine that can read PGN you should read the specs:
  <a href="http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm" rel="noreferrer">http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm</a></p>
  
  <p>Especially the section about the moves is probably interesting for you:
  <a href="http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm#8.2" rel="noreferrer">http://web.archive.org/web/20100528142843/http://www.very-best.de/pgn-spec.htm#8.2</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-11-15T15:29:49.903Z
Answers.LastActivityDate: 2013-11-16T13:18:52.623Z
Answers.LastEditDate: 2013-11-16T13:18:52.623Z
Answers.UserDisplayName: Rafiek
Answers.Body: <p>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.</p>
  
  <p>If it's the starting position then white has to move, otherwise it has a FEN and a standard FEN contains who must move.</p>
  
  <p>Another way is looking at moves, if the first written move is like <code>N. Kg2</code> then it's white's turn. If the first written move is line <code>N. ... Rh5</code> then it's black's turn.</p>
  
  <p>Also, it's popular (I'm not sure) to write the first move by number <code>1</code> such as <code>1. Kg2</code> or <code>1. ... Rh5</code>. So, the numbers can help to follow the sequences.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-11-15T20:40:15.037Z
Answers.LastActivityDate: 2013-11-15T20:40:15.037Z
Answers.UserDisplayName: masoud
Answers.Body: <p>There is a detailed standard of PGN available, for instance here: <a href="https://web.archive.org/web/20100407090858/http://www.very-best.de/pgn-spec.htm">PGN standard at the Wayback Machine</a>.</p>
  
  <p>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".</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-11-17T18:34:23.700Z
Answers.Text: 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!
Answers.UserDisplayName: Lynob
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-11-18T09:07:45.947Z
Answers.Text: 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.
Answers.UserDisplayName: RemcoGerlich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2013-11-15T15:42:35.020Z
Answers.LastActivityDate: 2013-11-15T15:42:35.020Z
Answers.UserDisplayName: RemcoGerlich
Body: <p>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.</p>
  
  <p>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 <code>bN</code> for example but by doing so i won't be able to import PGN easily.</p>
  
  <p>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?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-15T15:12:59.067Z
Comments.Text: i didn't ask on SO because i'm not interested in a particular piece of code, i just want to understand the idea
Comments.UserDisplayName: Lynob
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-15T15:28:57.363Z
Comments.Text: Do you say, you can not determine that it's white's turn to play the next move or black's turn?
Comments.UserDisplayName: masoud
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-15T18:46:15.867Z
Comments.Text: @MM. thats right
Comments.UserDisplayName: Lynob
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-15T18:53:20.883Z
Comments.Text: PGN is not enough for a chess engine. How are you going to handle a starting position where black is to move?
Comments.UserDisplayName: Rafiek
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-15T19:17:35.083Z
Comments.Text: 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.
Comments.UserDisplayName: Andrew
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2013-11-16T13:19:30.000Z
Comments.Text: @Andrew Exactly!
Comments.UserDisplayName: Rafiek
Tags: <pgn><programming>
Title: How do engines read PGN?
ThreadText: # How do engines read PGN?
  
  (Asked by: username_0 on 2013-11-15)
  
  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?
  
  **Tags:** pgn, programming
  
  > **Comments:**
  > **username_0**: i didn't ask on SO because i'm not interested in a particular piece of code, i just want to understand the idea  
  > **username_1**: Do you say, you can not determine that it's white's turn to play the next move or black's turn?  
  > **username_0**: @MM. thats right  
  > **username_2**: PGN is not enough for a chess engine. How are you going to handle a starting position where black is to move?  
  > **username_3**: 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.  
  > **username_2**: @username_3 Exactly!  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2013-11-15. Score: 7 | Accepted answer
  
  There is a detailed standard of PGN available, for instance here: [PGN standard at the Wayback Machine](https://web.archive.org/web/20100407090858/http://www.very-best.de/pgn-spec.htm).
  
  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".
  
  > **Comments:**
  > **username_0**: 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!  
  > **username_4**: 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.  
  
  ---
  
  ### Answer by username_2 on 2013-11-15. Score: 5
  
  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>
  
  
  ---
  
  ### Answer by username_1 on 2013-11-15. Score: 1
  
  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.
  
  
  ---
  
  