Answers.Body: <p>Here is another approach. It uses the python chess library to flip the side to move via a null move, which also removes the e.p. square if there is. I also added a condition that the side to move is not in_check and not in stalemate.</p>
  <p>You need to install python chess with <code>pip install chess</code>.</p>
  <h2>Code</h2>
  <p>In the code there is a list of fens. If you want to use this, you have to add your fen.</p>
  <p><em>flip_turn.py</em></p>
  <pre><code>&quot;&quot;&quot;Flips side to move of the given FEN.
  
  Required library:
    pip install chess
  &quot;&quot;&quot;
  
  
  import chess
  
  
  fens = [
      'r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R b KQkq - 3 6',
      'rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR w KQkq - 1 5',
      '8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 b - - 3 61',
      'rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3'
  ]
  
  output = []  # store output here
  
  for fen in fens:
      board = chess.Board(fen)
  
      # The current side to move must not be in check or stalemate.
      if not board.is_check() and not board.is_stalemate():
  
          # Push a null move to flip color, ep will be removed if there is.
          board.push(chess.Move.null())
  
          # Save the fen to output.
          new_fen = board.fen()
          output.append(new_fen)
  
  # Print the output to terminal.
  for fen in output:
      print(fen)
  </code></pre>
  <h2>Run the script</h2>
  <pre><code>python flip_turn.py
  </code></pre>
  <h2>Output</h2>
  <pre><code>r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R w KQkq - 4 7
  rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR b KQkq - 2 5
  8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 w - - 4 62
  rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 1 3
  </code></pre>
  <h2>Another code when your fens are in the file</h2>
  <p>Rename your file to <code>mypos.fen</code>. The code below will use it to get the fen input.</p>
  <p><em>flip_turn_file.py</em></p>
  <pre><code>&quot;&quot;&quot;Flips side to move of the given FEN from a file.
  
  Required library:
    pip install chess
  &quot;&quot;&quot;
  
  
  import chess
  
  
  fens = []  # store fens from a file
  with open('mypos.fen') as f:
      for line in f:
          fen = line.rstrip()
          fens.append(fen)
  
  output = []  # store output here
  
  for fen in fens:
      board = chess.Board(fen)
  
      # The current side to move must not be in check or stalemate.
      if not board.is_check() and not board.is_stalemate():
  
          # Push a null move to flip color, ep will be removed if there is.
          board.push(chess.Move.null())
  
          # Save the fen to output.
          new_fen = board.fen()
          output.append(new_fen)
  
  # Print the output to terminal.
  for fen in output:
      print(fen)
  </code></pre>
  <h2>Sample mypos.fen file</h2>
  <pre><code>r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R b KQkq - 3 6
  rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR w KQkq - 1 5
  8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 b - - 3 61
  rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-06-08T17:08:05.980Z
Answers.Text: whoa! thank you so much
Answers.UserDisplayName: user38519
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-26T09:50:44.070Z
Answers.LastActivityDate: 2024-05-26T09:58:39.410Z
Answers.LastEditDate: 2024-05-26T09:58:39.410Z
Answers.UserDisplayName: ferdy
Answers.Body: <p>As noted in comments, all that needs to be done is replace the single character that represents the color to go next with it's &quot;opposite&quot;, i.e. 'b' -&gt; 'w' or 'w' -&gt; 'b'. Also, the OP doesn't say what programming language this should be written in, nor if any error checking needs to be done to verify that the resulting FEN represents a valid position. However, the output FEN given as an example isn't valid (White has made one move, while Black has made no moves, but the output FEN shows that it's White's move). Thus, this answer assumes that no checking is needed in this regard. Also, with Python being popular and easy to understand, that is what is used here. Note that there is also minimal checking done to validate that the input FEN is valid. No external modules need to be imported.</p>
  <pre><code>def change_colors(orig_fen_str):
      &quot;&quot;&quot;
      Return a FEN with the color of the next move changed from white
      to black or black to white. Indicate error otherwise.
      &quot;&quot;&quot;
  
      fen_parts = orig_fen_str.split(' ', 3)
      new_fen = ''     # Avoid pylint messages about uninitialized variables
      if fen_parts[1] == 'w':
          # Change white to black
          new_fen = ' '.join([fen_parts[0], 'b', fen_parts[2]])
      elif fen_parts[1] == 'b':
          # Change black to white
          new_fen = ' '.join([fen_parts[0], 'w', fen_parts[2]])
      else:
          # Indicate an error
          new_fen = 'Error! Original FEN does not contain a valid value for next move color.'
      return new_fen
  
  orig_fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1'
  new_fen = change_colors(orig_fen)
  print(new_fen)
  </code></pre>
  <p>Output:</p>
  <blockquote>
  <p>rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq</p>
  </blockquote>
  <p>It should be noted that this is just one technique (and likely not the best) to accomplish the goal.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-22T20:30:27.373Z
