This question already has an answer here:
- Connect 4 check for a win algorithm 5 answers
I am developing a Connect 4 game with 7x7 fields, horizontal and vertical checks but I dont get diagonal checks works
I can win as long as one of the tokens is not in the last column.
This are all checks:
private static int getWinningInDiagonals() {
// Top-Left -> Bottom-Right
for (int column = 0; column < 7; column++) {
int count = 0;
for (int row = 0; row < 7; row++) {
if (field[row][column] != 0 && field[row+1][column + row - 1] == field[row][column])
count++;
else
count = 1;
if (count >= 4) {
return field[row][column];
}
}
}
// Top-Right -> Bottom-Left
for (int column = 0; column < 7; column++) {
int count = 0;
for (int row = 0; row < 7; row++) {
if (field[row][column] != 0 && field[row+1][column - row + 1] == field[row][column])
count++;
else
count = 1;
if (count >= 4) {
return field[row][column];
}
}
}
return 0;
}