Code:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
public class Solit extends JFrame
{
private static final long serialVersionUID = -4382011225551225106L;
final static int OBEN=1;
final static int UNTEN=2;
final static int LINKS=3;
final static int RECHTS=4;
final static int KEIN_STEIN=0;
final static int STEIN=1;
static int [][][] loesung= new int[1][7][7];
static Zug [] zug=new Zug[32];
static int currentStep=0;
static boolean zeichnen=false;
static boolean zugzeichnen=false;
static long zaehler=0;
public Solit()
{
super();
setDefaultCloseOperation(EXIT_ON_CLOSE);
for (int i=0;i<32;i++)
{
zug[i]= new Zug();
}
}
public void paint(Graphics gs)
{ // @Override ermöglicht dem Compiler die Kontrolle
Graphics2D g=(Graphics2D)gs;
int x,y;
int inhalt;
if(zeichnen)
{
g.setColor(Color.lightGray);
g.fillRect(0, 0, getWidth(), getHeight());
{
for (y=0;y<7;y++)
{
for (x=0;x<7;x++)
{
inhalt=loesung[currentStep-1][x][y];
if (inhalt == KEIN_STEIN)
{
g.setColor(Color.WHITE);
g.fillOval(x*100+150, y*100+150, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(x*100+150, y*100+150, 28, 28);
}
else if(inhalt == STEIN)
{
g.setColor(Color.GREEN);
g.fillOval(x*100+150, y*100+150, 30, 30);
}
}
}
}
}
if(zugzeichnen)
{
switch (zug[currentStep-1].richtung)
{
case OBEN:
g.setColor(Color.WHITE);
g.fillOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.fillOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y-1)*100+150, 30, 30);
g.setColor(Color.BLACK);
g.setColor(Color.lightGray);
g.drawOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.drawOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y-1)*100+150, 30, 30);
g.setColor(Color.GREEN);
g.fillOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y-2)*100+150, 30, 30);
break;
case UNTEN:
g.setColor(Color.WHITE);
g.fillOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.fillOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y+1)*100+150, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.drawOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y+1)*100+150, 30, 30);
g.setColor(Color.GREEN);
g.fillOval(zug[currentStep-1].x*100+150, (zug[currentStep-1].y+2)*100+150, 30, 30);
break;
case LINKS:
g.setColor(Color.WHITE);
g.fillOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.fillOval((zug[currentStep-1].x-1)*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.drawOval((zug[currentStep-1].x-1)*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.setColor(Color.GREEN);
g.fillOval((zug[currentStep-1].x-2)*100+150, zug[currentStep-1].y*100+150, 30, 30);
break;
case RECHTS:
g.setColor(Color.WHITE);
g.fillOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.fillOval((zug[currentStep-1].x+1)*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.setColor(Color.BLACK);
g.drawOval(zug[currentStep-1].x*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.drawOval((zug[currentStep-1].x+1)*100+150, zug[currentStep-1].y*100+150, 30, 30);
g.setColor(Color.GREEN);
g.fillOval((zug[currentStep-1].x+2)*100+150, zug[currentStep-1].y*100+150, 30, 30);
break;
default:
}
}
}
static void zug_zurueck(int A[][], int x, int y, int r)
{
switch (r)
{
case OBEN:
A[x][y]=1; A[x][y-1]=1; A[x][y-2]=0;
break;
case UNTEN:
A[x][y]=1; A[x][y+1]=1; A[x][y+2]=0;
break;
case LINKS:
A[x][y]=1; A[x-1][y]=1; A[x-2][y]=0;
break;
case RECHTS:
A[x][y]=1; A[x+1][y]=1; A[x+2][y]=0;
break;
default:
}
}
static void zug_aktuell(int A[][], int x, int y, int r)
{
switch (r)
{
case OBEN:
A[x][y]=0; A[x][y-1]=0; A[x][y-2]=1;
break;
case UNTEN:
A[x][y]=0; A[x][y+1]=0; A[x][y+2]=1;
break;
case LINKS:
A[x][y]=0; A[x-1][y]=0; A[x-2][y]=1;
break;
case RECHTS:
A[x][y]=0; A[x+1][y]=0; A[x+2][y]=1;
break;
default:
}
}
static boolean richtung(int A[][], int x, int y, int r)
{
switch (r)
{
case OBEN:
return (y>1) && (A[x][y-1]==1) && (A[x][y-2]==0);
case UNTEN:
return (y<5) && (A[x][y+1]==1) && (A[x][y+2]==0);
case LINKS:
return (x>1) && (A[x-1][y]==1) && (A[x-2][y]==0);
case RECHTS:
return (x<5) && (A[x+1][y]==1) && (A[x+2][y]==0);
default:
return false;
}
}
static void speichern(int A[][], int i)
{
for(int y=0;y<7;y++)
{
for(int x=0;x<7;x++)
{
loesung[i][x][y]=A[x][y];
}
}
}
static void zug_speichern(int x, int y, int richtung, int zug)
{
Solit.zug[zug].x=x;
Solit.zug[zug].y=y;
Solit.zug[zug].richtung=richtung;
}
static boolean soli (int A[][], int i, int r1, int r2, int r3, int r4)
{
boolean inhalt;
zaehler++;
if (i<31)
{
i++;
for (int y=0;y<7;y++)
{
for (int x=0;x<7;x++)
{
if(A[x][y]==STEIN)
{
if(richtung(A,x,y,r1))
{
zug_aktuell(A,x,y,r1);
inhalt=soli(A,i,UNTEN,RECHTS,LINKS,OBEN);
if(inhalt) {
zug_speichern(x,y,r1,i);
return true;
}
else
{
zug_zurueck(A,x,y,r1);
}
}
if(richtung(A,x,y,r2))
{
zug_aktuell(A,x,y,r2);
inhalt=soli(A,i,LINKS,OBEN,UNTEN,RECHTS);
if(inhalt) {
zug_speichern(x,y,r2,i);
return true;
}
else
{
zug_zurueck(A,x,y,r2);
}
}
if(richtung(A,x,y,r3))
{
zug_aktuell(A,x,y,r3);
inhalt=soli(A,i,OBEN,LINKS,RECHTS,UNTEN);
if(inhalt) {
zug_speichern(x,y,r3,i);
return true;
}
else {
zug_zurueck(A,x,y,r3);
}
}
if(richtung(A,x,y,r4))
{
zug_aktuell(A,x,y,r4);
inhalt=soli(A,i,RECHTS,UNTEN,OBEN,LINKS);
if(inhalt) {
zug_speichern(x,y,r4,i);
return true;
}
else {
zug_zurueck(A,x,y,r4);
}
}
}
}
}
i--;
return false;
}
else
{
return true;
}
}
public static int[][] initialisierung(int A[][])
{
A = new int [][]{
{ 9,9,1,1,1,9,9 },
{ 9,9,1,1,1,9,9 },
{ 1,1,1,1,1,1,1 },
{ 1,1,1,0,1,1,1 },
{ 1,1,1,1,1,1,1 },
{ 9,9,1,1,1,9,9 },
{ 9,9,1,1,1,9,9 }
};
return A;
}
public static void main(String[] args)
{
Solit f= new Solit();
f.setSize(1000, 1000);
f.setVisible(true);
int [][] A= new int [][]{
{ 9,9,1,1,1,9,9 },
{ 9,9,1,1,1,9,9 },
{ 1,1,1,1,1,1,1 },
{ 1,1,1,0,1,1,1 },
{ 1,1,1,1,1,1,1 },
{ 9,9,1,1,1,9,9 },
{ 9,9,1,1,1,9,9 }
};
speichern(A,0);
Zeitmesser.startZeitmessung(1);
for (int i=0;i<100;i++)
{
A=initialisierung(A);
soli(A,0,OBEN,UNTEN,LINKS,RECHTS);
}
System.out.println(Zeitmesser.getZeitmessung(1)/1000./100.);
Zeitmesser.stopZeitmessung(1);
System.out.println();
System.out.println(zaehler);
zeichnen=true;
currentStep++;
f.repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
zeichnen=false;
zugzeichnen=true;
for (int i=1;i<32;i++)
{
currentStep++;
f.repaint();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Zug
{
int x,y;
int richtung;
public Zug()
{
x=0;
y=0;
richtung=0;
}
}
}