nested for loops

When i get rid of the top for loop and replace 'i' with 7(7th row of the board), the program works correctly. Since i want to check every row i added the top for loop. When i do that, the program doesn't end when 5 'X's come up in a row.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
  bool gameOn(char board[][5]){
    int counter=0;
    for(int i=7;i<7;i--){
        for(int j=0;j<6;j++){
            if(board[i][j]=='X'){
            counter=counter+1;
            }
        }
    }
    if(counter ==5){
        cout <<"You win game"<<endl;
        return true;
    }else{
        return false;
    }
}
for(int i=7;i<7;i--) <<------- should be i < 0 ?
Last edited on
that works thank you.
actually its not working. when i get 4 X's it returns true. I put the if after the count is done for that row, but now it doesn't end when i have 5 X's in a row.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool gameOn(char board[][5]){
    int counter=0;
    for(int i=7;i>0;i--){
        for(int j=0;j<6;j++){
            if(board[i][j]=='X'){
            counter=counter+1;
            }
            if(counter ==5){
                 cout <<"You win game"<<endl;
                 return true;
            }
            else
            {
        return false;
            }
        }
    }
}
Last edited on
for(int i=7;i>0;i--) <<--- shouldn't be i < 0 ? xD

i > 0 effectively means "skip my 0 row"
Last edited on
If i am starting at the 7th row i want to go down a row until i hit the last one. So changed it to i>=0. but now when the 7th row is all 'X's it exits correctly but when the 6th row is all 'X's it doesnt exit.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool gameOn(char board[][5]){
    int counter=0;
    for(int i=7;i>=0;i--){
        for(int j=0;j<6;j++){
            if(board[i][j]=='X'){
                counter=counter+1;
            }
        }
            if(counter ==5){
                 cout <<"You win game"<<endl;
                 return true;
            }
            else
            {
        return false;
            }
        }
}
I tried that and still get same propblem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
bool gameOn(char board[][5]){
    
    for(int i=7;i>=0;i--){
        int counter =0;
        for(int j=0;j<6;j++){
            if(board[i][j]=='X'){
                counter=counter+1;
            }
        }
            if(counter ==5){
                 cout <<"You win game"<<endl;
                 return true;
            }
            else
            {
        return false;
            }
        }
}
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
X X X X X X
0 0 0 0 0 0

Is the test above supposed to pass? If yes, then you have a bug because your counter will end up being equal to 6 and not 5
Last edited on
Break it down into 2 functions and use meaningful variable names, maybe it help you reason about it, like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
constexpr int Rows = 7, Columns = 5;

bool isRowWin(char row[Columns], int size)
{
	for (int i = 0, columns = size; i < columns; i++)
		if (row[i] == 'X') size--;

	return !size;
}

bool isWin(char board[Rows][Columns], int rows = Rows, int cols = Columns) {

	for (int i = 0; i < rows; i++) {
		if (isRowWin(board[i], cols)) {
			cout << "You win game" << endl;
			return true;
		}
	}
	return false;
}


Also you should be using vector, much simpler :)
Last edited on
in your above example, it should only be 5 items in a row. and it should then pass if it finds 5 'X's
What you have in your code is
for(int j=0;j<6;j++)
which means you iterate from 0 trough 5, which is 6 elements.
Last edited on
Topic archived. No new replies allowed.