Hanoi Towers Java Recursion

Good evening everyone,

I have a quick question about the homework that my class does for recursion. The idea is that we have this tower from the hanoi program, and we need to write basic information that will display a table that displays the numbers 5-25, and how many moves it will take to solve a tower of this size, for example

5 ---- 31 Move

6 ---- 63 Travel

etc...

I'm in a bit of trouble since the TowersOfHanoi class is set up to print out each move, and I don't think we should get rid of this, but I'm not sure.

Here is the class TowersOfHanoi

public class TowersOfHanoi {
    private int totalDisks;
    private int count;

    public TowersOfHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);
        }
        else {
            moveTower (numDisks-1, start, temp, end);
            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
}

Now I just need to write the main one that will create this table without printing every move for each tower, but I'm not quite sure how to do this. Any help is much appreciated

+3
3

TowersOfHanoi , . main, ( 5 25). solve.

, .

+2

print moveOneDisk, main, , .

private void moveOneDisk(int start, int end) {
    count = count+1;
    //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
}

public static void main(){
TowersOfHanoi tower = TowersOfHanoi(5);
tower.solve();
system.out.print(tower.count);
}//end of main

print moveOneDisk , , . , . tower.count

0

average time intervals;

public class TowersofHanoi {
    public int totalDisks;
    public static int count;

    public TowersofHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);


        }
        else {
            moveTower (numDisks-1, start, temp, end);

            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
    public static void main(){
        TowersofHanoi tower = new TowersofHanoi(5);
        tower.solve();

        System.out.println(tower.count);
        }

}
0
source

All Articles