-2

I have written a java program to check if sudoku solution is valid or not, but its not working,

It's giving false for all values, the program logic is correct according to me.

I want to do this without hashsets, and in the simnplest way, hence I have made the program very simple, please dont suggest hashsets or anything but still its not working. Any suggestions on how to fix this program would be of great help.

import java.util.*;

class sudoku {
private int[][] sudoku;
public sudoku() {
    sudoku = new int[9][9];
}
public sudoku(int sudoku[][]) {
    this.sudoku = sudoku;
}
private boolean containsInRow(int row, int number) {

    for (int i = 0; i < 9; i++) {
        if (sudoku[row][i] == number) {


            return true;
        }
    }
    return false;
}
private boolean containsInCol(int col, int number) {

    for (int i = 0; i < 9; i++) {

        if (sudoku[i][col] == number) {

            return true;
        }
    }
    return false;
}

private boolean containsInBox(int row, int col, int number) {

    int r = row - row % 3;
    int c = col - col % 3;
    for (int i = r; i < r + 3; i++) {
        for (int j = c; j < c + 3; j++) {
            if (sudoku[i][j] == number) {


                return true;
            }
        }

    }
    return false;
}

private boolean isAllowed(int row, int col, int number) {
boolean checkforCol = containsInCol(col, number);
boolean checkforBox = containsInBox(row, col, number);
boolean checkforRow = containsInRow(row, number);



    return !( checkforBox || checkforCol || checkforRow);
}

public static void main(String ar[]) {
    Scanner sc = new Scanner(System.in);
    int count = 0;


    int[][] board = 
    {
    {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},

    {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},
    
    {3,1,2  ,6,4,5, 9,7,8},
    {6,4,5  ,9,7,8, 3,1,2},
    {9,7,8  ,3,1,2, 6,4,5}
   };
    sudoku a = new sudoku(board);
    for (int i = 0; i < 9; i++) {
        for (int j = 0; j < 9; j++) {
            boolean c = a.isAllowed(i, j, board[i][j] ) ;

           if(c == true) {
               count++;
           }

        }
    }
    if(count == 81) {
    System.out.print("Valid");
    }
    else {
    System.out.print("Invalid");
    }
}

}

IIT Topper
  • 39
  • 7
  • 1
    *Suggestions on how to fix this program:* Use a **debugger** to find the problem. Or were you hoping we'd debug your code for you? – Andreas Oct 29 '20 at 17:10
  • I have debugged the program, there is no syntax error – IIT Topper Oct 29 '20 at 17:12
  • 4
    You apparently don't know what a debugger is, if you think it has anything to do with syntax errors, so: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Oct 29 '20 at 17:13
  • any solution on how to make this program working would be of great help. Thanks:) – IIT Topper Oct 29 '20 at 17:21
  • Is this an assignment? Otherwise, why are you crippling yourself by depriving you of one of the tools used to help you? – NomadMaker Oct 29 '20 at 19:21

2 Answers2

2

In your checks functions for rows, columns and boxes you are checking if it contains the number. Whick is always true because you are also checking the case inside your number is.

You should either skip the case within the number you are checking is in isAllowed and checksCol Row and Box or change your checking function to make them counts the occurrences of numbers and check in isAllowed if each numbers have only one occurrence.

Something like this:

import java.util.*;

class sudoku {
    private int[][] sudoku;
    public sudoku() {
        sudoku = new int[9][9];
    }
    public sudoku(int sudoku[][]) {
        this.sudoku = sudoku;
    }
    private int containsInRow(int row, int number) {
        int c = 0;
        for (int i = 0; i < 9; i++) {
            if (sudoku[row][i] == number) {
            c++;    
            }
        }
        return c;
    }
    private int containsInCol(int col, int number) {
        int c = 0;
        for (int i = 0; i < 9; i++) {
            if (sudoku[i][col] == number) {
                c++;
            }
        }
        return c;
    }
    private int containsInBox(int row, int col, int number) {
        int count = 0;
        int r = row - row % 3;
        int c = col - col % 3;
        for (int i = r; i < r + 3; i++) {
            for (int j = c; j < c + 3; j++) {
                if (sudoku[i][j] == number) {
                    count++;
                }
            }
        }
        return count;
    }
    private boolean isAllowed(int row, int col, int number) {
        int checkforCol = containsInCol(col, number);
        int checkforBox = containsInBox(row, col, number);
        int checkforRow = containsInRow(row, number);

        return !( (checkforBox!=1) || (checkforCol!=1) || (checkforRow!=1) );
    }
    public static void main(String ar[]) {
        Scanner sc = new Scanner(System.in);
        int count = 0;

        int[][] board = 
        {
        {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},

        {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},
    
        {3,1,2  ,6,4,5, 9,7,8},
        {6,4,5  ,9,7,8, 3,1,2},
        {9,7,8  ,3,1,2, 6,4,5}
        };
        sudoku a = new sudoku(board);
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                boolean c = a.isAllowed(i, j, board[i][j] ) ;
               if(c == true) {
                   count++;
               }
            }
        }
        if(count == 81) {
            System.out.print("Valid");
        }
        else {
            System.out.print("Invalid");
        }
    }
}
Kerat
  • 1,284
  • 10
  • 15
1

You need to check if a set of 9 one-digit numbers are all the numbers 1-9.

Easiest way to do this is with bit-manipulation, i.e. set the bit correlating to the number, and the result must be 0b1111111110, aka 0x3FE, aka 01776, aka 1022.

If a number is present twice, then another number would be missing, i.e. that bit would be 0.

So, e.g.

private boolean validRow(int row) {
    int bits = 0;
    for (int col = 0; col < 9; col++)
        bits |= 1 << sudoku[row][col];
    return bits == 0b1111111110;
}
private boolean validColumn(int col) {
    // TODO
}
private boolean validBox(int row, int col) {
    // TODO
}
private boolean validBoard() {
    for (int row = 0; row < 9; row++)
        if (! validRow(row))
            return false;
    // TODO Test columns
    // TODO Test boxes
    return true;
}
Andreas
  • 154,647
  • 11
  • 152
  • 247