Answers.LastActivityDate: 2024-05-22T20:30:27.373Z
Answers.UserDisplayName: GreenMatt
Body: <p>How would I write a function that would change the color to play of the given position and be consistent with?</p>
  <p>Example FEN:<br />
  <code>input:rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1</code></p>
  <p><code>output:rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1</code></p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-21T18:30:29.467Z
Comments.Text: Should be easy with just basic regex.
Comments.UserDisplayName: Hank's back rank
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-21T20:00:40.420Z
Comments.Text: Are you interested in whether the resulting position is legal? (E.g. the initial game array is not legal when flipped.)
Comments.UserDisplayName: Laska
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-21T20:07:08.340Z
Comments.Text: @Hank'sbackrank: Careful! Now [they have two problems](https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)!  ;-)
Comments.UserDisplayName: GreenMatt
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-21T21:32:06.647Z
Comments.Text: If you don't need to change the board position or move counter, it's simply swapping the last w for a b.
Comments.UserDisplayName: qwr
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-21T23:25:26.880Z
Comments.Text: @GreenMatt fair enough. You could do it instead along with legality checks using the chess module in python which is pretty simple to use.
Comments.UserDisplayName: Hank's back rank
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-22T01:53:01.787Z
Comments.Text: @Hank'sbackrank: While I do sometimes find regexes frustrating, I was (mostly) joking. They'll work, as will many other techniques.
Comments.UserDisplayName: GreenMatt
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-22T01:53:34.180Z
Comments.Text: It would help to know what programming language is going to be used.
Comments.UserDisplayName: GreenMatt
Tags: <programming><computer-chess><fen>
Title: Changing the FEN position color
ThreadText: # Changing the FEN position color
  
  (Asked by: username_0 on 2024-05-21)
  
  How would I write a function that would change the color to play of the given position and be consistent with?
  
  Example FEN:  
  `input:rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1`
  
  `output:rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1`
  
  **Tags:** programming, computer-chess, fen
  
  > **Comments:**
  > **username_1**: Should be easy with just basic regex.  
  > **username_2**: Are you interested in whether the resulting position is legal? (E.g. the initial game array is not legal when flipped.)  
  > **username_3**: @Hank'sbackrank: Careful! Now [they have two problems](https://blog.codinghorror.com/regular-expressions-now-you-have-two-problems/)!  ;-)  
  > **username_4**: If you don't need to change the board position or move counter, it's simply swapping the last w for a b.  
  > **username_1**: @username_3 fair enough. You could do it instead along with legality checks using the chess module in python which is pretty simple to use.  
  > **username_3**: @Hank'sbackrank: While I do sometimes find regexes frustrating, I was (mostly) joking. They'll work, as will many other techniques.  
  > **username_3**: It would help to know what programming language is going to be used.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_5 on 2024-05-26. Score: 2
  
  Here is another approach. It uses the python chess library to flip the side to move via a null move, which also removes the e.p. square if there is. I also added a condition that the side to move is not in_check and not in stalemate.
  You need to install python chess with pip install chess.
  Code
  In the code there is a list of fens. If you want to use this, you have to add your fen.
  flip_turn.py
  """Flips side to move of the given FEN.
  
  Required library:
    pip install chess
  """
  
  
  import chess
  
  
  fens = [
      'r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R b KQkq - 3 6',
      'rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR w KQkq - 1 5',
      '8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 b - - 3 61',
      'rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3'
  ]
  
  output = []  # store output here
  
  for fen in fens:
      board = chess.Board(fen)
  
      # The current side to move must not be in check or stalemate.
      if not board.is_check() and not board.is_stalemate():
  
          # Push a null move to flip color, ep will be removed if there is.
          board.push(chess.Move.null())
  
          # Save the fen to output.
          new_fen = board.fen()
          output.append(new_fen)
  
  # Print the output to terminal.
  for fen in output:
      print(fen)
  
  Run the script
  python flip_turn.py
  
  Output
  r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R w KQkq - 4 7
  rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR b KQkq - 2 5
  8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 w - - 4 62
  rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR b KQkq - 1 3
  
  Another code when your fens are in the file
  Rename your file to mypos.fen. The code below will use it to get the fen input.
  flip_turn_file.py
  """Flips side to move of the given FEN from a file.
  
  Required library:
    pip install chess
  """
  
  
  import chess
  
  
  fens = []  # store fens from a file
  with open('mypos.fen') as f:
      for line in f:
          fen = line.rstrip()
          fens.append(fen)
  
  output = []  # store output here
  
  for fen in fens:
      board = chess.Board(fen)
  
      # The current side to move must not be in check or stalemate.
      if not board.is_check() and not board.is_stalemate():
  
          # Push a null move to flip color, ep will be removed if there is.
          board.push(chess.Move.null())
  
          # Save the fen to output.
          new_fen = board.fen()
          output.append(new_fen)
  
  # Print the output to terminal.
  for fen in output:
      print(fen)
  
  Sample mypos.fen file
  r1bqk2r/ppp1ppbp/2np1np1/8/2PPP3/2NB4/PP2NPPP/R1BQK2R b KQkq - 3 6
  rnbqk2r/ppp1ppbp/3p1np1/8/2PPP3/2N5/PP3PPP/R1BQKBNR w KQkq - 1 5
  8/p3k3/4Pp2/2p1b2p/P1Bn3P/7K/8/1R6 b - - 3 61
  rnbqkbnr/ppp2ppp/4p3/3pP3/8/8/PPPP1PPP/RNBQKBNR w KQkq d6 0 3
  
  > **Comments:**
  > **username_0**: whoa! thank you so much  
  
  ---
  
  ### Answer by username_3 on 2024-05-22. Score: 0
  
  As noted in comments, all that needs to be done is replace the single character that represents the color to go next with it's "opposite", i.e. 'b' -> 'w' or 'w' -> 'b'. Also, the OP doesn't say what programming language this should be written in, nor if any error checking needs to be done to verify that the resulting FEN represents a valid position. However, the output FEN given as an example isn't valid (White has made one move, while Black has made no moves, but the output FEN shows that it's White's move). Thus, this answer assumes that no checking is needed in this regard. Also, with Python being popular and easy to understand, that is what is used here. Note that there is also minimal checking done to validate that the input FEN is valid. No external modules need to be imported.
  def change_colors(orig_fen_str):
      """
      Return a FEN with the color of the next move changed from white
      to black or black to white. Indicate error otherwise.
      """
  
      fen_parts = orig_fen_str.split(' ', 3)
      new_fen = ''     # Avoid pylint messages about uninitialized variables
      if fen_parts[1] == 'w':
          # Change white to black
          new_fen = ' '.join([fen_parts[0], 'b', fen_parts[2]])
      elif fen_parts[1] == 'b':
          # Change black to white
          new_fen = ' '.join([fen_parts[0], 'w', fen_parts[2]])
      else:
          # Indicate an error
          new_fen = 'Error! Original FEN does not contain a valid value for next move color.'
      return new_fen
  
  orig_fen = 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1'
  new_fen = change_colors(orig_fen)
  print(new_fen)
  
  Output:
  
  rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq
  
  It should be noted that this is just one technique (and likely not the best) to accomplish the goal.
  
  
  ---
  
  