Skip to content

Commit 52ab5fb

Browse files
authored
Restricted tower of hanoi implementation with GUI (TheAlgorithms#561)
1 parent 23c12f7 commit 52ab5fb

File tree

7 files changed

+239
-0
lines changed

7 files changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
import java.util.*;
2+
import javax.swing.*;
3+
import javax.swing.Timer;
4+
import java.awt.*;
5+
import java.awt.event.*;
6+
7+
public class Hanoi extends JFrame{
8+
9+
public static int ONE_SECOND = 1000;
10+
11+
int number_of_disks=0;
12+
int game_counter = 0;
13+
int i=0;
14+
15+
/* GUI COMPONENTS */
16+
public JButton move_button = new JButton();
17+
public JButton exit_button = new JButton();
18+
public JButton replay_button = new JButton();
19+
public JButton auto_button = new JButton();
20+
21+
22+
/* BACKEND COMPONENTS */
23+
public ArrayList<String> movements = new ArrayList<String>();
24+
public StringBuilder stringBuilder = new StringBuilder();
25+
26+
public ArrayList<Integer> Stack1 = new ArrayList<Integer>();
27+
public ArrayList<Integer> Stack2 = new ArrayList<Integer>();
28+
public ArrayList<Integer> Stack3 = new ArrayList<Integer>();
29+
30+
public void updateStacks() {
31+
if(game_counter!=movements.size()) {
32+
String temp = movements.get(game_counter);
33+
System.out.println(temp);
34+
if(temp.charAt(1)=='A') {
35+
if(temp.charAt(2)=='B') {
36+
int x = Stack1.get(Stack1.size()-1);
37+
Stack1.remove(Stack1.size()-1);
38+
Stack2.add(x);
39+
}
40+
}
41+
if(temp.charAt(1)=='C') {
42+
if(temp.charAt(2)=='B') {
43+
int x = Stack3.get(Stack3.size()-1);
44+
Stack3.remove(Stack3.size()-1);
45+
Stack2.add(x);
46+
}
47+
}
48+
49+
if(temp.charAt(1)=='B') {
50+
if(temp.charAt(2)=='C') {
51+
int x = Stack2.get(Stack2.size()-1);
52+
Stack2.remove(Stack2.size()-1);
53+
Stack3.add(x);
54+
}
55+
else if(temp.charAt(2)=='A') {
56+
int x = Stack2.get(Stack2.size()-1);
57+
Stack2.remove(Stack2.size()-1);
58+
Stack1.add(x);
59+
}
60+
}
61+
revalidate();
62+
repaint();
63+
game_counter++;
64+
}
65+
}
66+
67+
public void paint(Graphics canvas) {
68+
super.paint(canvas);
69+
70+
//Drawing pedestels
71+
for(int i=0;i<3;i++) {
72+
canvas.drawRect(30+i*230,670,200,20);
73+
canvas.setColor(new Color(76,174,227)); //Blue Accent
74+
canvas.fillRect(30+i*230,670,200,20);
75+
76+
canvas.fillRect(130+i*230-2,670-170,4,170);
77+
canvas.setColor(new Color(150,0,0)); //Arseny
78+
canvas.fillRect(130+i*230-2,670-170,4,170);
79+
}
80+
81+
//Disks in stack1
82+
for(int i=1;i<=Stack1.size();i++) {
83+
canvas.drawRect(130-Stack1.get(i-1)*10,670-i*12,Stack1.get(i-1)*20,10);
84+
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
85+
canvas.fillRect(130-Stack1.get(i-1)*10,670-i*12,Stack1.get(i-1)*20,10);
86+
}
87+
88+
//Disks in stack2
89+
for(int i=1;i<=Stack2.size();i++) {
90+
canvas.drawRect(360-Stack2.get(i-1)*10,670-i*12,Stack2.get(i-1)*20,10);
91+
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
92+
canvas.fillRect(360-Stack2.get(i-1)*10,670-i*12,Stack2.get(i-1)*20,10);
93+
}
94+
95+
//Disks in stack3
96+
for(int i=1;i<=Stack3.size();i++) {
97+
canvas.drawRect(590-Stack3.get(i-1)*10,670-i*12,Stack3.get(i-1)*20,10);
98+
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
99+
canvas.fillRect(590-Stack3.get(i-1)*10,670-i*12,Stack3.get(i-1)*20,10);
100+
}
101+
}
102+
103+
// Function to initialize the widget properties and the frame.
104+
public void initialize() {
105+
106+
move_button.setIcon(new ImageIcon("../Resources/rsz_move.png"));
107+
move_button.setBounds(130,0,50,50);
108+
109+
auto_button.setIcon(new ImageIcon("../Resources/rsz_loop.png"));
110+
auto_button.setBounds(260,0,50,50);
111+
112+
replay_button.setIcon(new ImageIcon("../Resources/rsz_replay.jpg"));
113+
replay_button.setBounds(390,0,50,50);
114+
115+
exit_button.setIcon(new ImageIcon("../Resources/rsz_exit.png"));
116+
exit_button.setBounds(520,0,50,50);
117+
118+
add(move_button);
119+
add(exit_button);
120+
add(replay_button);
121+
add(auto_button);
122+
123+
setLayout(null);
124+
setSize(720,720);
125+
setVisible(true);
126+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
127+
}
128+
// Main cnstructor.
129+
Hanoi() {
130+
super("restricted tower of hanoi");
131+
initialize();
132+
133+
//MOVE BUTTON ACTION LISTENER
134+
move_button.addActionListener(new ActionListener() {
135+
@Override
136+
public void actionPerformed(ActionEvent e) {
137+
updateStacks();
138+
}
139+
});
140+
141+
//EXIT BUTTON ACTION LISTENER
142+
exit_button.addActionListener(new ActionListener() {
143+
@Override
144+
public void actionPerformed(ActionEvent e) {
145+
System.exit(0);
146+
}
147+
});
148+
149+
//REPLAY BUTTON ACTION LISTENER
150+
replay_button.addActionListener(new ActionListener() {
151+
@Override
152+
public void actionPerformed(ActionEvent e) {
153+
startGame();
154+
repaint();
155+
}
156+
});
157+
158+
//AUTOMATIC PLAY BUTTON ACTION LISTENER
159+
auto_button.addActionListener(new ActionListener() {
160+
@Override
161+
public void actionPerformed(ActionEvent e) {
162+
timer.start();
163+
if(game_counter == movements.size()) {
164+
timer.stop();
165+
}
166+
}
167+
});
168+
}
169+
170+
Timer timer = new Timer(ONE_SECOND,new ActionListener() {
171+
public void actionPerformed(ActionEvent e) {
172+
updateStacks();
173+
}
174+
});
175+
176+
public void startGame() {
177+
178+
System.out.println("New Game Started");
179+
timer.stop();
180+
181+
Stack1 = new ArrayList<Integer>();
182+
Stack2 = new ArrayList<Integer>();
183+
Stack3 = new ArrayList<Integer>();
184+
185+
movements = new ArrayList<String>();
186+
game_counter = 0;
187+
188+
for(int i=0;i<number_of_disks;i++) {
189+
Stack1.add(number_of_disks-i);
190+
}
191+
192+
towerOfHanoi(number_of_disks,'A','C','B');
193+
}
194+
195+
public static void main(String args[]) {
196+
Hanoi tower = new Hanoi();
197+
int number = Integer.parseInt(args[0]);
198+
tower.number_of_disks = number;
199+
tower.startGame();
200+
/*for(int i=0;i<tower.movements.size();i++) {
201+
System.out.println(tower.movements.get(i));
202+
//System.out.println(tower.Stack1.get(i));
203+
}*/
204+
}
205+
206+
//Recursive function to formulate restricted tower of hanoi.
207+
public void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
208+
if (n == 1) {
209+
stringBuilder.setLength(0);
210+
stringBuilder.append(Integer.toString(n));
211+
stringBuilder.append(from_rod);
212+
stringBuilder.append(aux_rod);
213+
movements.add(stringBuilder.toString());
214+
//System.out.println("Move disk 1 from rod " + from_rod + " to rod " + aux_rod);
215+
stringBuilder.setLength(0);
216+
stringBuilder.append(Integer.toString(n));
217+
stringBuilder.append(aux_rod);
218+
stringBuilder.append(to_rod);
219+
movements.add(stringBuilder.toString());
220+
//System.out.println("Move disk 1 from rod " + aux_rod + " to rod " + to_rod);
221+
return;
222+
}
223+
towerOfHanoi(n-1, from_rod, to_rod, aux_rod);
224+
stringBuilder.setLength(0);
225+
stringBuilder.append(Integer.toString(n));
226+
stringBuilder.append(from_rod);
227+
stringBuilder.append(aux_rod);
228+
movements.add(stringBuilder.toString());
229+
//System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + aux_rod);
230+
towerOfHanoi(n-1, to_rod, from_rod, aux_rod);
231+
stringBuilder.setLength(0);
232+
stringBuilder.append(Integer.toString(n));
233+
stringBuilder.append(aux_rod);
234+
stringBuilder.append(to_rod);
235+
movements.add(stringBuilder.toString());
236+
//System.out.println("Move disk " + n + " from rod " + aux_rod + " to rod " + to_rod);
237+
towerOfHanoi(n-1, from_rod, to_rod, aux_rod);
238+
}
239+
}
Loading
Loading
1017 Bytes
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)