1

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.

2 Answers2

1

The mistake is you never make connection to false, Just add last else and make connection equal false or make connection to be false by default not true.

 public static boolean isConseccutiveFour (int[][] matrix){
        //creates a boolean to give answer later on
        boolean connection = false;
        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;
Fady Saad
  • 1,169
  • 8
  • 13
  • So I did what you said and had created an else statement that makes connection = false; and have the original equation equal false too but now all it does is return false as the answer. even when I have hard coded a connection to equal false. – JamesJSchindler Apr 15 '17 at 04:00
  • My answer was to do one either make it by default false or add an else statement, However I think you should make the condition ((consecFour.length - Y < 3) && (consecFour[0].length - X < 3)) to be ((consecFour.length - 1) - Y <= 3) && ((consecFour[0].length -1) - X <= 3) Because length starting from 1 and loop starting from 0 – Fady Saad Apr 15 '17 at 04:07
  • The answer is correct (+1) . I added an explanation that might help. – c0der Apr 15 '17 at 07:51
0

As user7790438 answered correctly, connection is never set to false, so method can only return true. To illustrate it, here is the skeleton of your code :

public static boolean isConseccutiveFour (int[][] matrix){

        boolean connection = true;

        for (....){

                if (....){
                        ....
                        connection = true;
                }

                else if (....){
                        ....
                        connection = true;
                }
                else if (....){
                        ....
                        connection = true;
                }

                else if (....){
                        ....
                        connection = true;
                }       
        }

        return connection;
    }

Can you see connection = false1 anywhere ?

c0der
  • 18,467
  • 6
  • 33
  • 65