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
| import java.util.*;
public class Grille {
//public static void main(String[] args) {
int[][] tab = new int[][] {
{ 0, 4, 3, 7, 0, 0, 9, 0, 8 },
{ 0, 0, 5, 0, 3, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 3, 0, 0 },
{ 6, 0, 0, 0, 2, 7, 2, 0, 3 },
{ 4, 0, 7, 0, 0, 0, 1, 0, 3 },
{ 0, 0, 0, 5, 4, 0, 0, 0, 9 },
{ 0, 0, 2, 0, 0, 0, 0, 3, 0 },
{ 0, 0, 0, 0, 5, 0, 4, 0, 0 },
{ 5, 0, 4, 0, 0, 1, 2, 6, 0 } };
//}
public Grille(ArrayList<Integer> al){
int nbcol = 9;
int nblig = 9;
//ArrayList<Integer> liste ;
ArrayList<Integer> liste = new ArrayList<Integer> ();
if (liste.size() != nbcol * nblig) {
System.out
.println("erreur : pas assez ou trop de données dans la liste");
}
int ligne = 0;
int colonne = 0;
for (Integer e : al) {
tab [ligne][colonne] = e;
colonne++;
if (colonne >= nbcol) {
colonne = 0;
ligne++;
}
}
}
public void GrilleToArray(ArrayList<Integer> al) {
al = new ArrayList<Integer> (ArrayList(tab));
for (int nblig = 0; nblig <tab.length; nblig++) {
for(int nbcol = 0; nbcol < tab.length; nbcol++) {
al.add(tab [nblig] [nbcol]);
}
}
}
private Collection<Integer> ArrayList(int[][] tab2) {
// TODO Auto-generated method stub
return null;
}
public boolean estSurLaLigne(int _Valeur, int _ligne) {
for (int i = 0; i < 9; i++) {
if (tab [_ligne][i] == _Valeur) {
System.out.println("cette valeur est deja presente sur cette ligne");
return true;
}
}
return false;
}
public boolean estSurLaColonne(int _Valeur, int _colonne) {
for (int j = 0; j < 9; j++) {
if (tab [_colonne][j] == _Valeur) {
System.out.println("cette valeur est deja presente sur cette colonne");
return true;
}
}
return false;
}
public boolean estDansLeBloc(int _Valeur, int _ligne, int _colonne) {
return tab[_ligne][_colonne] == _Valeur;
}
private static final String tiret = "------------------------------------\n";
public void imprimeGrille () {
StringBuilder sb = new StringBuilder();
sb.append(tiret);
for (int i = 0; i < tab.length; i++) {
if (i == 3 || i ==6) {
sb.append(tiret);
}
sb.append("|");
for (int j = 0; j < tab[i].length; j += 3) {
for (int kj = j; kj < j + 3; kj += 1) {
if (tab[i] [kj] != 0)
sb.append(tab[i] [kj]);
else
sb.append(" ");
sb.append(" ");
}
sb.append("| ");
}
sb.append("\n");
}
System.out.println(sb);
sb.append(tiret) ;
}
public static void main (int[ ][] args){
}
} |
Partager