Ich halte es für keine so gute Idee die Scheiben als Strings zu speichern. Besser ist es die relevanten Daten der Scheiben, die jeweilige Größe zu verwenden.
z.B. Die Scheibe "/\/\" hat die Größe 2. die String lenght() Funktion liefert aber 4.
Aber das ist ja die Vorgabe.
Hier ein Beispiel mit Integer. Die Lösung hilft Dir vielleicht die Probleme bei Deinem Code in den Griff zu bekommen.
[CODE=java]public class start {
private static int cnt = 0;
public static void main(String[] args) {
int size = 22;
HanoiTower srcTower = new HanoiTower(size);
HanoiTower tmpTower = new HanoiTower(size);
HanoiTower destTower = new HanoiTower(size);
srcTower.fill();
move(size, srcTower, tmpTower, destTower);
System.out.println(destTower);
System.out.println("Scheiben\t: " + size);
System.out.println("Verschiebungen\t: " + cnt);
}
private static void move(int numDisc, HanoiTower srcTower, HanoiTower tmpTower, HanoiTower destTower) {
if (numDisc <= 0)
return;
move(numDisc - 1, srcTower, destTower, tmpTower);
destTower.push(srcTower.pop());
cnt++;
move(numDisc - 1, tmpTower, srcTower, destTower);
}
}[/CODE]
[CODE=java]public class HanoiTower {
public final static int EMPTY = -1;
private int[] discs; // each disc is represented by its size
private int position; // points to tower's current top position
/**
* @param height
* Specifies maximum tower's maximum height.
**/
public HanoiTower(int height) {
position = EMPTY;
discs = new int[height];
}
public void fill() {
position = EMPTY;
int size = getMaxHeight();
for (int i = 0; i < getMaxHeight(); i++)
push(size--);
}
private static String getMultipleStr(String str, int num) {
StringBuffer tmp = new StringBuffer();
for (int i = 0; i < num; i++)
tmp.append(str);
return tmp.toString();
}
/**
* @return Tower's maximum height.
*/
public int getMaxHeight() {
return discs.length;
}
/**
* @return Disc's size on towers's top or EMPTY.
*/
public int top() {
if (!isEmpty())
return discs[position];
System.out.println("Zugriff auf leeren Stab");
return EMPTY;
}
/**
* Removes disc on tower's top.
*
* @return Disc's size on towers's top or EMPTY.
*/
public int pop() {
if (isEmpty()) {
System.out.println("Zugriff auf leeren Stab");
return EMPTY;
}
return discs[position--];
}
public boolean isEmpty() {
return position == EMPTY;
}
public boolean isFull() {
return position == getMaxHeight() - 1;
}
/**
* Puts a disc on tower's top. Provided that new disc is smaller than disc on
* top and tower isn't full.
*
* @param size
* Size of new disc.
* @return true if successful.
*/
public boolean push(int size) {
if (isFull()) {
System.out.println("Maximale Anzahl an Scheiben erreicht");
return false;
}
if (!isEmpty() && (discs[position] < size)) {
System.out.println("Kleinere Scheibe vorhanden");
return false;
}
discs[++position] = size;
return true;
}
/**
* @return Tower's height.
*/
public int height() {
return position + 1;
}
@Override
public String toString() {
StringBuffer tmp = new StringBuffer();
for (int i = position; i >= 0; i--) {
tmp.append(getMultipleStr(" ", i));
tmp.append(getMultipleStr("/\\", (discs[i])));
tmp.append(System.getProperty("line.separator"));
}
return tmp.toString();
}
}[/CODE]