Question: Tower of Hanoi . Java fx, recursive. Need help writing a program like this. Should show count as counted, number of calls to method, and

Tower of Hanoi . Java fx, recursive. Need help writing a program like this. Should show count as counted, number of calls to method, and number of disks textfield should be cleared as button is pressed. I will post the code I need to implement GUI in bottom of post, this code writes to console. I need to implement GUI to something like the picture underneath. Thanks! Tower of Hanoi . Java fx, recursive. Need help writing a program

Tower of Hanoi recursive code ::

/** Main method */

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

// Read number of disks, n

System.out.print("Enter number of disks: ");

int n = input.nextInt();

// Find the solution recursively

System.out.println("Moves are:");

moveDisks(n, 'A', 'B', 'C');

System.out.println("Number of calls to the method is: " + count);

}

static int count = 0;

/** The method for finding the solution to move n disks

from fromTower to toTower with auxTower */

public static void moveDisks(int n, char fromTower,

char toTower, char auxTower) {

count++;

if (n == 1) // Stopping condition

System.out.println("Move disk " + n + " from " +

fromTower + " to " + toTower);

else {

moveDisks(n - 1, fromTower, auxTower, toTower);

System.out.println("Move disk " + n + " from " +

fromTower + " to " + toTower);

moveDisks(n - 1, auxTower, toTower, fromTower);

}

}

}

Tower Of Hanoi Number of disks Find moves Moves are: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: Move number: 1 move disk 1 from A to C 2 move disk 2 from A to B 3 move disk 1 from C to B 4 move disk 3 from A to C 5 move disk 1 from B to A 6 move disk 2 from B to C 7 move disk 1 from A to C 8 move disk 4 from A to B 9 move disk 1 from C to B 10 move disk 2 from C to A 11 move disk 1 from B to A 12 move disk 3 from C to B 13 move disk 1 from A to C 14 move disk 2 from A to B 15 move disk 1 from C to B Number of calls to the method is: 15

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!