G
G@st
Gast
Hi,
ich hab folgendes Problem. Ich baue gerade an einer Klasse, die für mich das bekannte Springerproblem aus der Mathematik lösen soll. Den Code an sich bitte nicht so sehr auf seine Effizienz bemängeln, ich weiß, dass er eine Katastrophe ist. Mir geht es mehr darum, herauszufinden, warum mein Programm niemals aus der Rekursion zurückkehrt xO
[
Fällt euch da auf die Schnelle was auf?
ich hab folgendes Problem. Ich baue gerade an einer Klasse, die für mich das bekannte Springerproblem aus der Mathematik lösen soll. Den Code an sich bitte nicht so sehr auf seine Effizienz bemängeln, ich weiß, dass er eine Katastrophe ist. Mir geht es mehr darum, herauszufinden, warum mein Programm niemals aus der Rekursion zurückkehrt xO
[
Java:
package springerproblem;
import java.awt.Point;
import java.util.*;
public class SpringerProblem {
int [][] aktuellesFeld = new int [5][5];
int [][] speicherFeld = new int [5][5];
static int x, y;
static double zahl = 0;
int[] initFeld = new int [2];
static boolean check1 = true;
static boolean check2 = true;
static boolean check3 = true;
static boolean check4 = true;
static boolean check5 = true;
static boolean check6 = true;
static boolean check7 = true;
static boolean check8 = true;
static Stack<Point> stack = new Stack<Point>();
public static void main (String [] args ){
SpringerProblem sp = new SpringerProblem();
sp.initialise();
berechne();
}
private static void berechne() {
zahl++;
//SpringerProblem intern_sp = new SpringerProblem();
//Pseudocode: Setze aktuelle Werte von x und y auf Stack.length - 1
x = stack.peek().x;
y = stack.peek().y;
checkNext();
choseNext();
}
private void initialise() {
Scanner sc = new Scanner(System.in);
System.out.println("Geben sie das Startfeld ein: ");
System.out.println("Koordinate x: ");
x = sc.nextInt();
System.out.println("Koordinate y: ");
y = sc.nextInt();
Point p = new Point(x,y);
stack.add(p);
}
// Prüft ob die 8 theoret. mögl. Felder in diesem Durchgang erreichbar sind
private static void checkNext() {
if (((x+2) > 0 && (x+2) <= 5) && ((y+1) > 0 && (y+1) <= 5)){
check1 = true;
}
else {
check1 = false;
}
if (((x+1) > 0 && (x+1) <= 5) && ((y+2) > 0 && (y+2) <= 5)){
check2 = true;
}
else {
check2 = false;
}
if (((x-1) > 0 && (x-1) <= 5) && ((y+2) > 0 && (y+2) <= 5)){
check3 = true;
}
else {
check3 = false;
}
if (((x-2) > 0 && (x-2) <= 5) && ((y+1) > 0 && (y+1) <= 5)){
check4 = true;
}
else {
check4 = false;
}
if (((x-2) > 0 && (x-2) <= 5) && ((y-1) > 0 && (y-1) <= 5)){
check5 = true;
}
else {
check5 = false;
}
if (((x-1) > 0 && (x-1) <= 5) && ((y-2) > 0 && (y-2) <= 5)){
check6 = true;
}
else {
check6 = false;
}
if (((x+1) > 0 && (x+1) <= 5) && ((y-2) > 0 && (y-2) <= 5)){
check7 = true;
}
else {
check7 = false;
}
if (((x+2) > 0 && (x+2) <= 5) && ((y-1) > 0 && (y-1) <= 5)){
check8 = true;
}
else {
check8 = false;
}
}
private static void choseNext() {
try {
System.out.println(x + " " + y);
if (check1) {
System.out.println("Check1");
stack.add(new Point(x,y));
x = x + 2;
y = y + 1;
berechne();
} else if (check2) {
System.out.println("Check2");
stack.add(new Point(x,y));
x = x + 1;
y = y + 2;
berechne();
} else if (check3) {
System.out.println("Check3");
stack.add(new Point(x,y));
x = x - 1;
y = y + 2;
berechne();
} else if (check4) {
System.out.println("Check4");
stack.add(new Point(x,y));
x = x - 2;
y = y + 1;
berechne();
} else if (check5) {
System.out.println("Check5");
stack.add(new Point(x,y));
x = x - 2;
y = y - 1;
berechne();
} else if (check6) {
System.out.println("Check6");
stack.add(new Point(x,y));
x = x - 1;
y = y - 2;
berechne();
} else if (check7) {
System.out.println("Check7");
stack.add(new Point(x,y));
x = x + 1;
y = y - 2;
berechne();
} else if (check8) {
System.out.println("Check8");
stack.add(new Point(x,y));
x = x + 2;
y = y - 1;
berechne();
} else {
stack.pop();
}
} catch (Exception e){
e.printStackTrace();
}
}
Fällt euch da auf die Schnelle was auf?