import javax.swing.*;
import java.awt.*;
public class TowerOfHanoi {
    int n=4;
    int numMoves=0;
    double  totalMoves=Math.pow(2,n)-1;
    JPanel commentary=new JPanel();
    JTextField moves=new JTextField("Moves: 0");
    JTextField  nextMove=new JTextField("Move disc from to");
    Tower A= new Tower("A", n);
    Tower B =new Tower("B", 0);
    Tower C= new Tower("C", 0);

    public static void main(String [] args){
        TowerOfHanoi t=new TowerOfHanoi();
        System.out.println("-----");
        t.createGUI();
        t.moveDiscs(t.n, t.A, t.B,t.C );
        t.A.print();
        t.B.print();
        t.C.print();
    }
    // Argument sequence: From, Via, To
    public void moveDiscs(int discs, Tower A, Tower B, Tower C) {
        if (discs==0){
            return;
        }else if (discs == 1) {
            move(A, C);
        } else {
            moveDiscs(discs - 1, A, C, B);
            move(A, C);
            moveDiscs(discs - 1, B, A, C);
        }
    }// End of moverDiscs
    public void move(Tower A, Tower C){
        int disc=A.pop();
        C.push(disc);
        A.print();
        C.print();
        numMoves++;
        moves.setText("Moves: "+ numMoves+" of "+ Math.round(totalMoves));
        nextMove.setText("Move from "+A.getID()+ " to "+ C.getID());
    }
    public void createGUI(){
        JFrame f=new JFrame("Tower of Hanoi");
        Font fnt=new Font("Arial", Font.BOLD, 24);
        moves.setFont(fnt);
        nextMove.setFont(fnt);
        moves.setBackground(Color.YELLOW);
        nextMove.setBackground(Color.YELLOW);
        commentary.setLayout(new GridLayout(1,2,5,5));
        f.setLayout(new GridLayout(4,1, 10, 10));
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(A.getPanel());
        f.add(B.getPanel());
        f.add(C.getPanel());
        commentary.add(moves);
        commentary.add(nextMove);
        f.add(commentary);
        f.setPreferredSize(new Dimension(600,200));
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);
    }
}
