Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Anonymous
Anonymous asked in Computers & InternetProgramming & Design · 1 decade ago

Java sudoku question?

Ok so I have to create a sudoku board that is completely filled, but i cant figure out how to put in the numbers. Heres my code so far.

public class Sudoku {

private int N = 3; //sets N to 3

private int[][]board; // creates the board.

private int square = N*N;

public Sudoku() //constructor sets all values to 0

{

board = new int [N*N][N*N]; //sets board size

for (int x=0; x< square; x++){

for(int y=0; y< square; y++){

board [x][y]=0; // sets all elements to 0

}

}

}

public int getSquare(int i, int j){ // Gets the value of board[i][j]

if(i >=0 && i< square && j>=0 && j<square)

return board[i][j];

else

return 0;

}

public void setSquare(int i, int j, int val) // Sets board[i][j] = a value between 1 - 9.

{

board[i][j] = val;

}

public void show()

{

for (int i=0; i< square; i++)

{

if (i%N == 0 && i > 0)

System.out.println("------+------+------");

for(int j=0; j< square; j++){

if (board [i][j] == 0)

System.out.print(" .");

else

System.out.print(" " + board [i][j]);

if ((j+1)%N == 0 && j < square -1)

System.out.print("|");

}

System.out.println();

}

}

public static void main (String []Args)

{

Sudoku s = new Sudoku();

s.show();

}

}

//end code

So it creates a board, but doesn't fill any of the numbers in. My teacher said to use a

public void fill(String[])

but I dont know how to do this, and at this point, I just want to be able to put numbers in. Can any1 help?

Update:

ok, maybe i phrased this wrong. I only need to put the numbers in once. The point of the program was to generate a complete and correct sudoku board from nothing.

Im supposed to use the public void fill(string[]) to use a string array of numbers, and pull numbers off the array one at a time and put them on the grid, but i dont know how to do this. Any ideas?

2 Answers

Relevance
  • Anonymous
    1 decade ago
    Favorite Answer

    The problem is that you're drawing the board via System.out. You can't alter whats already been sent to the console. However you have a few options.

    1. Set the squares BEFORE drawing the board

    2. Redraw the board after every change

    3. Make an actual graphical interface

    Edit: small working example, feel free to do whatever you want with it

    public class Sudoku {

        private int N = 3; //sets N to 3

        private int[][]board; // creates the board.

        private int square = N*N;

       

        public Sudoku() //constructor sets all values to 0   

        {

            board = new int [N*N][N*N]; //sets board size

            for (int x=0; x< square; x++){

                for(int y=0; y< square; y++){

                    board [x][y]=0; // sets all elements to 0

                }

            }

        }   

        public void drawBoard(){

            String divider = "";

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

                divider += "+---";

            }

            divider = divider.substring(1);

           

            for(int x=0; x < N; x++){

                if(x > 0)

                    System.out.println(divider);

                for(int y=0; y < N; y++){

                    if(y > 0)

                        System.out.print("|");

                    System.out.print(" "+getSquare(x,y)+" ");

                }

                System.out.println();

            }

            System.out.println();

        }

       

        public int getSquare(int x, int y){

            return board[x][y];

        }

       

       

        public void setSquare(int i, int j, int val) // Sets board[i][j] = a value between 1 - 9.

        {

            board[i][j] = val;

        }

       

        public static void main (String[] args){

            Sudoku s = new Sudoku();

            s.drawBoard();     

        }

    }

  • 1 decade ago

    I wrote my sudoku solver in Python, not Java, but I can offer some advice.

    The hard part for a person or a computer is to determine what number can be placed in a square without creating an immediate conflict (duplication in a row, column or box) or an unfillable square later on. There will be some recursion involved. Mine looked like a method that accepted a row index, a column index, and a number to fill there; and returned a boolean true if that moved led to a filled board position, or false if it did not. If the return is true, the board is filled, and on a false return the board is unchanged.

    The algorithm is to:

    1. Make the indicated move.

    2. Pick an empty location on the board. If the board is full, return true.

    3. For each value that isn't immediatley blocked in that cell:

    ... 3.1 Call self recursively on that cell and value to test the move.

    ... 3.2 If 3.1 returned true, then return true as a result.

    4. At this point there is no value that could go into the empty cell and the current board in unfillable. Undo the initial move in step 1, and return false.

    That's just one possibility. You can use random starting points in steps 2 and 3 so that each run doesn't produce the same solution, but rather a randomly-selected solution; but you might want to add that detail after everything else is working.

    I don't know what your teacher meant with that fill(String[]) either, but I do know that it's a good idea for you to ask.

Still have questions? Get your answers by asking now.