i'm writing a Java program that verifies whether or not a set of 9x9, 2 dimensional array of integers is a possible solution of a sudoku puzzle. The problem is that the program I've written doesn't pick up on some integers which are side by side. For example, I put an 8 next to an 8 at 5x4 and it still returns true. Here are the methods to check the Rows by Columns and blocks of 3 by 3. Thank you in advanced
public class Sudoku{
private static boolean sudoku= true;
public static void main(String[] args){
int input[][]= {{1,8,4,9,6,3,7,2,5},
{5,6,2,7,4,8,3,1,9},
{3,9,7,5,1,2,8,6,4},
{2,3,9,6,5,7,1,4,8},
{7,5,6,8,8,4,2,9,3},//repeated "8" here
{4,1,8,2,3,9,6,5,7},
{9,4,1,3,7,6,5,8,2},
{6,2,3,8,9,5,4,7,1},
{8,7,5,4,2,1,9,3,6}};
RowXColumn(input);
if(sudoku == true)
Blocks(input);
else{
}
System.out.println("True or false, is this a sudoku puzzle solution: " + sudoku);
}
public static void RowXColumn(int [][]b){
for(int row= 0; row < a.length; row++){
for(int col=0; col< a[row].length; col++){//checks rows
int b= a[row][col];
for(int x= row+1; x< a.length; x++){
if(b == a[x][col])
sudoku= false;
else
continue;
}
for(int y= col+1; y< a[row].length; y++){//checks columns
if(b == a[row][y])
sudoku= false;
else
continue;
}
}
}
}
public static void Blocks(int [][]b){//checks 3x3 blocks
for(int d= 0; d< (b.length/3); d++){
for(int e= 0; e< (b[d].length/3); e++){
for(int d1= (d*3); d1< (b.length); d1+=3){
for(int e1= (e*3); e1< (b[e].length); e1+=3){
int bu= b[d1][e1];
for(int co=d1; co < 3; co++){
for(int col= e1 +1; e1< 3; e1++){
if(bu == b[co][e1])
sudoku= false;
else
continue;
}
}
}
}
}
}
}
}
After Some research I think I solved my own problem. Also, sorry for the bad explanation for the problem
public class Sudoku{
private static boolean sudoku= true;
public static void main(String[] args){
int input[][]= {{1,8,4,9,6,3,7,2,5},
{5,6,2,7,4,8,3,1,9},
{3,9,7,5,1,2,8,6,4},
{2,3,9,6,5,7,1,4,8},
{7,5,6,1,8,4,2,9,3},
{4,1,8,2,3,9,6,5,7},
{9,4,1,3,7,6,5,8,2},
{6,2,3,8,9,5,4,7,1},
{8,7,5,4,2,1,9,3,6}};
RowXColumn(input);
if(sudoku == true)
Blocks(input);
System.out.println("True or false, is this a sudoku puzzle solution: " + sudoku);
}
public static void RowXColumn(int [][]a){
for(int row=0; row < a.length; row++){
for(int col=0; col< a[row].length; col++){
for(int x= row+1; x< a.length; x++){//Checks columns
if(a[row][col] == a[x][col])
sudoku = false;
}
for(int y= col+1; y< a[row].length; y++){//Checks rows
if(a[row][col] == a[row][y])
sudoku = false;
}
}
}
}
public static void Blocks(int [][]b){
for(int row= 0; row< (b.length); row+= 3){
for(int col= 0; col< (b[row].length); col+= 3){
for(int p= row; p< (row+2); p++){
for(int p1= col; p1< (col+2); p1++){//picks out value to compare
for(int comp= p1+1 ; comp < 3-p1; comp++){//compares
if(b[p][p1] == b[p][comp])
sudoku= false;
}
}
}
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.