Question

I am making a sudoku-style program with JPanel and the program must a legal starting position of the game, the game has to start with 3 to 7 (random) numbers already inserted in the panel in random positions (from 1 to 5).
I have a 5x5 grid with 25 JButtons, I have my random numbers and my random locations of these numbers, except I also have to follow the rules of the game which state that no number can repeat itself in its row and column.
I know I have to select each button (the number that is on the button) and go through the row and column and check if there is a repetition, if there is go back and change number, but I was unable to do so with the many codes I've tried.

public class Sudoku implements ActionListener {
    JButton[][] gumbi = new JButton[5][5];

    public Sudoku() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);

        JPanel panel = new JPanel(new GridLayout(5, 5));

        Random st = new Random();
        int stevila = st.nextInt(5) + 4;  
            // initial random number to be inserted

        int counter = 0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {

                Random flag = new Random();
                int f = flag.nextInt(2);            
                            // random location of number

                Random rnd = new Random();
                int r = rnd.nextInt(5) + 1;         
                            // random number, range 1-5

                if (counter < stevila && f == 0) {
                    gumbi[i][j] = new JButton("0");
                } else if (counter < stevila && f == 1) {
                    gumbi[i][j] = new JButton(Integer.toString(r));
                    counter++;
                    gumbi[i][j].setEnabled(false);
                } else {
                    gumbi[i][j] = new JButton("0");
                }

                gumbi[i][j].addActionListener(this);
                panel.add(gumbi[i][j]);
            }
        }

        frame.add(panel);

        frame.setVisible(true);
    }

I have to insert something resembling this (recursive or otherwise) to check if there are repetitions in rows and columns.

public boolean checkRow(int row, String num) {

        for (int col = 0; col < 5; col++)
            if (gumbi[row][col].getText() == num)
                return false;

        return true;
    }

public boolean checkCol( int col, String num ) {

          for( int row = 0; row < 5; row++ )
             if(gumbi[row][col].getText() == num)
                return false ;

          return true ;
    }

It's a whole mess with JPanel, as the buttons contain Strings and not ints (I have to use JButton.getText and then convert it to int with Integer.toString) and it's just... bad.

Was it helpful?

Solution

The following code snippet will check whether placing a particular number at a particular position is valid or not.

board is a 2d 9 x 9 array representing the sudoku game board. An empty cell i.e. where you have not yet put any number would contain zero.

row and col are the rwo and column locations of the number num you want to place

boolean isValidMove(int num, int row, int col)
{
    //1. Check whether the horizontal run contains the number
    for (int i = 0; i < 9; i++)
        if (board[row][i] == num) return false;

    //2. Check whether the vertical run contains the number
    for (int i = 0; i < 9; i++)
        if (board[i][col] == num) return false;

    //3. Check whether the 3x3 grid contains the number
    int starting_row = row / 3;
    int starting_col = col / 3;

    for (int i = starting_row * 3; i < starting_row * 3 + 3; i++)
        for (int j = starting_col * 3; j < starting_col * 3 + 3; j++)
            if (board[i][j] == num) return false;

    return true;
}

What you can do is implement a recursive backtracking algorithm that tries all possible combinations for a cell (namely 1...9). If a number can be put in that cell proceed to the next one. If not discard the contents of the current cell and move back to the previous one to place the next valid number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top