-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTowerOfHanoi.java
173 lines (139 loc) · 3.68 KB
/
TowerOfHanoi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Disk extends JPanel implements Runnable {
static final int W_WIDTH = 1400;
static final int W_HEIGHT = 500;
static final int R1 = 300;
static final int R2 = 700;
static final int R3 = 1100;
static final int RW = 10;
static final int DISK_HEIGHT = 20;
static final int DISK_DISTANCE = 10;
static final int DISK_STEP = 20;
static final int MIN_DISK_WIDTH = 30;
static final String[] ABC = {"0", "A", "B", "C"};
static int step = 0;
static int DELAY = 100;
int nDisk;
int[] disks = new int[1000];
Disk() {
initPanel();
}
void initPanel() {
setPreferredSize(new Dimension(W_WIDTH, W_HEIGHT));
setDoubleBuffered(true);
}
void drawRod(Graphics g, int x, int w, String s) {
g.fillRect(x-RW/2, 100, RW, W_HEIGHT-100);
Font font = new Font(g.getFont().getFontName(), Font.TRUETYPE_FONT, 30);
g.setFont(font);
g.drawString(s, x - 12, 100-20);
}
void drawRods(Graphics g) {
drawRod(g,R1,RW, "A");
drawRod(g,R2,RW, "B");
drawRod(g,R3,RW, "C");
}
void drawDisk(Graphics g, int r, int hth, int nth) {
int len = MIN_DISK_WIDTH + 2 * DISK_STEP * nth;
int x = r - len/2;
int y = W_HEIGHT - (hth-1)*DISK_DISTANCE - hth*DISK_HEIGHT;
g.fillRect(x, y, len, DISK_HEIGHT);
Font font = new Font(g.getFont().getFontName(), Font.TRUETYPE_FONT, 20);
g.setFont(font);
g.drawString(String.valueOf(nth), x-15, y+DISK_HEIGHT);
}
void drawDisks(Graphics g, int[] rods, int n) {
int n1, n2, n3;
n1=n2=n3=0;
for (int i=n; i>0; i--) {
if (rods[i] == 1) {
n1++;
drawDisk(g, R1, n1, i);
}
if (rods[i] == 2) {
n2++;
drawDisk(g, R2, n2, i);
}
if (rods[i] == 3) {
n3++;
drawDisk(g, R3, n3, i);
}
}
}
@Override
public void addNotify() {
super.addNotify();
Thread animator = new Thread(this);
animator.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// pain here
drawRods(g);
drawDisks(g, disks, nDisk);
}
@Override
public void run() {
System.out.print("Enter number of disks: ");
Scanner scan = new Scanner(System.in);
nDisk = scan.nextInt();
System.out.print("Enter delay time: ");
DELAY = scan.nextInt();
scan.close();
repaint();
System.out.println("Starting after 3s...");
for (int i=1; i<=nDisk; i++)
disks[i] = 1;
try {
Thread.sleep(3000);
} catch (Exception e) {
}
Hanoi(nDisk, 1,2,3);
System.out.println("Done!");
}
public void moveDisk(int n, int nguon, int dich) {
disks[n] = dich;
step++;
System.out.println("Step " + step + ". Disk " + n + " from " + ABC[nguon] + " to " + ABC[dich]);
repaint();
try {
Thread.sleep(DELAY);
} catch (Exception e) {
}
}
public void Hanoi(int n, int nguon, int tam, int dich) {
if (n==0)
return;
Hanoi(n-1, nguon, dich, tam);
moveDisk(n, nguon, dich);
Hanoi(n-1, tam, nguon, dich);
}
}
public class TowerOfHanoi extends JFrame {
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame ex = new TowerOfHanoi();
ex.setVisible(true);
}
});
}
TowerOfHanoi() {
// TODO Auto-generated constructor stub
add(new Disk());
setResizable(false);
pack();
setTitle("Tower of Hanoi");
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}