日期:2014-05-20  浏览次数:20805 次

一个汉诺塔自动演示的问题~
想实现一个3阶汉诺塔的自动演示,主要是想看到每次移动的过程,但是用Timer或者Thread.sleep都弄不好```
求大侠帮忙看看应该怎么弄~
Java code

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class HanoiDemo extends JFrame {
    private panel p = new panel();
    
    public HanoiDemo() {
        add(p);
    }
    
    public void doDemo() {
        p.moveDisks(3, 'A', 'B', 'C');
    }
    
    private class panel extends JPanel {
        private int xA;
        private int xB;
        private int xC;
        
        private int numbersOfA = 3;        //柱子盘子的数目
        private int numbersOfB = 0;
        private int numbersOfC = 0;
        
        private int[] disksOfA = new int[]{3, 2, 1};   //柱子上放盘子情况
        private int[] disksOfB = new int[]{0, 0, 0};
        private int[] disksOfC = new int[]{0, 0, 0};    
        

        
        private Timer timer = new Timer(100, new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent arg0) {
                // TODO Auto-generated method stub
                repaint();
            }
        });
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            
            xA = getWidth() / 4;
            xB = getWidth() / 2;
            xC = getWidth() / 4 * 3;
            g.drawRect(xA - 10, getHeight() / 4, 20, getHeight() / 4 * 3);
            g.drawRect(xB - 10, getHeight() / 4, 20, getHeight() / 4 * 3);
            g.drawRect(xC - 10, getHeight() / 4, 20, getHeight() / 4 * 3);
            
    
            if (numbersOfA != 0) {
                for (int i = 0; i < 3; i++) {
                    if (disksOfA[i] != 0)
                        g.fillRect(xA - 10 - disksOfA[i] * 10, getHeight() - 20 - i * 20, 20 + disksOfA[i] * 20, 20);
                }
            }
            
            if (numbersOfB != 0) {
                for (int i = 0; i < 3; i++) {
                    if (disksOfB[i] != 0)
                        g.fillRect(xB - 10 - disksOfB[i] * 10, getHeight() - 20 - i * 20, 20 + disksOfB[i] * 20, 20);
                }
            }
            
            if (numbersOfC != 0) {
                for (int i = 0; i < 3; i++) {
                    if (disksOfC[i] != 0)
                        g.fillRect(xC - 10 - disksOfC[i] * 10, getHeight() - 20 - i * 20, 20 + disksOfC[i] * 20, 20);
                }
            }

        }
        
        public void moveDisks(int n, char fromTower, char toTower, char auxTower) {
                                try {            
                    Thread.sleep(500);
                                        repaint();    
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }     //本来想用sleep来看每次移动的过程,但动作一卡一卡的
                if (n == 1){ // Stopping condition
                    System.out.println("Move disk " + n + " from " +
                            fromTower + " to " + toTower);
                    if (fromTower == 'A') {
                        if (toTower == 'B') {
                            move(disksOfA, disksOfB);
                            numbersOfA--;
                            numbersOfB++;
                        }
                        else {
                            move(disksOfA, disksOfC);
                            numbersOfA--;
                            numbersOfC++;
                        }
                    }     
                    if (fromTower == 'B') {
                        if (toTower == 'A') {
                            move(disksOfB, disksOfA);
                            numbersOfB--;
                            numbersOfA++;
                        }
                        else {
                            move(disksOfB, disksOfC);
                            numbersOfB--;
                            numbersOfC++;
                        }
                    }        
                    if (fromTower == 'C') {
                        if (toTower == 'B') {
                            move(disksOfC, disksOfB);
                            numbersOfC--;
                            numbersOfB++;
                        }
                        else {
                            move(disksOfC, disksOfA);
                            numbersOfC--;
                            numbersOfA++;
                        }
                    }            
                }
                
                else {
                  moveDisks(n - 1, fromTower, auxTower, toTower);
                  System.out.println("Move disk " + n + " from " +
                    fromTower + " to " + toTower);
                  if (fromTower == 'A') {
                        if (toTower == 'B') {
                            move(disksOfA, disksOfB);
                            numbersOfA--;
                            numbersOfB++;
                        }
                        else {
                            move(disksOfA, disksOfC);
                            numbersOfA--;
                            numbersOfC++;
                        }
                    }     
                    if (fromTower == 'B') {
                        if (toTower == 'A') {
                            move(disksOfB, disksOfA);
                            numbersOfB--;
                            numbersOfA++;
                        }
                        else {
                            move(disksOfB, disksOfC);
                            numbersOfB--;
                            numbersOfC++;
                        }
                    }        
                    if (fromTower == 'C') {
                        if (toTower == 'B') {
                            move(disksOfC, disksOfB);
                            numbersOfC--;
                            numbersOfB++;
                        }
                        else {
                            move(disksOfC, disksOfA);
                            numbersOfC--;
                            numbersOfA++;
                        }
                    }            
                  moveDisks(n - 1, auxTower, toTower, fromTower);
                }
         }
        
        public void move(int[] from, int[] to) {
            int i = 2;
            while (from[i] == 0) {
                i--;
            }
            int moveDisk = from[i];
            from[i] = 0;
            i = 0;
            while (to[i] != 0) {
                i++;
            }
            to[i] = moveDisk;
        }
    
    }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HanoiDemo frame = new HanoiDemo();
        frame.setTitle("HanoiDemo");
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 300);
        frame.setVisible(true);
        frame.doDemo();
    }

}




------解决方案--------------------
Java code
        public static void move(int size,String from,String temp,String to){
        if (size == 1) {
            System.out.println("把"+size+"#从"+from+"移动到"+to);
        }else {
            move(size - 1, from, to, temp); //from-->inner
            System.out.println("把"+size+"#从"+from+"移动到"+to);
            move(size - 1, temp, from, to); //inner-->to
        }
    }