数据结构

package com.struc;

import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.*;
import java.awt.Graphics;

public class Hanoi extends JFrame {

private static final long serialVersionUID = 1L;

static int n = Integer.parseInt(JOptionPane
.showInputDialog("输入盘子数目 int:"));

static int time = Integer.parseInt(JOptionPane
.showInputDialog("输入移动盘子的频率 int:"));
static int num = 0;

static char[] array = new char[n];

MyPanel panel = new MyPanel();
static String string = "";
static JTextArea text = new JTextArea();

public Hanoi() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(new JScrollPane(text), BorderLayout.EAST);
getContentPane().add(panel, BorderLayout.CENTER);
}

public static void main(String[] args) {
for (int i = 0; i < n; i++) {
array[i] = 'A';
}
Hanoi hanoi = new Hanoi();
hanoi.setTitle("HanoiZiJiZuo Tower");
hanoi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
hanoi.setSize(1450, 400);
hanoi.setVisible(true);
hanoi.moveDisks(n, 'A', 'C', 'B');
}

public class MyPanel extends JPanel {

public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
g.drawLine(50, 310, 1100, 310);
g.fillRect(200, 10, 10, 300);
g.drawString("A", 203, 340);
g.fillRect(500, 10, 10, 300);
g.drawString("B", 503, 340);
g.fillRect(800, 10, 10, 300);
g.drawString("C", 803, 340);
draw(g);
repaint();
}

void draw(Graphics g) {
g.setColor(Color.YELLOW);
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1.0)] == 'A') {
g.fillOval(205 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);

g.setColor(Color.BLACK);
g.drawString("" + i, 203, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;
for (int i = n; i > 0; i--) {

if (array[(int) (i - 1)] == 'B') {
g.fillOval(505 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 503, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}

}
num = 0;

for (int i = n; i > 0; i--) {
if (array[(int) (i - 1)] == 'C') {
g.fillOval(805 - (int) ((i - 1 + 1.0) / n * 150),
290 - 20 * num,
2 * (int) ((i - 1 + 1.0) / n * 150), 20);
g.setColor(Color.BLACK);
g.drawString("" + i, 803, 304 - 20 * num);
g.setColor(Color.YELLOW);
num++;
}
}
g.setColor(Color.BLACK);
}

}

public void moveDisks(int a, char fromTower, char toTower, char auxTower) {
// Stopping condition
if (a == 1) {
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + " .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
} else {
moveDisks(a - 1, fromTower, auxTower, toTower);
System.out.println("Move disk " + a + " from " + fromTower + " to "
+ toTower);
string += "Move disk " + a + " from " + fromTower + " to "
+ toTower + " .\n";
text.setText(string);
array[a - 1] = toTower;
pause();
moveDisks(a - 1, auxTower, toTower, fromTower);

}

}

public static void pause() {
try {
Thread.sleep(time * 100);// 可以修改此值加快盘子移动的速度
} catch (InterruptedException e) {
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值