Search code examples
javaartificial-intelligencesudoku

Sudoku solution checker


I have a program in Java that check if a sudoku is valid or not, I have two methods, the first is check the sum of all columns, sub-grids, rows and tell if is 45, and the second is check if the sum of all the sudoku is 405 or not if yes, so the sudoku is valid so I'm looking for a counter example, that in input I have a valid sudoku but the programme tell that is not valid , so this is the code :

public class test_checker {
    static final boolean valide=true;
    static final boolean non_valide=false;

    // verify every sub-grid if it is valid
        static boolean check_subgrid(int a[][],int ei,int ej){
            int sum=0;
            for(int i=ei;i<ei+3;i++){           
                for(int j=ej;j<ej+3;j++){
                    sum=sum+a[j][i];
                }
            }
            if(sum!=45) return non_valide;
            else return valide;
        }
        //verify a sudoku by sum of every row & column & sub-grid
        static boolean Checker1(int a[][]){
            int sum=0;
            //check row
            for(int i=0;i<9;i++){
                sum=0;
                for(int j=0;j<9;j++){
                    sum=sum+a[i][j];
                }
                if(sum!=45) return non_valide;

            }
            //check column
            for(int i=0;i<9;i++){
                sum=0;
                for(int j=0;j<9;j++){
                    sum=sum+a[j][i];
                }
                if(sum!=45) return non_valide;
            }       
            //check sub-grid
            for(int i=0;i<9;i=i+3){
                for(int j=0;j<3;j=j+3){
                    if(check_subgrid(a,i,j)==non_valide) return non_valide;
                }
            }
            return valide;
        }
        //verify by sum of all sudoku  
        static boolean Checker2(int a[][]){
            int sum=0;
            for(int i=0;i<9;i++){           
                for(int j=0;j<9;j++){
                    sum=sum+a[i][j];
                }
            }   
            if(sum!=405) return non_valide;
            else return valide;
        }


    public static void main(String[] args) {
        int [][] sudoku =
            {
                {1,2,3,4,5,6,7,8,9},
                {4,5,6,7,8,9,1,2,3},
                {7,8,9,1,2,3,4,5,6},
                {3,1,2,9,7,8,6,4,5},
                {6,4,5,3,1,2,9,7,8},
                {9,7,8,6,4,5,3,1,2},
                {2,3,1,5,6,4,8,9,7},
                {5,6,4,8,9,7,2,3,1},
                {8,9,7,2,3,1,5,6,4}
            };
        if(Checker1(sudoku)) System.out.println("it's valide (checker1)!");
        else System.out.println("it's not valide !");
        if(Checker2(sudoku)) System.out.println("it's valide (checker2) !");
        else System.out.println("it's not valide !");

    }

}

Solution

  • As @John Bollinger said

    you should be asking is whether there is an invalid 9 by 9 grid that nevertheless does satisfy your criteria.

    The simplest 2 cases, which comes to my mind are:

    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5},
    {5,5,5,5,5,5,5,5,5}
    

    and your example, where you would put 0's instead of 1's and 10's instead of 9's. Make sure each fields are distinct in rows, columns and sub-grids and are in interval of [1,9].