1 2 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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
| package sudoku;
public class Algorithme {
//déclaration de la grille de sudoku
int[][] sudoGridCopie = new int[9][9];
private int[][] sudoGrid = {
{ 0, 0, 0, 0, 9, 0, 0, 8 ,0 },
{ 7, 0, 0, 6, 0, 0, 4, 0, 0 },
{ 0, 8, 0, 0, 5, 4, 9, 3, 7 },
{ 3, 4, 8, 0, 0, 6, 0, 0, 2 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 4 },
{ 2, 0, 0, 5, 0, 0, 6, 1, 8 },
{ 9, 7, 5, 1, 6, 0, 0, 4, 0 },
{ 0, 0, 3, 0, 0, 8, 0, 0, 1 },
{ 0, 1, 0, 0, 3, 0, 0, 0, 0 }
};
//constructeur
public Algorithme() {
// TODO Auto-generated constructor stub
}
//méthode pour dupliquer la grille de base
public int copieGridBase(){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
sudoGridCopie[i][j] = sudoGrid[i][j];
}
}
return 0;
}//fin de la méthode copieGridBase()
//méthode pour checker les lignes
public boolean testLine(int[] line){
for(int i = 0; i < line.length; i++){
for(int j = 0; j < line.length; j++){
if(line[i] == line[j] && i!=j && line[i]!=0){
return false;
}
}
}
return true;
}//fin de la methode testLine()
//Méthode pour checker les colones
public boolean testCol(int colToCheck){
for(int col = 0; col < 9; col++){
int i = 0;
for(i = i+col; i < 9; i++){
if(sudoGrid[i][colToCheck] == sudoGrid[col][colToCheck] && i != col && sudoGrid[col][colToCheck] != 0){
return false;
}
}
}
return true;
}//fin de la méthode testCol
public int[] test3x3(int col, int line){
int lineMin, lineMax, colMin, colMax;
colMin = (col/3)*3;
colMax = colMin+2;
lineMin = (line/3)*3;
lineMax = lineMin+2;
int tabFor3x3[] = new int[9];
for(int i = lineMin; i <= lineMax; i++){
for(int j = colMin; j <= colMax; j++){
for(int x = 0; x < 9; x++){
tabFor3x3[x] = sudoGrid[lineMin+(x/3)][colMin+(x%3)];
}
}
}
return tabFor3x3;
}
//methode pour retourner true ou false en fonction du resultat des autres méthodes
public boolean testGrid(int row, int col){
return testCol(col) && testLine(sudoGrid[row]) && testLine(test3x3(col, row));
}
//methode pour envoyer toutes les col et line de la grille
//à la methode pour tester toutes les methode et return un boolean final
public boolean testGridFinal(){
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(!testGrid(i, j)){
return false;
}
}
}
return true;
}
public String getNextCellToUpdate(int line, int col){
String NextCell = "";
return NextCell;
}
public String getPreviousCellToUpdate(int line, int col){
return "";
}
//méthode pour compléter la grille de Sudoku
public int completGrid(){
int row = 0;
int col = -1;
while(row < 9){//tan que row est plus petit que 9
if(sudoGrid[row][col] == 9){//si la position est égale a 9
sudoGrid[row][col] = 0;//on remet la case à 0
getPreviousCellToUpdate(row, col);//on prend la précédente case updatable
}else{//sinon
sudoGrid[row][col] += 1;//on ajoute 1 au nombre acutel
if(testGrid(row, col)){//si le test de la grille passe
getNextCellToUpdate(row, col);//on prend la prochaine case updatable
}else{//sinon
break;
}//fin du second if
}//fin du if
}//fin du while
return 0;
}//fin de la méthode comletGrid
//sert a définir quels sont les champs de départ qui sont updatable
public boolean canUpdate(int line, int col, int value){
if(sudoGrid[line][col] == 0 && value <= 9 && value > 0){
return true;
}else{
return false;
}
}
public int showTab(){
for(int i = 0; i < 9; i++){
System.out.println(" ");
for(int j = 0; j < 9; j++){
System.out.print(sudoGrid[i][j]+" ");
}
}
return 0;
}
public static void main(String[] args) {
Algorithme algo = new Algorithme();//instance de la classe Algorithme
algo.copieGridBase();
//System.out.println(algo.completGrid());
//System.out.println(algo.showTab());
//System.out.println("the result of the final test is : "+algo.testGridFinal());
System.out.println(algo.getNextCellToUpdate(5, 2));
}//fin du main
}//fin de la class Alorithme |
Partager