What is wrong with this line?

After checking with console output lines, I have determined that the below line is crashing my program, but I have no idea why. Whenever I give input that would cause the program to go over the line, it freezes and a window pops up saying that the program has stopped working.

hasBeenChecked [smallToCheckX [smallToCheckCounter]] [smallToCheckY [smallToCheckCounter]] = true;
Can we see the definitions of those variables and the surrounding code? Currently you're giving us your finger and asking what's wrong with your arm.
Right, here's the function that it is in:
(the problem line is line 62)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Checks for pieces to capture and captures them
void Board::capturePieces (int newPieceX, int newPieceY)
{
    // Copy the current pieces matrix
    std::vector< std::vector< pieces > > tempPiecesMatrix = piecesMatrix;

    // Make the large to check list
    std::vector< int > largeToCheckX (0);
    std::vector< int > largeToCheckY (0);

    // Make the large delete list
    std::vector< int > largeDeleteX;
    std::vector< int > largeDeleteY;


    // Add the new piece to the temporary pieces matrix
    tempPiecesMatrix [newPieceX] [newPieceY] = currentTurn;

    // For each branch off the placed piece, if the piece is valid and the enemy's, add it to the large to check list
    for (int x = newPieceX - 1; x <= newPieceX + 1; x += 2)
    {
        if (inXBounds (x) && (tempPiecesMatrix [x] [newPieceY] == enemyPiece))
        {
            largeToCheckX.push_back (x);
            largeToCheckY.push_back (newPieceY);
        }
    }

    for (int y = newPieceY - 1; y <= newPieceY + 1; y += 2)
    {
        if (inYBounds (y) && (tempPiecesMatrix [newPieceX] [y] == enemyPiece))
        {
            largeToCheckX.push_back (newPieceX);
            largeToCheckY.push_back (y);
        }
    }

    // For each item on the large to check list (each main branch)
    for (int largeToCheckCounter = 0; largeToCheckCounter != largeToCheckX.size (); ++largeToCheckCounter)
    {
        // Make the black found flag
        bool blankFound = false;

        // Make the matrix for recording if a space has been checked
        std::vector< std::vector< bool > > hasBeenChecked (xDim, std::vector< bool > (yDim, false));

        // Make the small to check lists
        std::vector< int > smallToCheckX (0);
        std::vector< int > smallToCheckY (0);

        // Make the small delete list
        std::vector< int > smallDeleteX (0);
        std::vector< int > smallDeleteY (0);

        // Set the large to check list item to the first item on the small to check list
        smallToCheckX.push_back (largeToCheckX [largeToCheckCounter]);

        // For each item on the small to check list
        for (unsigned int smallToCheckCounter = 0; smallToCheckCounter != smallToCheckX.size (); ++smallToCheckCounter)
        {
            // Mark the piece as checked
            hasBeenChecked [smallToCheckX [smallToCheckCounter]] [smallToCheckY [smallToCheckCounter]] = true;

            // Put the piece on the small delete list
            smallDeleteX.push_back (smallToCheckX [smallToCheckCounter]);
            smallDeleteY.push_back (smallToCheckY [smallToCheckCounter]);

            // Check around the piece on the x axis
            for (int x = smallToCheckX [smallToCheckCounter] - 1; x <= smallToCheckX [smallToCheckCounter] + 1; ++x)
            {
                // If x is not within the x bounds, continue
                if (!inXBounds (x))
                {
                    continue;
                }
                // If the new piece is the enemy's and unchecked, add it to the small to check list
                if ((tempPiecesMatrix [x] [smallToCheckY [smallToCheckCounter]] == enemyPiece) && (hasBeenChecked [x] [smallToCheckY [smallToCheckCounter]] == false))
                {
                    smallToCheckX.push_back (x);
                    smallToCheckY.push_back (smallToCheckY [smallToCheckCounter]);
                }

                // If the new piece is a blank space, set blankFound to true and break
                if (tempPiecesMatrix [x] [smallToCheckY [smallToCheckCounter]] == noPiece)
                {
                    blankFound = true;
                    break;
                }
            }

            // Check around the piece on the y axis
            for (int y = smallToCheckY [smallToCheckCounter] - 1; y <= smallToCheckY [smallToCheckCounter] + 1; ++y)
            {
                // If y is not within y bounds, continue
                if (!inYBounds (y))
                {
                    continue;
                }
                // If the new piece is the enemy's and unchecked, add it to the small to check list
                if ((tempPiecesMatrix [smallToCheckX [smallToCheckCounter]] [y] == enemyPiece) && (hasBeenChecked [smallToCheckX [smallToCheckCounter]] [y] == false))
                {
                    smallToCheckX.push_back (smallToCheckX [smallToCheckCounter]);
                    smallToCheckY.push_back (y);
                }

                // If the new piece is a blank space, set blankFound to true and break
                if (tempPiecesMatrix [smallToCheckX [smallToCheckCounter]] [y] == noPiece)
                {
                    blankFound = true;
                    break;
                }
            }

            // If blankFound is true, break to stop looking for more pieces from the current main branch
            if (blankFound == true)
            {
                break;
            }
        }

        // If blankFound is true, continue to start checking the next main branch
        //   without acting on the findings in the current branch
        if (blankFound == true)
        {
            continue;
        }

        // Add the contents of the small delete list to the large delete list
        for (unsigned int smallDeleteCounter = 0; smallDeleteCounter != smallDeleteX.size (); ++smallDeleteCounter)
        {
            largeDeleteX.push_back (smallDeleteX [smallDeleteCounter]);
            largeDeleteY.push_back (smallDeleteY [smallDeleteCounter]);
        }
    }

    // Now do the deletions from the temporary pieces matrix
    for (unsigned int largeDeleteCounter = 0; largeDeleteCounter != largeDeleteX.size (); ++largeDeleteCounter)
    {
        int x = largeDeleteX [largeDeleteCounter];
        int y = largeDeleteY [largeDeleteCounter];

        tempPiecesMatrix [x] [y] = noPiece;
    }

    // Now check for a ko infraction, if one is found, set the koAlert and return
    if (tempPiecesMatrix == twoTurnsAgo)
    {
        koAlert = true;
        return;
    }

    // Now actually capture the pieces
    for (unsigned int largeDeleteCounter = 0; largeDeleteCounter != largeDeleteX.size (); ++largeDeleteCounter)
    {
        capturePiece (largeDeleteX [largeDeleteCounter], largeDeleteY [largeDeleteCounter]);
    }

    // And actually place the piece
    piecesMatrix [newPieceX] [newPieceY] = currentTurn;
    return;
}
From what I can tell, the first time through smallToCheckY has nothing in it, and you're using the subscript operator to try and get something from nothing. If you use the .at() method you can check for when it throws an exception.
It turns out that at line 56, I forgot to do the same for the Y co-ordinate list.
Thanks for the help.
New problem in another function, the line 24 for loop sometimes gets stuck in an infinite loop

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
bool Board::suicideCheck (int newPieceX, int newPieceY)
{
    // Copy the board for a temporary copy
    std::vector< std::vector< pieces > > tempPiecesMatrix = piecesMatrix;

    // Make the to check list
    std::vector< int > toCheckX (0);
    std::vector< int > toCheckY (0);

    // Make the has been checked matrix
    std::vector< std::vector< bool > > hasBeenChecked (xDim, std::vector< bool > (yDim, false));

    // Assume a suicide is found until proven otherwise
    bool suicideFound = true;

    // Place a piece on the temporary board
    tempPiecesMatrix [newPieceX] [newPieceY] = currentTurn;

    // Put the new piece on the to check list
    toCheckX.push_back (newPieceX);
    toCheckY.push_back (newPieceY);

    // For each item on the to check list
    for (unsigned int toCheckCounter = 0; toCheckCounter != toCheckX.size (); ++toCheckCounter)
    {
        // Give the new piece's location shorter names
        int pieceX = toCheckX [toCheckCounter];
        int pieceY = toCheckY [toCheckCounter];

        // Mark the current piece as checked
        hasBeenChecked [pieceX] [pieceY] == true;

        // Check around the piece on the x axis
        for (int x = pieceX - 1; x <= pieceX + 1; x += 2)
        {
            // If the piece is on the board
            if (inXBounds (x))
            {
                // If the piece found is blank, set suicideFound to false and break
                if (tempPiecesMatrix [x] [pieceY] == noPiece)
                {
                    suicideFound = false;
                    break;
                }

                // If the piece found is friendly and unchecked, add it to the to check list
                if ((tempPiecesMatrix [x] [pieceY] == currentTurn) && (hasBeenChecked [x] [pieceY] == false))
                {
                    toCheckX.push_back (x);
                    toCheckY.push_back (pieceY);
                }
            }
        }

        // Check around the piece on the y axis
        for (int y = pieceY - 1; y <= pieceY + 1; y += 2)
        {
            // If the piece is on the board
            if (inYBounds (y))
            {
                // If the piece found is blank, set suicideFound to false and break
                if (tempPiecesMatrix [pieceX] [y] == noPiece)
                {
                    suicideFound = false;
                    break;
                }

                // If the piece found is friendly and unchecked, add it to the to check list
                if ((tempPiecesMatrix [pieceX] [y] == currentTurn) && (hasBeenChecked [pieceX] [y] == false))
                {
                    toCheckX.push_back (pieceX);
                    toCheckY.push_back (y);
                }
            }

        }

        // if a blank was found, stop looking in the to check list
        if (suicideFound == false)
        {
            break;
        }
    }

    // If a suicide is found, set the alert and return true, otherwise return false
    if (suicideFound == true)
    {
        suicideAlert == true;
        return true;
    }
    else
    {
        return false;
    }
}
Try changing toCheckCounter != toCheckX.size (); to toCheckCounter < toCheckX.size();. It's possible somewhere in your loop you increment toCheckCounter somehow. If it still loops infinitely you may be decrementing it or setting it.
I tried using '<' but it still looped. What do you mean by "setting it"? EDIT: Nevermind, I misread your post.

Last edited on
After further testing, It looks like a piece is added to the to check list even if it has been marked as checked. Any idea why?
Topic archived. No new replies allowed.