Diagonal and vertical win condition checks

How do I create a function that creates diagonal and vertical check win conditions for connect 4? i have the horizontal one here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool hwin(){
    for(int r=0; r<6; r++){
        for (int c=0; c<7;c++){	
	    if ((board [r][c] == 'X'||board [r][c] == 'O')&& 
		board[r][c] == board[r+1][c]&&
		board[r][c+1] == board[r+2][c]&&
		board[r][c+2] == board[r+3][c]&&
		board[r][c+3] == board[r+4][c]&&
		board[r][c+4] == board[r+5][c]&&
		board[r][c+5] == board[r+6][c]&&
		board[r][c+6] == board[r+7][c]&&
	        )
		    return true;
	    }		
    }
    return false;
}
r is rows and c is columns
Last edited on
go back to the drawing board


from c to c+6 you have seven cells
from r to r+7 you have eight cells
¿why are you checking fourteen cells when you should only be looking at four?

that's supposed to work for an horizontal line, yet you move on `r' (vertically)

take one of your checks
board[r][c+5] == board[r+6][c]
those points are not even near
also, you just ask them both to be the same, they may be empty, or from a different color that the starting cell

given that r goes from [0; 5], then r+7 goes from [7; 12]
¿do those cell even exist?
That's how my teacher presented it, so I'm assuming that's how it works. Unless you got another code?
That's how my teacher presented it
Your prof is pulling your leg, or not a very good programmer.

The board is 6 rows and 7 columns. That means a horizontal run must start in columns 0-3. So a horizontal winner is something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Check for a winner.
// Return 'X' or 'O' if one of them wins.
// Return 'F' if the board is fill.
// Return 'C' if there is no winner and play should continue
char checkWinner()
{
// Check for horizontal winner
for (r=0; r<6; ++r) {
    for (c=0; c<4; ++c) {
        if ((board[r][c] == 'X' || board[r][c] == '0') &&
             board[r][c] == board[r][c+1] &&
             board[r][c] == board[r][c+2] &&
             board[r][c] == board[r][c+3]) {
            return board[r][c];
        }
   }
}
// To Do: Check for vertical and diagonal winner, and for a full board.
return 'C';
}

Topic archived. No new replies allowed.