| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 
 | public class Sudoku {
 
//initialisation du tableau
private int grille[][] = {
{2,0,0,0,0,8,0,0,0},
{0,0,8,7,6,0,0,0,3},
{0,3,0,5,0,0,0,6,0},
{0,0,2,0,0,0,0,0,1},
{0,0,9,0,8,0,4,0,0},
{7,0,0,0,0,0,2,0,0},
{0,2,0,0,0,4,0,3,0},
{4,0,0,0,9,7,1,0,0},
{0,0,0,8,0,0,0,0,4}
};
 
 
 
 
public static void main(String[] args) {
 
System.out.println ("Demarrage de l'application SU DO KU"+ "\n");
Sudoku sudoku = new Sudoku();
sudoku.afficheTableau();
System.out.println ("Fin de l'application SU DO KU");
}
 
//constructeur
public Sudoku(){
if (!this.controlTableau()){
System.out.println("Les données d'initialisation sont erronées");
}else{
System.out.println("Les données d'initialisation sont valides");
}
 
}
 
//affiche le tableau a l'écran
public void afficheTableau(){
String tableau = "";
for(int i = 0; i < grille.length; i++){
tableau += this.construitLigne(i) +"\n";
 
}
System.out.println(tableau);
}
 
// construit l'affichage d'une ligne du tableau
 
private String construitLigne(int noLigne){
String ligne = "";
for(int i = 0; i < grille.length; i++) {
ligne += grille[noLigne][i]+ " ";
}
ligne = ligne.replace("0","_");
return ligne;
}
 
private boolean controlTableau(){
boolean result = true;
for(int i = 0; i < grille.length; i++){
result = this.chercheDoublon(convertLigneVersTab(i));
if (!result)break;
result = this.chercheDoublon(convertColonneVersTab(i));
if (!result)break;
result = this.chercheDoublon(convertZoneVersTab(i));
if (!result)break;
}
return result;
}
 
// Vérifier que les nombres de 0 à 9 ne soient pas en double dans le tableau
 
private boolean chercheDoublon(int[] tab){
for(int i = 0; i < tab.length; i++){
if(tab[i] != 0){
if(indiceDansTab(tab,i) > 0){
return false;
}
}
}
return true;
}
// Vérifier si une valeur est présente dans un tableau à l'exclusion de l'indice indiceCle
private int indiceDansTab(int tab[], int indiceCle){
//on boucle sur chaque occurance du tableau
for(int i = 0; i < tab.length; i++){
 
//si l'indice en cours est différent de l'indice d'exclusion
if (i != indiceCle){
 
//si l'indice de la valeur testée est la meme que celle de l'indice clé alors
//on return la valeur de l'indice trouvé en doublon
if (tab[i] != 0 & tab[i] == tab[indiceCle]){
return i;
}
}
}
 
//si lôn arrive à ce point c'est qu'aucun doublon na été trouvé.
return -1;
}
 
private int[] convertZoneVersTab(int noZone){
int[] tab = new int[grille.length];
int nbLarg = (int)Math.sqrt(grille.length);
int ligneDeb = (Math.abs(noZone / nbLarg))*3;
int colonneDeb = (noZone - ligneDeb) * 3;
int ligneFin = ligneDeb + nbLarg;
int colonneFin = colonneDeb + nbLarg;
for(int ligne = ligneDeb; ligne < ligneFin; ligne++){
for(int colonne = colonneDeb; colonne < colonneFin; colonne++){
tab[((colonne - colonneDeb)* nbLarg)+(ligne - ligneDeb)] =
grille[ligne][colonne];
}
}
return tab;
}
 
private int[] convertLigneVersTab(int noLigne){
int[] tab = new int[grille.length];
for(int i = 0; i < tab.length; i++){
tab[i] = grille[noLigne][i];
}
return tab;
}
 
private int[] convertColonneVersTab(int noColonne){
int[] tab = new int[grille.length];
for(int i = 0; i < tab.length; i++){
tab[i] = grille[i][noColonne];
}
 
return tab;
 
}
 
} | 
Partager