M
Max Matti
Gast
Hi,
Ich wollte mal ein Programm entwerfen, was (später auch nach Vorgaben) Sudokus bastelt.
Naja, das Ergebnis ist eher ernüchternd, es kommen momentan meistens 81 Nullen raus, die da alle nicht stehen sollten.
Vielleicht kann mir mal jemand weiterhelfen.
Der Code:
Danke im Voraus.
LG
Max Matti
Ich wollte mal ein Programm entwerfen, was (später auch nach Vorgaben) Sudokus bastelt.
Naja, das Ergebnis ist eher ernüchternd, es kommen momentan meistens 81 Nullen raus, die da alle nicht stehen sollten.
Vielleicht kann mir mal jemand weiterhelfen.
Der Code:
Java:
public class Sudoku {
/**
* @param args
*/
public static void main(String[] args) {
// Auto-generated method stub
int[][] a = new int[9][9];
boolean restart = false;
int i = 0; // Zählvariable für die erste Dimension - in jeder Funktion i
int j = 0; // Zählvariable für die zweite Dimension - in jeder Funktion j
while (i < 9 && !restart) {
j = 0;
while (j < 9 && !restart) {
if (!generiereZufallszahl(a, i, j)) {
/*
* generiereZufallszahl holt sich von gibZufallszahl eine Zufallszahl.
* Dann wird mit pruefeZeile, pruefe Spalte und pruefeQuadrat überprüft,
* ob die Zahl da überhaupt stehen darf.
* Wenn nicht, wird das mit der nächsten versucht.
* Nach 500 Versuchen wird ein neues Feld erstellt.
*/
restart = true;
break;
}
j++;
}
i++;
if (restart) {
i = 0;
j = 0;
a = new int[9][9];
}
}
i = 0;
j = 0;
while (i < 9) {
j = 0;
while (j < 9) {
if (a[i][j] == 0) {
generiereZufallszahl(a, i, j);
}
}
}
System.out.println("╔═══════════╦═══════════╦═══════════╗");
for (i = 0; i < 9; i++) {
System.out.println("║ " + a[i][0] + " │ " + a[i][1] + " │ " + a[i][2] + " ║ " + a[i][3] + " │ " + a[i][4] + " │ " + a[i][5] + " ║ " + a[i][6] + " │ " + a[i][7] + " │ " + a[i][8] + " ║");
if (i == 2 || i == 5) {
System.out.println("╠═══════════╬═══════════╬═══════════╣");
} else if (i < 8) {
System.out.println("║───┼───┼───║───┼───┼───║───┼───┼───║");
}
}
System.out.println("╚═══════════╩═══════════╩═══════════╝");
}
public static int gibZufallszahl() {
return (int) ((Math.random()*9)+1);
}
public static boolean generiereZufallszahl(int[][] a, int i, int j) {
a[i][j] = gibZufallszahl();
boolean retry = true;
int versuche = 0;
boolean ok = false;
while (retry && versuche < 500) {
retry = false;
if (!pruefeSpalte(a, i, j) || !pruefeZeile(a, i, j) || !pruefeQuadrat(a, i, j)) {
a[i][j] = gibZufallszahl();
retry = true;
versuche++;
}
}
if (versuche < 500) {
ok = true;
}
return ok;
}
public static boolean pruefeSpalte(int[][] a, int i, int j) {
int k = 0;
boolean success = true;
for (k=0; k<i && success; k++) {
if (a[i][j] == a[k][j]) {
success = false;
} else {
}
}
return success;
}
public static boolean pruefeZeile(int[][] a, int i, int j) {
int k = 0;
boolean success = true;
for (k=0; k<j && success; k++) {
if (a[i][j] == a[i][k]) {
success = false;
} else {
}
}
return success;
}
public static boolean pruefeQuadrat(int[][] a, int i, int j) {
boolean success = true;
int iversatz = 0;
int jversatz = 0;
int izaehler = 0;
int jzaehler = 0;
boolean test = true;
if (i == i % 3) {
iversatz = 0;
} else if (i == (i % 3) + 3) {
iversatz = 3;
} else if (i == (i % 3) + 6) {
iversatz = 6;
} else {
test = false;
}
if (j == j % 3) {
jversatz = 0;
} else if (j == (j % 3) + 3) {
jversatz = 3;
} else if (j == (j % 3) + 6) {
jversatz = 6;
} else {
test = false;
}
int[] b = {a[iversatz][jversatz], a[iversatz][jversatz+1], a[iversatz][jversatz+2], a[iversatz+1][jversatz], a[iversatz+1][jversatz+1], a[iversatz+1][jversatz+2], a[iversatz+2][jversatz], a[iversatz+2][jversatz+1], a[iversatz+2][jversatz+2]};
if (test) {
for (izaehler = 0; izaehler < b.length; izaehler++) {
for (jzaehler = 0; jzaehler < b.length; jzaehler++) {
if (b[izaehler] == b[jzaehler] && izaehler != jzaehler) {
success = false;
}
}
}
} else {
success = false;
}
return success;
}
}
Danke im Voraus.
LG
Max Matti