So I'm trying to animate the common puzzle Tower of Hanoi. I already wrote the algorithm to do this in the console, but I want to make a JApplet that pops up and animates the puzzle being solved after I ask for the number of disks. Here is my code for the algorithm if that helps. Just looking for some instruction, no need to write out the entire code. Thanks.
This is my code for the algorithm.
public class TowerofHanoi extends JFrame{
static int count= 0;
public void move(int n, String start, String auxiliary, String end) {
if (n == 1) {
count++;
System.out.println(start + " -> " + end);
} else {
count++;
move(n - 1, start, end, auxiliary);
System.out.println(start + " -> " + end);
move(n - 1, auxiliary, start, end);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TowerofHanoi towersOfHanoi = new TowerofHanoi();
System.out.print("Enter number of discs: ");
Scanner scanner = new Scanner(System.in);
int discs = scanner.nextInt();
towersOfHanoi.move(discs, "A", "B", "C");
System.out.println("This puzzle took "+count+" moves.");
}
public void paint(Graphics g) {
g.drawRect (10, 10, 200, 200);
}
public TowerofHanoi(){
setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
}
This is my code for the JApplet.
public class Graphics_TOH {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame frame = new JFrame ("Draw Person");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
TowerofHanoi panel = new TowerofHanoi ();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}