Answers.Body: <p>The issue may be here:</p>
  <pre><code>Board newBoard = board;
  </code></pre>
  <p>This creates a new variable, but it doesn't actually create a new <code>Board</code>. <code>board</code> and <code>newBoard</code> are pointing to the exact same <code>Board</code> object. So, when your code changes anything in <code>newBoard</code>, it also changes it in <code>board</code>, and when it changes, you get this error.</p>
  <p>What you may want to do is make an actual copy of <code>board</code>, so when you change it, it doesn't change the original.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-04T17:26:12.353Z
Answers.Text: That's strange, but I can see how it makes sense. How would I make a copy? Would I have to start with `Board newBoard = new Board();` then copy over all components? Is there an easier way to do this? Is this efficient too? Thank you very much!
Answers.UserDisplayName: Kieran Montgomery
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-04T17:39:01.667Z
Answers.Text: Thank you very much! I just made my own and this fixed the problem. I will look into performance soon!
Answers.UserDisplayName: Kieran Montgomery
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-04T17:39:09.803Z
Answers.Text: Yes, that's pretty much it, unless you have an easier way of making a new Board or there's already a built-in copy function. Also note that those components themselves may need to be copied, because the same thing might happen. `newBoard.someObject = board.someObject` would make `someObject` be literally the same object in both.
Answers.UserDisplayName: D M
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-07T19:48:09.917Z
Answers.Text: I don't know what the `Board` class looks like, but presumably you're going to want to add a `Copy` method to it, and you might be able to implement such a method like so: `public Board Copy() { return MemberwiseClone(); }`
Answers.UserDisplayName: Sophie Swett
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-08T17:09:49.377Z
Answers.Text: alternatively you could add an `undoMove` function to put the board back the way it was.  You would call it after `search` in your for-loop.
Answers.UserDisplayName: Jeff Wilson
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-07-04T17:18:13.773Z
Answers.LastActivityDate: 2020-07-04T17:18:13.773Z
Answers.UserDisplayName: D M
Body: <h2>Explanation</h2>
  <p>I have a list of all possible current moves in a position. At each node, I want to then generate all possible moves and continue. The problem is that my code is modifying the list of moves in the node above. At least I think this is the case!</p>
  <p>I am open to completely changing the way I handle moves, as I feel this isn't very efficient since I have a struct for every move.</p>
  <h2>Output</h2>
  <p>This is the current output.</p>
  <pre><code>PS D:\Users\kmont\Desktop\ChessEngine&gt; dotnet run
  Number of moves at depth 1 is 4. It is White's turn.
  Number of moves at depth 2 is 4. It is Black's turn. Previous move was knight to a3
  Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
      at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
      at System.Collections.Generic.List`1.Enumerator.MoveNext()
      at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 55
      at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 60
      at Program.Main(String[] args) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 33
  </code></pre>
  <h2>Code</h2>
  <p>The Move Struct:</p>
  <pre><code>public struct Move{
      public Piece.Type pieceType;
      public UInt64 piece;
      public UInt64 moveTo;
      public int color;
  
      public bool quiet;
      public UInt64 capturedPiece;
      public int capturedColor;
  }
  </code></pre>
  <p>Search function:</p>
  <pre><code>public static void search(int depth, Board board, string prevMove = &quot;nothing&quot;){
  
      // If at the end of the tree, return.
  
      depth++;
      if (depth == maxDepth) return;
  
      Console.Write(&quot;Number of moves at depth {0} is {1}. It is {2}'s turn.&quot;, depth, board.allMoves.Count, board.color == 0 ? &quot;White&quot; : &quot;Black&quot;);
      if (prevMove != &quot;nothing&quot;) Console.WriteLine(&quot; Previous move was knight to &quot; + prevMove);
      else Console.WriteLine(&quot;&quot;);
  
      // Uncomment below line for a list of all possible moves.
      //if (true) foreach (Board.Move move in board.allMoves) Console.WriteLine(String.Format(&quot;    {0} {1} on square {2} can move to {3}.&quot;, move.color == 0 ? &quot;White&quot; : &quot;Black&quot;, move.pieceType.ToString(), help.square(move.piece), help.square(move.moveTo)));
  
      foreach (Board.Move move in board.allMoves){
          Board newBoard = board;
          newBoard.color = 1 - board.color;
          initialiseBoard(newBoard);
          newBoard = makeMove(newBoard, move);
          search(depth, newBoard, help.square(move.moveTo));
      }
  }
  </code></pre>
  <p>Make move function:</p>
  <pre><code>public static Board makeMove(Board board, Board.Move move){
  
      string moveColor = move.color == 0 ? &quot;White&quot; : &quot;Black&quot;;
      string boardColor = board.color == 0 ? &quot;White&quot; : &quot;Black&quot;;
      string pmove = help.square(move.moveTo);
      //Console.WriteLine(String.Format(&quot;    Making {0} move {2}: It is {1}'s turn.&quot;, moveColor, boardColor, pmove));
      
      Board toReturn = board;
      // Remove piece from board
      if (toReturn.color == 0 &amp; move.color == 0){
          if (move.pieceType == Piece.Type.Knight){
              toReturn.WhiteKnightsBitboard &amp;= ~move.piece; // Get rid of piece on board
              toReturn.WhiteKnightsBitboard |= move.moveTo; // Add piece to board
          }
      }
      else if (toReturn.color == 1 &amp; move.color == 1){
          if (move.pieceType == Piece.Type.Knight){
              toReturn.BlackKnightsBitboard &amp;= ~move.piece;
              toReturn.BlackKnightsBitboard |= move.moveTo;
          }
      }
  
      Game.node++; // To count nodes
      return toReturn;
  
  }
  </code></pre>
  <p>Initialise Board function:</p>
  <pre><code>static void initialiseBoard(Board board){
      BoardOperations.updatePieces(board); // Sets all pieces and finds empty squares 
      board.generateAttackSquares(board);  // Finds white knight moves and puts them into board.allMoves
  }
  </code></pre>
  
Tags: <engines><programming><bitboard>
Title: How do I apply moves (bitboard) and continue searching? My current attempt is throwing a strange error!
ThreadText: # How do I apply moves (bitboard) and continue searching? My current attempt is throwing a strange error!
  
  (Asked by: username_0 on 2020-07-04)
  
  ## Explanation
  
  I have a list of all possible current moves in a position. At each node, I want to then generate all possible moves and continue. The problem is that my code is modifying the list of moves in the node above. At least I think this is the case!
  
  I am open to completely changing the way I handle moves, as I feel this isn't very efficient since I have a struct for every move.
  
  ## Output
  
  This is the current output.
  
  ```
  PS D:\Users\kmont\Desktop\ChessEngine> dotnet run
  Number of moves at depth 1 is 4. It is White's turn.
  Number of moves at depth 2 is 4. It is Black's turn. Previous move was knight to a3
  Unhandled exception. System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
      at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
      at System.Collections.Generic.List`1.Enumerator.MoveNext()
      at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 55
      at Program.search(Int32 depth, Board board, String prevMove) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 60
      at Program.Main(String[] args) in D:\Users\kmont\Desktop\ChessEngine\Main\main.cs:line 33
  
  ```
  
  ## Code
  
  The Move Struct:
  
  ```
  public struct Move{
      public Piece.Type pieceType;
      public UInt64 piece;
      public UInt64 moveTo;
      public int color;
  
      public bool quiet;
      public UInt64 capturedPiece;
      public int capturedColor;
  }
  
  ```
  
  Search function:
  
  ```
  public static void search(int depth, Board board, string prevMove = "nothing"){
  
      // If at the end of the tree, return.
  
      depth++;
      if (depth == maxDepth) return;
  
      Console.Write("Number of moves at depth {0} is {1}. It is {2}'s turn.", depth, board.allMoves.Count, board.color == 0 ? "White" : "Black");
      if (prevMove != "nothing") Console.WriteLine(" Previous move was knight to " + prevMove);
      else Console.WriteLine("");
  
      // Uncomment below line for a list of all possible moves.
      //if (true) foreach (Board.Move move in board.allMoves) Console.WriteLine(String.Format("    {0} {1} on square {2} can move to {3}.", move.color == 0 ? "White" : "Black", move.pieceType.ToString(), help.square(move.piece), help.square(move.moveTo)));
  
      foreach (Board.Move move in board.allMoves){
          Board newBoard = board;
          newBoard.color = 1 - board.color;
          initialiseBoard(newBoard);
          newBoard = makeMove(newBoard, move);
          search(depth, newBoard, help.square(move.moveTo));
      }
  }
  
  ```
  
  Make move function:
  
  ```
  public static Board makeMove(Board board, Board.Move move){
  
      string moveColor = move.color == 0 ? "White" : "Black";
      string boardColor = board.color == 0 ? "White" : "Black";
      string pmove = help.square(move.moveTo);
      //Console.WriteLine(String.Format("    Making {0} move {2}: It is {1}'s turn.", moveColor, boardColor, pmove));
      
      Board toReturn = board;
      // Remove piece from board
      if (toReturn.color == 0 & move.color == 0){
          if (move.pieceType == Piece.Type.Knight){
              toReturn.WhiteKnightsBitboard &= ~move.piece; // Get rid of piece on board
              toReturn.WhiteKnightsBitboard |= move.moveTo; // Add piece to board
          }
      }
      else if (toReturn.color == 1 & move.color == 1){
          if (move.pieceType == Piece.Type.Knight){
              toReturn.BlackKnightsBitboard &= ~move.piece;
              toReturn.BlackKnightsBitboard |= move.moveTo;
          }
      }
  
      Game.node++; // To count nodes
      return toReturn;
  
  }
  
  ```
  
  Initialise Board function:
  
  ```
  static void initialiseBoard(Board board){
      BoardOperations.updatePieces(board); // Sets all pieces and finds empty squares 
      board.generateAttackSquares(board);  // Finds white knight moves and puts them into board.allMoves
  }
  
  ```
  
  **Tags:** engines, programming, bitboard
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-07-04. Score: 5 | Accepted answer
  
  The issue may be here:
  
  ```
  Board newBoard = board;
  
  ```
  
  This creates a new variable, but it doesn't actually create a new `Board`. `board` and `newBoard` are pointing to the exact same `Board` object. So, when your code changes anything in `newBoard`, it also changes it in `board`, and when it changes, you get this error.
  
  What you may want to do is make an actual copy of `board`, so when you change it, it doesn't change the original.
  
  > **Comments:**
  > **username_0**: That's strange, but I can see how it makes sense. How would I make a copy? Would I have to start with `Board newBoard = new Board();` then copy over all components? Is there an easier way to do this? Is this efficient too? Thank you very much!  
  > **username_0**: Thank you very much! I just made my own and this fixed the problem. I will look into performance soon!  
  > **username_1**: Yes, that's pretty much it, unless you have an easier way of making a new Board or there's already a built-in copy function. Also note that those components themselves may need to be copied, because the same thing might happen. `newBoard.someObject = board.someObject` would make `someObject` be literally the same object in both.  
  > **username_2**: I don't know what the `Board` class looks like, but presumably you're going to want to add a `Copy` method to it, and you might be able to implement such a method like so: `public Board Copy() { return MemberwiseClone(); }`  
  > **username_3**: alternatively you could add an `undoMove` function to put the board back the way it was.  You would call it after `search` in your for-loop.  
  
  ---
  
  