i have the code below which solve the sudoku by check every random number if is not duplicate in the sudoku but doesn't work & i think it goes in infinite loop because it does'int show any result , if my method is completly false please tell me about the write way and edit the code for me please , because i need in emergency for my research .
package essai;
import java.util.Random;
public class Essai_checker {
static final boolean valide=true;
static final boolean non_valide=false;
//check number if not deplicate in row or colomn or sub grid
static boolean checkInt(int a[][],int test,int c_i,int c_j){
if(test==0) return non_valide;
//check ligne
for(int i=0;i<9;i++){
if(test==a[c_i][i]) return non_valide;
}
//check colomn
for(int i=0;i<9;i++){
if(test==a[i][c_j]) return non_valide;
}
//check sub_grid
int ii=c_i/3;
int jj=c_j/3;
// this test are for telling wich sub-grid contain the number
if(ii<3) c_i=0;
else
if(ii>1 && ii<2) c_i=3;
else if(ii>2 && ii<=3) c_i=6;
if(jj<3) c_j=0;
else
if(jj>1 && jj<2) c_j=3;
else if(jj>2 && jj<=3) c_j=6;
for(int i=c_i;i<c_i+3;i++){
for(int j=c_j;j<c_j+3;j++){
if(test==a[c_i][c_j]) return non_valide;
}
}
return valide;
}
public static void main(String[] args) {
int [][] soduko2={
{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}
};
Random r=new Random();
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
int entier=r.nextInt(9)+1;
boolean init =checkInt(soduko2,entier,i,j);
while(init ==non_valide){
entier=r.nextInt(9);
init =checkInt(soduko2,entier,i,j);
}
if(init=true)
soduko2[i][j]=entier;
}
}
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
System.out.println(soduko2[i][j]+",");
}
}
System.out.println("Done!");
}
}
Aucun commentaire:
Enregistrer un commentaire