I am coding for my programing class which is supposed to be made to read and fine if in a 2 layer multidimensional array has 4 equal values in a row in either a diagonal, vertical, or horizontal fashion. Here is my code.
public static boolean isConseccutiveFour (int[][] matrix){
//creates a boolean to give answer later on
boolean connection = true;
int[][] consecFour = matrix;
//incrementing for loops that move through the two arrays by going horizontally then diagonally
for (int Y = 0; Y < consecFour.length - 3; Y++){
for(int X= 0; X < consecFour[0].length - 3; X++){
//if statement used to give the proper constraints for diagonal check
if ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)){
if ( consecFour[X][Y] == consecFour[X + 1][Y + 1] && consecFour[X][Y] == consecFour[X+2][Y+2] && consecFour[X][Y] == consecFour[X+3][Y+3])
connection = true;
}
//if statement used to give the proper constraints for diagonal check
else if ((consecFour.length - Y < 3) && (consecFour[0].length < 3)){
if ( consecFour[X][Y] == consecFour[X-1][Y-1] && consecFour[X][Y] == consecFour[X-2][Y-2] && consecFour[X][Y] == consecFour[X-3][Y-3])
connection = true;
}
//if statement used to give the proper constraints for horizontal check
else if (consecFour[0].length - X < 3){
if(consecFour[X][Y] == consecFour[X+1][Y] && consecFour[X][Y] == consecFour[X+2][Y] && consecFour[X][Y] == consecFour[X+3][Y])
connection = true;
}
//if statement used to give the proper constraints for vertical check
else if (consecFour.length - Y < 3){
if ( consecFour[X][Y] == consecFour[X][Y + 1] && consecFour[X][Y] == consecFour[X][Y+2] && consecFour[X][Y] == consecFour[X][Y+3])
connection = true;
}
}
}
//return statement of boolean value
return connection;
My current problem is that it always returns true, no matter what array is put in and I know this may seem like a silly mistake but I really can't find what's wrong. I do have in my main statement before this method is called a check to make sure the input array has a length greater than 4 and the width is greater than 4. This is in java as you already know and answers would be appreciated.