Answers.Body: <p><a href="https://python-chess.readthedocs.io/" rel="noreferrer">Python chess</a> library has functionality for validating FEN strings.</p>
  <p>You can validate FEN by creating board from it and checking <a href="https://python-chess.readthedocs.io/en/latest/core.html#chess.Board.is_valid" rel="noreferrer">is_valid()</a> method:</p>
  <pre><code>import chess
  
  fen = &quot;rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1&quot;
  board = chess.Board(fen)
  
  board.is_valid() # -&gt; True
  </code></pre>
  <p>To see reasons why it isn't valid you can use <a href="https://python-chess.readthedocs.io/en/latest/core.html#chess.Board.status" rel="noreferrer">status()</a> method to get bitmask and then compare it too various reasons for invalidity:</p>
  <pre><code>import chess
  
  fen = &quot;rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1&quot;
  board = chess.Board(fen)
  
  board.status() == chess.STATUS_VALID # -&gt; True
  </code></pre>
  <p>When checking validity this library uses these criteria:</p>
  <p><code>STATUS_NO_WHITE_KING, STATUS_NO_BLACK_KING, STATUS_TOO_MANY_KINGS, STATUS_TOO_MANY_WHITE_PAWNS, STATUS_TOO_MANY_BLACK_PAWNS, STATUS_PAWNS_ON_BACKRANK, STATUS_TOO_MANY_WHITE_PIECES, STATUS_TOO_MANY_BLACK_PIECES, STATUS_BAD_CASTLING_RIGHTS, STATUS_INVALID_EP_SQUARE, STATUS_OPPOSITE_CHECK, STATUS_EMPTY, STATUS_RACE_CHECK, STATUS_RACE_OVER, STATUS_RACE_MATERIAL, STATUS_TOO_MANY_CHECKERS, STATUS_IMPOSSIBLE_CHECK.</code></p>
  <p>Don't forget that these criteria aren't enough to prove that position is actually reachable. From library docs:</p>
  <blockquote>
  <p>STATUS_VALID if all basic validity requirements are met. This does not imply that the position is actually reachable with a series of legal moves from the starting position.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-17T20:56:44.553Z
Answers.Text: Thanks for the info. That's actually more than I needed as this framework seems overly sophisticated for my needs i.e. it checks if position is legal, but what I wanted is to check if position is playable. E.g. 2 white kings makes position unplayable vs 9 white pawns is not, etc.
  I am actually in process of writing my own validator based on chess.js. Just augmenting its validation logic with some extra rules and adding java layer on top of js code. This way it can be used in both java and js execution environments, so both js front end and java back end can use exactly the same validation.
