import java.util.*;
/*
* PuzelShuffle.java
*
* Created on 12. August 2003, 11:40
*/
/**
*
* @author Darimont
*/
public class PuzelShuffle {
int[][] puzel = {{0,2,3},{4,5,6},{7,8,9}}; // 0 = Freies Feld
Vector merkeShuffle;
int currentX;
int currentY;
final int obersteZeile;
final int untersteZeile;
final int rechteSpalte;
final int linkeSpalte;
/** Creates a new instance of PuzelShuffle */
public PuzelShuffle() {
obersteZeile = 0; //ganz oben
untersteZeile = puzel.length; // ganz unten
rechteSpalte = puzel[0].length; // ganz rechts
linkeSpalte = 0; // ganz links
merkeShuffle = new Vector();
}
private void printPuzel(int[][] a){
for(int i = 0; i< a.length; i++){
for(int j = 0; j < a[i].length; j++)
System.out.print(a[i][j] + " ");
System.out.println();
}
}
private void doIt(){
int runden = 0;
int StartX = 0;
int Starty = 0;
int zufall = 1;
Random r = new Random();
printPuzel(puzel);
swap(puzel,0,0, zufall,0);
while (runden++ < 20){
System.out.println();
printPuzel(puzel);
zufall = (int)(Math.random() * 1000);
if (zufall > 500)
swap(puzel,currentY,currentX, zufall % 2,0);
else
swap(puzel,currentY,currentX, 0,(zufall % 3) -1);
}
System.out.println("--------------------------------------");
System.out.println("---------------UNDO-------------------");
System.out.println("--------------------------------------");
undoShuffle();
/*
swap(puzel,0,0, 1,0); // 0 Startet bei 0,0 mit verschiebung nach unten
System.out.println();
printPuzel(puzel);
swap(puzel,currentY,currentX, 1,0);
System.out.println();
printPuzel(puzel);
swap(puzel,currentY,currentX, 0,1);
System.out.println();
printPuzel(puzel);
swap(puzel,currentY,currentX, 0,1);
System.out.println();
printPuzel(puzel);
undoShuffle();
*/
}
private int[][] shuffle(int anzahlDurchgang, int a[][]){
return null;
}
private void undoShuffle(){
for(int i = merkeShuffle.size() -1 ; i >= 0; i--){
int[] ta = (int[]) merkeShuffle.elementAt(i);
swap(puzel, ta[0] + ta[2], ta[1] + ta[3], -ta[2],-ta[3]);
System.out.println();
printPuzel(puzel);
}
}
/*
*int obersteZeile;
*int untersteZeile;
*int rechteSpalte;
*int linkeSpalte;
*
* z1 : Zeile von der geschoben werden soll
* s1 : Splate von der geschoben werden soll
*
* dz = Y - Richtung --> 1 unten, -1 oben
* ds = X - Richtung --> 1 rechts, -1 links
*
*/
private void swap(int[][] a, int z1, int s1, int dz, int ds){
if ( dz != 0){
// Y - Bewegung sinnvoll ?
if ((z1 < untersteZeile) && (z1 >= obersteZeile)){
if ( dz == 1) //Nach unten;
{
if (( (z1 + dz) < untersteZeile ) && ( a[z1][s1] == 0 )){
int temp;
temp = a[z1+dz][s1];
a[z1+dz][s1] = a[z1][s1];
currentY = z1 + dz;
a[z1][s1] = temp;
saveShuffle(z1,s1, dz, ds);
}
}
if (dz == -1) //Nach oben
{
if (( (z1 + dz) >= obersteZeile ) && ( a[z1][s1] == 0 )){
int temp;
temp = a[z1+dz][s1];
a[z1+dz][s1] = a[z1][s1];
currentY = z1 + dz;
a[z1][s1] = temp;
saveShuffle(z1,s1, dz, ds);
}
}
}
}
// X - Bewegung sinnvoll ?
if(ds != 0){
if( (s1 < rechteSpalte) && (s1 >= linkeSpalte)){
if( ds == 1){ //nach rechts
if (((s1 + ds) < rechteSpalte) && ( a[z1][s1] == 0)){
int temp;
temp = a[z1][s1+ds];
a[z1][s1+ds] = a[z1][s1];
currentX = s1 + ds;
a[z1][s1] = temp;
saveShuffle(z1,s1, dz, ds);
}
}
if (ds == -1){ //nach links
if ((( s1 + ds ) >= linkeSpalte) && ( a[z1][s1] == 0)){
int temp;
temp = a[z1][s1+ds];
a[z1][s1+ds] = a[z1][s1];
currentX = s1 + ds;
a[z1][s1] = temp;
saveShuffle(z1,s1, dz, ds);
}
}
}
}
}
private void saveShuffle(int z1, int s1,int dz, int ds){
int a[] = new int[4];
a[0] = z1;
a[1] = s1;
a[2] = dz;
a[3] = ds;
merkeShuffle.add(a);
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new PuzelShuffle().doIt();
}
}