J
JosephineX
Gast
Hallo
Ich soll als Übungsaufgabe ein Programm zum Springerproblem implementieren, also ein Schachfeld mit n Feldern und es soll ein Pfad gefunden werden, so das der Springer auf jedem Feld genau einmal landet.
Dummerweise tut das Programm nicht was es soll bis jetzt und jetzt gibts auch noch eine Fehlermeldung:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at SpringerRek.NochFrei(SpringerRek.java:86)
at SpringerRek.position(SpringerRek.java:19)
at SpringerRek.main(SpringerRek.java:107)
Ich hab mich bisl schlau gemacht, und gefunden das wohl die ArrayGrenzen überschritten werden. Irgendwie versteh ich aber nicht wieso.
Kann mir irgendwer helfen das Programm zum laufen zu bringen?
Hier mal mein Code:
Ich soll als Übungsaufgabe ein Programm zum Springerproblem implementieren, also ein Schachfeld mit n Feldern und es soll ein Pfad gefunden werden, so das der Springer auf jedem Feld genau einmal landet.
Dummerweise tut das Programm nicht was es soll bis jetzt und jetzt gibts auch noch eine Fehlermeldung:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at SpringerRek.NochFrei(SpringerRek.java:86)
at SpringerRek.position(SpringerRek.java:19)
at SpringerRek.main(SpringerRek.java:107)
Ich hab mich bisl schlau gemacht, und gefunden das wohl die ArrayGrenzen überschritten werden. Irgendwie versteh ich aber nicht wieso.
Kann mir irgendwer helfen das Programm zum laufen zu bringen?
Hier mal mein Code:
Code:
public class SpringerRek
{
static int n = 8; //Feldgroesse
static int x =0, y=0; //Startwerte
static int[][] Jumper = new int[n*n][2]; //Springer
static boolean[][] Frei = new boolean[n][n]; //Feldbelegung
public static int[][] position(/*int[][] akt,*/int x,int y,int zug)
{
if ((NochFrei(x+2,y+1) == true) && (AufmFeld(x+2,y+1) == true)) //Sprungmöglichkeiten
{
x = x+2;
y = y+1;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x+2,y-1) == true) && (AufmFeld(x+2,y-1) == true))
{
x = x+2;
y = y-1;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x+1,y+2) == true) && (AufmFeld(x+1,y+2) == true))
{
x = x+1;
y = y+2;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x+1,y-2) == true) && (AufmFeld(x+1,y-2) == true))
{
x = x+1;
y = y-2;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x-1,y+2) == true) && (AufmFeld(x-1,y+2) == true))
{
x = x-1;
y = y+2;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x-1,y-2) == true) && (AufmFeld(x-1,y-2) == true))
{
x = x-1;
y = y-2;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x-2,y+1) == true) && (AufmFeld(x-2,y+1) == true))
{
x = x-2;
y = y+1;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else if ((NochFrei(x-2,y-1) == true) && (AufmFeld(x-2,y-1) == true))
{
x = x-2;
y = y-1;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
else
{
x=99; y= 99;
Jumper[zug][0] = x;
Jumper[zug][1] = y;
return Jumper;
}
}
public static boolean NochFrei(int aktX, int aktY) //Testet ob das Feld noch frei ist
{
if (Frei[aktX][aktY] == true)
{
Frei[aktX][aktY] = false;
return true;
}
else return false;
}
public static boolean AufmFeld(int x,int y) //Testet ob der Springer sich noch aufm Feld befindet
{
if(x >= n) {return false;} //Das Feld ist n*n gros, da Zählung bei 0 beginnt, mus x <= n-1 bleiben!
if(x < 0) {return false;}
if(y >= n) {return false;}
if(y < n) {return false;}
else {return true;} //Alles ok!
}
public static void main(String[] args)
{
for (int znum = 0; znum <= (n*n)-1; znum++)
{
SpringerRek.position(/*Jumper,*/x,y,znum);
System.out.println ("Sprung Nr " + znum + " x: " + Jumper[znum][0] + " y: " + Jumper[znum][1]);
}
}
}