Need help converting the following code for Tower of Hanoi moves to javafx: impo
ID: 3730927 • Letter: N
Question
Need help converting the following code for Tower of Hanoi moves to javafx:
import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Tower{
private JLabel label;
private JLabel label2;
private JLabel label3;
private JTextField textfield;
private static JTextField textfield2;
private static JTextArea jtArea;
private JButton button;
private JPanel panel;
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:");
//display();
new Tower();
// 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 */
static String st1="";
public static void moveDisks(int n, char fromTower, char toTower, char auxTower) {
count++;
if (n == 1){ // Stopping condition
jtArea.append(" Move disk " + n + " from " + fromTower + " to " + toTower);
}
else {
moveDisks(n - 1, fromTower, auxTower, toTower);
jtArea.append(" Move disk " + n + " from " + fromTower + " to " + toTower);
moveDisks(n - 1, auxTower, toTower, fromTower);
}
//textfield2.setText(st1);
}
public Tower()
{
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600,600);
frame.setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(10,10,600,600);
panel.setLayout(null);
textfield = new JTextField();
textfield.setBounds(5,30,100,20);
//textfield2=new JTextField();
jtArea=new JTextArea();
jtArea.setBounds(10,50,400,400);
JButton button = new JButton("Find Moves");
button.setBounds(130,30,100,20);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String s=textfield.getText().toString();
int n = Integer.parseInt(s);
System.out.println("Moves are:");
new Tower();
moveDisks(n, 'A', 'B', 'C');
jtArea.append(" Number of calls to the method is: " + count);
}
});
panel.add(button);
panel.add(textfield);
panel.add(jtArea);
frame.add(panel);
frame.setVisible(true);
}
}
Explanation / Answer
USE THIS CODE THIS MAY HELP YOU
public class towersofhanoi
{
public static void move(intn,int startpole,int endpole)
{
if(n==0)
{ return;}
int intermediate pole=6 -startpole -endpole;
move(n-1,startpole,intermediatepole)
system.out.println("move"+n+ "from" +startpole+ "to" +endpole+);
move(n-1,intermediatepole,endpole);
}
public static void main( string args[])
move(5,1,3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.