Answers.Body: <p>There is at least one case where your method will fail: pawns on f2, g3, h2 and  h3. </p>
  
  <p><a href="https://i.sstatic.net/RPrjv.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/RPrjv.png" alt="enter image description here"></a></p>
  
  <p>The structure is illegal, but drawing triangles from every pawn will not show this. To catch this, you'd have to draw the triangle from every square of the chessboard (h4 in this case demonstrates the illegality).</p>
  
  <p>So as you said, draw triangles, but instead of starting from pawns, draw them from every square on the board. (For white, it actually suffices to try every square from the 3rd to the 8th rank.)</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-03T21:36:04.390Z
Answers.Text: awesome catch, I just need to adjust the rules a bit then, instead of always decreasing the limit by 1 (because it always had an ally pawn on the top square) I just need to take that limit as it is, but then take into consideration the top square to count ally pawns there too.
Answers.UserDisplayName: ajax333221
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-03T23:10:50.083Z
Answers.Text: worth saying that you don't need to try the squares that would allow a limit of 8 pawns (if you already have validation for that). With this information, you go testing only from triangles that have 7 or lower limits (so for the 8th rank you only need to test the a8 and h8 squares), this reduces the squares in 12 (from 48 to 36)
Answers.UserDisplayName: ajax333221
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-04T00:27:24.980Z
Answers.Text: also for even further optimization, avoid searching inside triangles that have limits greater or equal that the amount of pawns in play for that side (best to have a map of the 36 triangles distributed by their limits)
Answers.UserDisplayName: ajax333221
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-08-02T08:38:49.600Z
Answers.LastActivityDate: 2019-08-02T17:55:32.263Z
Answers.LastEditDate: 2019-08-02T17:55:32.263Z
Answers.UserDisplayName: Remellion
Body: <p>I'm going to further expand a FEN validation function I have been working on for some time to include illegal pawn formations such that:</p>
  
  <blockquote>
    <p>... for example white pawns could
    never be on a2, a3, and b2; there is no way a pawn could be on both a3
    and b2. <a href="https://chess.stackexchange.com/questions/1482/how-to-know-when-a-fen-position-is-legal#comment1575_1483">(source)</a></p>
  </blockquote>
  
  <p>This particular test doesn't require to take opponent missing material into consideration (it could, but I have that on a separate test to check every column with multiple pawns and determine the minimum missing material the opponent should have, but that test is different, I only want to focus on this other test assuming infinite material can be used to reach those positions).</p>
  
  <p>So far, my approach has been to draw a triangle from any pawn (excluding the ones that sit on the starting rank, which wouldn't allow a triangle to be drawn) all the way to the starting rank, then counting all the pawns that would fit on the base line of the triangle (starting rank). In the case the triangle goes out of bounds, those pawns are not counted:</p>
  
  <p><a href="https://i.sstatic.net/geaGi.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/geaGi.png" alt="enter image description here"></a></p>
  
  <p>That amount of pawns on the bottom of the triangle are the upper exclusive limit that pawns could then appear anywhere inside the triangle (because the test is applied to each pawn and each one will draw their own triangles and because the test is a generalized "no invalid pawn structures?", as in, all or nothing, it is safe to give big triangles with cluttered pawns a pass, since later on, the pawns inside will undergo the same test).</p>
  
  <p>Example from the left image with limit of 3, there should be 0-2 to be a legal pawn structure.</p>
  
  <p>Will this solution always work or there are times where this limit will be incorrectly generated? Can this be simplified further using a different reasoning or methods?</p>
  
  <p>Any ideas, feedback, alternative solutions are appreciated.</p>
  
Tags: <pawn-structure><illegal-move><fen>
Title: How to detect invalid pawn structures (not based on opponent material) for FEN valdation?
ThreadText: # How to detect invalid pawn structures (not based on opponent material) for FEN valdation?
  
  (Asked by: username_0 on 2019-08-02)
  
  I'm going to further expand a FEN validation function I have been working on for some time to include illegal pawn formations such that:
  
  > ... for example white pawns could
  > never be on a2, a3, and b2; there is no way a pawn could be on both a3
  > and b2. [(source)](https://chess.stackexchange.com/questions/1482/how-to-know-when-a-fen-position-is-legal#comment1575_1483)
  
  This particular test doesn't require to take opponent missing material into consideration (it could, but I have that on a separate test to check every column with multiple pawns and determine the minimum missing material the opponent should have, but that test is different, I only want to focus on this other test assuming infinite material can be used to reach those positions).
  
  So far, my approach has been to draw a triangle from any pawn (excluding the ones that sit on the starting rank, which wouldn't allow a triangle to be drawn) all the way to the starting rank, then counting all the pawns that would fit on the base line of the triangle (starting rank). In the case the triangle goes out of bounds, those pawns are not counted:
  
  [![enter image description here](https://i.sstatic.net/geaGi.png)](https://i.sstatic.net/geaGi.png)
  
  That amount of pawns on the bottom of the triangle are the upper exclusive limit that pawns could then appear anywhere inside the triangle (because the test is applied to each pawn and each one will draw their own triangles and because the test is a generalized "no invalid pawn structures?", as in, all or nothing, it is safe to give big triangles with cluttered pawns a pass, since later on, the pawns inside will undergo the same test).
  
  Example from the left image with limit of 3, there should be 0-2 to be a legal pawn structure.
  
  Will this solution always work or there are times where this limit will be incorrectly generated? Can this be simplified further using a different reasoning or methods?
  
  Any ideas, feedback, alternative solutions are appreciated.
  
  **Tags:** pawn-structure, illegal-move, fen
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-08-02. Score: 3 | Accepted answer
  
  There is at least one case where your method will fail: pawns on f2, g3, h2 and h3.
  
  [![enter image description here](https://i.sstatic.net/RPrjv.png)](https://i.sstatic.net/RPrjv.png)
  
  The structure is illegal, but drawing triangles from every pawn will not show this. To catch this, you'd have to draw the triangle from every square of the chessboard (h4 in this case demonstrates the illegality).
  
  So as you said, draw triangles, but instead of starting from pawns, draw them from every square on the board. (For white, it actually suffices to try every square from the 3rd to the 8th rank.)
  
  > **Comments:**
  > **username_0**: awesome catch, I just need to adjust the rules a bit then, instead of always decreasing the limit by 1 (because it always had an ally pawn on the top square) I just need to take that limit as it is, but then take into consideration the top square to count ally pawns there too.  
  > **username_0**: worth saying that you don't need to try the squares that would allow a limit of 8 pawns (if you already have validation for that). With this information, you go testing only from triangles that have 7 or lower limits (so for the 8th rank you only need to test the a8 and h8 squares), this reduces the squares in 12 (from 48 to 36)  
  > **username_0**: also for even further optimization, avoid searching inside triangles that have limits greater or equal that the amount of pawns in play for that side (best to have a map of the 36 triangles distributed by their limits)  
  
  ---
  
  