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.
Trending News
Sudoku Java Question?
Ok 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 x){ // Gets the value of board[i][x]
if(i >=0 && i< square && x>=0 && x<square)
return board[i][x];
else
return 0;
}
public void setSquare(int i, int x, int val) // Sets board[i][j] = a value between 1 - 9.
{
board[i][x] = val;
}
public void show()
{
for (int i=0; i< square; i++)
{
if (i%N == 0 && i > 0)
System.out.println("------+------+------");
for(int x=0; x< square; x++){
if (board [i][x] == 0)
System.out.print(" .");
else
System.out.print(" " + board [i][x]);
if ((x+1)%N == 0 && x < square -1)
System.out.print("|");
}
System.out.println();
}
}
public static void main (String []Args)
{
Sudoku s = new Sudoku();
s.show();
}
}
now what i need to know is after I set all the numbers = to 0, how can I put in an array of numbers in its place? I dont need it to be user interactive, I can create the array and pick numbers that work, I just need to know how to implement it?
I asked this question before but every1 just gives me examples of full sudoku games and Im a beginner so I dont understand it. Could someone please tell me how to set every element of the board[][] 2d array with numbers?
4 Answers
- deonejuanLv 71 decade ago
when you do this...
int[][] sud = new int[ 9 ] [ 9 ];
that becomes...
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
the 'new' puts in the default value of Zero in every slot.
OK, if you...
import java.util.Arrays;
...
Arrays.fill( sud[1], 444 );
// there are many convenient methods. Try this program
import java.util.Arrays;
public class ArraysFillTest {
public static void main( String [] args ) {
int[][] sud = new int[ 9 ] [ 9 ];
System.out.println( Arrays.deepToString( sud));
Arrays.fill( sud[1], 22);
System.out.println( Arrays.deepToString( sud));
Arrays.fill(sud[1], 2,6, 9 );
System.out.println( Arrays.deepToString( sud));
}
}
- Anonymous5 years ago
Haven't thought about this
- Anonymous5 years ago
Extremely good question, hope we get some good answers