Answers.UserDisplayName: tim bits
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-06T11:25:11.073Z
Answers.LastActivityDate: 2024-05-06T11:25:11.073Z
Answers.UserDisplayName: Matija Sirk
Answers.Body: <p>All open source chess software {GUI's and Engines} are able to verify fen positions you can search there for the needed code.
  I searched for a standalone alone tool, but found nothing so I used a huge piece of software for just creating an verifying the fen positions that I was working on.
  If you have the programming experience you could easily extract the needed code from any open source software and build your standalone app (for windows hopefully)
  I only know pascal/lazarus/delphi to some extend, if there was some components and libraries for this platform I would have build one my self.
  anyway you can check one of the following:</p>
  <ol>
  <li><a href="https://github.com/Kashu7100/PyChess/tree/master/pychess" rel="nofollow noreferrer">https://github.com/Kashu7100/PyChess/tree/master/pychess</a></li>
  <li><a href="https://lucaschess.pythonanywhere.com/" rel="nofollow noreferrer">https://lucaschess.pythonanywhere.com/</a></li>
  <li><a href="https://scid.sourceforge.net/" rel="nofollow noreferrer">https://scid.sourceforge.net/</a> or any scid variant</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-05T22:26:09.700Z
Answers.Text: Thanks for your suggestion.
  Yes, that was actually my plan B, I was looking at different frameworks though e.g. chessboard.js and chess.js, which are not necessarily chess engines, but have all needed functionality in place to e.g. parse a fen into a two-dimensional array of squares/pieces, determine numbers/types of pieces, relative positions between them, detect checks etc.
Answers.UserDisplayName: tim bits
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-04T05:45:53.823Z
Answers.LastActivityDate: 2024-05-04T05:45:53.823Z
Answers.UserDisplayName: Wassim Saeed
Body: <p>I was looking for a sophisticated open source validator on github, but what I find so far are rudimentary regex based validators.
  By &quot;sophisticated&quot;, I mean ability to perform validation beyond a regular regex check e.g. something that implements some of the fundamental checks suggested in <a href="https://chess.stackexchange.com/questions/1482/how-do-you-know-when-a-fen-position-is-legal">'How do you know when a FEN position is legal?'</a> by <em>community wiki</em> e.g. number of kings, distance between them, side in check should have right to move etc.<br/>
  Just don't want to spend time reinventing the wheel and writing the code, which has already been written.</p>
  <p><strong>16/08/2024 Update:</strong>
  For anyone who might be interested, <a href="http://chess-api.online" rel="nofollow noreferrer">http://chess-api.online</a> is where my FEN validator is deployed as part of <a href="https://github.com/tim-bits/stockfishweb" rel="nofollow noreferrer">a larger app</a>, which allows to access some Stockfish functionality through REST API. This dual mode validator is used on both html/js front-end and java back-end of that app. It is an open source software which is also shared <a href="https://github.com/tim-bits/bughouse_fen_validator" rel="nofollow noreferrer">on github</a>, so it can be tweaked to your particular needs.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-08-17T08:28:39.217Z
Comments.Text: What you are looking for is not legality but validity. Legality is whether the position can be reached in a sequence of legal moves from the game array. Validity is a much narrower concept which basically says whether one can play forward chess: so correct number of kings, player to move not in check (which subsumes adjacent kings situation) and no pawn on first or last rank. Validation is so simple & 100% doable that you might as well write the code yourself. All legal positions are valid. In contrast, legality checking is hard. And often no point: cool to keep illegal positions, e.g. Syzygy
Comments.UserDisplayName: Laska
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-09-01T01:16:46.660Z
Comments.Text: "...legality checking is hard. And often *no point*..."
   You nailed it
Comments.UserDisplayName: tim bits
Tags: <fen>
Title: Is there open source sophisticated FEN validator?
ThreadText: # Is there open source sophisticated FEN validator?
  
  (Asked by: username_0 on 2024-05-04)
  
  I was looking for a sophisticated open source validator on github, but what I find so far are rudimentary regex based validators.
  By "sophisticated", I mean ability to perform validation beyond a regular regex check e.g. something that implements some of the fundamental checks suggested in ['How do you know when a FEN position is legal?'](https://chess.stackexchange.com/questions/1482/how-do-you-know-when-a-fen-position-is-legal) by *community wiki* e.g. number of kings, distance between them, side in check should have right to move etc.  
  Just don't want to spend time reinventing the wheel and writing the code, which has already been written.
  
  **16/08/2024 Update:**
  For anyone who might be interested, <http://chess-api.online> is where my FEN validator is deployed as part of [a larger app](https://github.com/tim-bits/stockfishweb), which allows to access some Stockfish functionality through REST API. This dual mode validator is used on both html/js front-end and java back-end of that app. It is an open source software which is also shared [on github](https://github.com/tim-bits/bughouse_fen_validator), so it can be tweaked to your particular needs.
  
  **Tags:** fen
  
  > **Comments:**
  > **username_1**: What you are looking for is not legality but validity. Legality is whether the position can be reached in a sequence of legal moves from the game array. Validity is a much narrower concept which basically says whether one can play forward chess: so correct number of kings, player to move not in check (which subsumes adjacent kings situation) and no pawn on first or last rank. Validation is so simple & 100% doable that you might as well write the code yourself. All legal positions are valid. In contrast, legality checking is hard. And often no point: cool to keep illegal positions, e.g. Syzygy  
  > **username_0**: "...legality checking is hard. And often *no point*..."  You nailed it  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-05-06. Score: 5
  
  Python chess library has functionality for validating FEN strings.
  You can validate FEN by creating board from it and checking is_valid() method:
  import chess
  
  fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
  board = chess.Board(fen)
  
  board.is_valid() # -> True
  
  To see reasons why it isn't valid you can use status() method to get bitmask and then compare it too various reasons for invalidity:
  import chess
  
  fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
  board = chess.Board(fen)
  
  board.status() == chess.STATUS_VALID # -> True
  
  When checking validity this library uses these criteria:
  STATUS_NO_WHITE_KING, STATUS_NO_BLACK_KING, STATUS_TOO_MANY_KINGS, STATUS_TOO_MANY_WHITE_PAWNS, STATUS_TOO_MANY_BLACK_PAWNS, STATUS_PAWNS_ON_BACKRANK, STATUS_TOO_MANY_WHITE_PIECES, STATUS_TOO_MANY_BLACK_PIECES, STATUS_BAD_CASTLING_RIGHTS, STATUS_INVALID_EP_SQUARE, STATUS_OPPOSITE_CHECK, STATUS_EMPTY, STATUS_RACE_CHECK, STATUS_RACE_OVER, STATUS_RACE_MATERIAL, STATUS_TOO_MANY_CHECKERS, STATUS_IMPOSSIBLE_CHECK.
  Don't forget that these criteria aren't enough to prove that position is actually reachable. From library docs:
  
  STATUS_VALID if all basic validity requirements are met. This does not imply that the position is actually reachable with a series of legal moves from the starting position.
  
  > **Comments:**
  > **username_0**: Thanks for the info. That's actually more than I needed as this framework seems overly sophisticated for my needs i.e. it checks if position is legal, but what I wanted is to check if position is playable. E.g. 2 white kings makes position unplayable vs 9 white pawns is not, etc. I am actually in process of writing my own validator based on chess.js. Just augmenting its validation logic with some extra rules and adding java layer on top of js code. This way it can be used in both java and js execution environments, so both js front end and java back end can use exactly the same validation.  
  
  ---
  
  ### Answer by username_3 on 2024-05-04. Score: 1
  
  All open source chess software {GUI's and Engines} are able to verify fen positions you can search there for the needed code.
  I searched for a standalone alone tool, but found nothing so I used a huge piece of software for just creating an verifying the fen positions that I was working on.
  If you have the programming experience you could easily extract the needed code from any open source software and build your standalone app (for windows hopefully)
  I only know pascal/lazarus/delphi to some extend, if there was some components and libraries for this platform I would have build one my self.
  anyway you can check one of the following:
  
  1. <https://github.com/Kashu7100/PyChess/tree/master/pychess>
  2. <https://lucaschess.pythonanywhere.com/>
  3. <https://scid.sourceforge.net/> or any scid variant
  
  > **Comments:**
  > **username_0**: Thanks for your suggestion. Yes, that was actually my plan B, I was looking at different frameworks though e.g. chessboard.js and chess.js, which are not necessarily chess engines, but have all needed functionality in place to e.g. parse a fen into a two-dimensional array of squares/pieces, determine numbers/types of pieces, relative positions between them, detect checks etc.  
  
  ---
  
  