Answers.Body: <p>You'd have some data structure, such as a 2-D 8x8 array, storing the current piece in each square of the board. Everytime you read a new move, update the array appropriately. Then, when you see a capture like "exf5", check what piece is stored in the "f5" square of the array.</p>
  
  <p>You'll have to be careful with en passant captures. For example, in the case of 1.e4 c5 2.e5 d5 3.exd6, you should be checking the element in the array 1 row above (for d5). In order to detect when an en passant has occurred, see if a pawn captured on to an empty square. For example, 3.exd6 is White's e5-pawn capturing on to the empty d6-square, so it must be en passant.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-01-07T04:53:40.613Z
Answers.LastActivityDate: 2020-01-07T04:53:40.613Z
Answers.UserDisplayName: Inertial Ignorance
Body: <p>I am trying to figure out an algorithm to extract the number of each piece that both black and white have from a PGN file. </p>
  
  <p>Right now I have all of the moves tokenized(i.e. c("e4", "d6", "Nc3", "f5", "exf5", "Bxf5", "Qh5", "g6"...), but I'm not sure where to proceed next. So far I know that identifying the "x" characters is necessary, but I am not sure what to do as the notation only identifies the capture square, and not what type of piece is captured.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-01-07T05:25:05.290Z
Comments.Text: At what moment do you want the piece count? The start of the game, the end, or some arbitrary point in the middle? Also, the 'x' character is not always present in some PGN input; a more robust method to track pieces would be to just make each move in some internal board representation.
Comments.UserDisplayName: Remellion
Tags: <programming>
Title: How to count pieces from PGN notation?
ThreadText: # How to count pieces from PGN notation?
  
  (Asked by: username_0 on 2020-01-07)
  
  I am trying to figure out an algorithm to extract the number of each piece that both black and white have from a PGN file.
  
  Right now I have all of the moves tokenized(i.e. c("e4", "d6", "Nc3", "f5", "exf5", "Bxf5", "Qh5", "g6"...), but I'm not sure where to proceed next. So far I know that identifying the "x" characters is necessary, but I am not sure what to do as the notation only identifies the capture square, and not what type of piece is captured.
  
  **Tags:** programming
  
  > **Comments:**
  > **username_1**: At what moment do you want the piece count? The start of the game, the end, or some arbitrary point in the middle? Also, the 'x' character is not always present in some PGN input; a more robust method to track pieces would be to just make each move in some internal board representation.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2020-01-07. Score: 6 | Accepted answer
  
  You'd have some data structure, such as a 2-D 8x8 array, storing the current piece in each square of the board. Everytime you read a new move, update the array appropriately. Then, when you see a capture like "exf5", check what piece is stored in the "f5" square of the array.
  
  You'll have to be careful with en passant captures. For example, in the case of 1.e4 c5 2.e5 d5 3.exd6, you should be checking the element in the array 1 row above (for d5). In order to detect when an en passant has occurred, see if a pawn captured on to an empty square. For example, 3.exd6 is White's e5-pawn capturing on to the empty d6-square, so it must be en passant.
  
  
  ---
  
  