简体   繁体   中英

C# When using foreach on a list of arrays, the foreach only iterates 5 times even though the list has length 86

I'm writing a chess engine in C#, and I'm trying to debug move generation. I've been using breakpoints so far to check variables, but I need a better way to debug. Despite the fact that a breakpoint shows that this list has a length of 86, the loop only runs 5 times. The only thing I can think of is that the arrays in the list have a length of 5, but I have no idea what would be causing this.

foreach (int[] debugBoard in futures)
{

    for (int i = 0; i < 5; i++)
    {
        Debug.Write(debugBoard[i].ToString().PadLeft(3, ' '));
    }
    Debug.Write("\n");
    int[,] debugOutBoard = new int[8, 8];
    Array.Copy(chessBoard.Pieces(), debugOutBoard, 64);

    if (debugBoard[0] < 0 || debugBoard[1] < 0 || debugBoard[2] < 0 || debugBoard[3] < 0 || debugBoard[0] > 7 || debugBoard[1] > 7 || debugBoard[2] > 7 || debugBoard[3] > 7) 
        break;

    debugOutBoard[debugBoard[2], debugBoard[3]] = debugOutBoard[debugBoard[0], debugBoard[1]];
    debugOutBoard[debugBoard[0], debugBoard[1]] = 0;
    int rowLength = debugOutBoard.GetLength(0);
    int colLength = debugOutBoard.GetLength(1);

    for (int i = 0; i < rowLength; i++)
    {
        for (int j = 0; j < colLength; j++)
        {
            Debug.Write(debugOutBoard[i, j].ToString().PadLeft(3, ' '));
        }
        Debug.Write(Environment.NewLine + Environment.NewLine);
    }
}

Also, I tried using a concurrentbag to store moves in (I was going to multithread the move processing later on) but as soon as the foreach loop touched the concurrent bag, all the memory values of the bag changed to one value. I've been stuck on this roadblock for days, and I really need help.

Your if statement breaks out of the for loop when its condition is met, I presume this happens for the first time on the 5th/6th iteration.

What I meant to do is iterate to the next element. What command would I use to do that?

You need to use continue instead of break

If 'futures' contains 86 items the only way it could stop iterating is if an exception occurs. Visual studio (In default settings) should break when this occurs unless you handle the exception somewhere.

Wrap the whole thing in a try{} catch{} and set a breakpoint in catch and see if it hits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM