mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
Formatted with Google Java Formatter
This commit is contained in:
@ -1,239 +1,244 @@
|
|||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.Timer;
|
import javax.swing.Timer;
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.event.*;
|
|
||||||
|
|
||||||
public class Hanoi extends JFrame{
|
public class Hanoi extends JFrame {
|
||||||
|
|
||||||
public static int ONE_SECOND = 1000;
|
public static int ONE_SECOND = 1000;
|
||||||
|
|
||||||
int number_of_disks=0;
|
int number_of_disks = 0;
|
||||||
int game_counter = 0;
|
int game_counter = 0;
|
||||||
int i=0;
|
int i = 0;
|
||||||
|
|
||||||
/* GUI COMPONENTS */
|
/* GUI COMPONENTS */
|
||||||
public JButton move_button = new JButton();
|
public JButton move_button = new JButton();
|
||||||
public JButton exit_button = new JButton();
|
public JButton exit_button = new JButton();
|
||||||
public JButton replay_button = new JButton();
|
public JButton replay_button = new JButton();
|
||||||
public JButton auto_button = new JButton();
|
public JButton auto_button = new JButton();
|
||||||
|
|
||||||
|
/* BACKEND COMPONENTS */
|
||||||
|
public ArrayList<String> movements = new ArrayList<String>();
|
||||||
|
public StringBuilder stringBuilder = new StringBuilder();
|
||||||
|
|
||||||
/* BACKEND COMPONENTS */
|
public ArrayList<Integer> Stack1 = new ArrayList<Integer>();
|
||||||
public ArrayList<String> movements = new ArrayList<String>();
|
public ArrayList<Integer> Stack2 = new ArrayList<Integer>();
|
||||||
public StringBuilder stringBuilder = new StringBuilder();
|
public ArrayList<Integer> Stack3 = new ArrayList<Integer>();
|
||||||
|
|
||||||
public ArrayList<Integer> Stack1 = new ArrayList<Integer>();
|
public void updateStacks() {
|
||||||
public ArrayList<Integer> Stack2 = new ArrayList<Integer>();
|
if (game_counter != movements.size()) {
|
||||||
public ArrayList<Integer> Stack3 = new ArrayList<Integer>();
|
String temp = movements.get(game_counter);
|
||||||
|
System.out.println(temp);
|
||||||
public void updateStacks() {
|
if (temp.charAt(1) == 'A') {
|
||||||
if(game_counter!=movements.size()) {
|
if (temp.charAt(2) == 'B') {
|
||||||
String temp = movements.get(game_counter);
|
int x = Stack1.get(Stack1.size() - 1);
|
||||||
System.out.println(temp);
|
Stack1.remove(Stack1.size() - 1);
|
||||||
if(temp.charAt(1)=='A') {
|
Stack2.add(x);
|
||||||
if(temp.charAt(2)=='B') {
|
|
||||||
int x = Stack1.get(Stack1.size()-1);
|
|
||||||
Stack1.remove(Stack1.size()-1);
|
|
||||||
Stack2.add(x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(temp.charAt(1)=='C') {
|
|
||||||
if(temp.charAt(2)=='B') {
|
|
||||||
int x = Stack3.get(Stack3.size()-1);
|
|
||||||
Stack3.remove(Stack3.size()-1);
|
|
||||||
Stack2.add(x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(temp.charAt(1)=='B') {
|
|
||||||
if(temp.charAt(2)=='C') {
|
|
||||||
int x = Stack2.get(Stack2.size()-1);
|
|
||||||
Stack2.remove(Stack2.size()-1);
|
|
||||||
Stack3.add(x);
|
|
||||||
}
|
|
||||||
else if(temp.charAt(2)=='A') {
|
|
||||||
int x = Stack2.get(Stack2.size()-1);
|
|
||||||
Stack2.remove(Stack2.size()-1);
|
|
||||||
Stack1.add(x);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
revalidate();
|
|
||||||
repaint();
|
|
||||||
game_counter++;
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (temp.charAt(1) == 'C') {
|
||||||
|
if (temp.charAt(2) == 'B') {
|
||||||
|
int x = Stack3.get(Stack3.size() - 1);
|
||||||
|
Stack3.remove(Stack3.size() - 1);
|
||||||
|
Stack2.add(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (temp.charAt(1) == 'B') {
|
||||||
|
if (temp.charAt(2) == 'C') {
|
||||||
|
int x = Stack2.get(Stack2.size() - 1);
|
||||||
|
Stack2.remove(Stack2.size() - 1);
|
||||||
|
Stack3.add(x);
|
||||||
|
} else if (temp.charAt(2) == 'A') {
|
||||||
|
int x = Stack2.get(Stack2.size() - 1);
|
||||||
|
Stack2.remove(Stack2.size() - 1);
|
||||||
|
Stack1.add(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
revalidate();
|
||||||
|
repaint();
|
||||||
|
game_counter++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void paint(Graphics canvas) {
|
||||||
|
super.paint(canvas);
|
||||||
|
|
||||||
|
// Drawing pedestels
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
canvas.drawRect(30 + i * 230, 670, 200, 20);
|
||||||
|
canvas.setColor(new Color(76, 174, 227)); // Blue Accent
|
||||||
|
canvas.fillRect(30 + i * 230, 670, 200, 20);
|
||||||
|
|
||||||
|
canvas.fillRect(130 + i * 230 - 2, 670 - 170, 4, 170);
|
||||||
|
canvas.setColor(new Color(150, 0, 0)); // Arseny
|
||||||
|
canvas.fillRect(130 + i * 230 - 2, 670 - 170, 4, 170);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(Graphics canvas) {
|
// Disks in stack1
|
||||||
super.paint(canvas);
|
for (int i = 1; i <= Stack1.size(); i++) {
|
||||||
|
canvas.drawRect(130 - Stack1.get(i - 1) * 10, 670 - i * 12, Stack1.get(i - 1) * 20, 10);
|
||||||
//Drawing pedestels
|
canvas.setColor(new Color(64, 26, 0)); // Brown Wolfers
|
||||||
for(int i=0;i<3;i++) {
|
canvas.fillRect(130 - Stack1.get(i - 1) * 10, 670 - i * 12, Stack1.get(i - 1) * 20, 10);
|
||||||
canvas.drawRect(30+i*230,670,200,20);
|
|
||||||
canvas.setColor(new Color(76,174,227)); //Blue Accent
|
|
||||||
canvas.fillRect(30+i*230,670,200,20);
|
|
||||||
|
|
||||||
canvas.fillRect(130+i*230-2,670-170,4,170);
|
|
||||||
canvas.setColor(new Color(150,0,0)); //Arseny
|
|
||||||
canvas.fillRect(130+i*230-2,670-170,4,170);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Disks in stack1
|
|
||||||
for(int i=1;i<=Stack1.size();i++) {
|
|
||||||
canvas.drawRect(130-Stack1.get(i-1)*10,670-i*12,Stack1.get(i-1)*20,10);
|
|
||||||
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
|
|
||||||
canvas.fillRect(130-Stack1.get(i-1)*10,670-i*12,Stack1.get(i-1)*20,10);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Disks in stack2
|
|
||||||
for(int i=1;i<=Stack2.size();i++) {
|
|
||||||
canvas.drawRect(360-Stack2.get(i-1)*10,670-i*12,Stack2.get(i-1)*20,10);
|
|
||||||
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
|
|
||||||
canvas.fillRect(360-Stack2.get(i-1)*10,670-i*12,Stack2.get(i-1)*20,10);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Disks in stack3
|
|
||||||
for(int i=1;i<=Stack3.size();i++) {
|
|
||||||
canvas.drawRect(590-Stack3.get(i-1)*10,670-i*12,Stack3.get(i-1)*20,10);
|
|
||||||
canvas.setColor(new Color(64,26,0)); //Brown Wolfers
|
|
||||||
canvas.fillRect(590-Stack3.get(i-1)*10,670-i*12,Stack3.get(i-1)*20,10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to initialize the widget properties and the frame.
|
// Disks in stack2
|
||||||
public void initialize() {
|
for (int i = 1; i <= Stack2.size(); i++) {
|
||||||
|
canvas.drawRect(360 - Stack2.get(i - 1) * 10, 670 - i * 12, Stack2.get(i - 1) * 20, 10);
|
||||||
move_button.setIcon(new ImageIcon("../Resources/rsz_move.png"));
|
canvas.setColor(new Color(64, 26, 0)); // Brown Wolfers
|
||||||
move_button.setBounds(130,0,50,50);
|
canvas.fillRect(360 - Stack2.get(i - 1) * 10, 670 - i * 12, Stack2.get(i - 1) * 20, 10);
|
||||||
|
|
||||||
auto_button.setIcon(new ImageIcon("../Resources/rsz_loop.png"));
|
|
||||||
auto_button.setBounds(260,0,50,50);
|
|
||||||
|
|
||||||
replay_button.setIcon(new ImageIcon("../Resources/rsz_replay.jpg"));
|
|
||||||
replay_button.setBounds(390,0,50,50);
|
|
||||||
|
|
||||||
exit_button.setIcon(new ImageIcon("../Resources/rsz_exit.png"));
|
|
||||||
exit_button.setBounds(520,0,50,50);
|
|
||||||
|
|
||||||
add(move_button);
|
|
||||||
add(exit_button);
|
|
||||||
add(replay_button);
|
|
||||||
add(auto_button);
|
|
||||||
|
|
||||||
setLayout(null);
|
|
||||||
setSize(720,720);
|
|
||||||
setVisible(true);
|
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|
||||||
}
|
|
||||||
// Main cnstructor.
|
|
||||||
Hanoi() {
|
|
||||||
super("restricted tower of hanoi");
|
|
||||||
initialize();
|
|
||||||
|
|
||||||
//MOVE BUTTON ACTION LISTENER
|
|
||||||
move_button.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
updateStacks();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//EXIT BUTTON ACTION LISTENER
|
|
||||||
exit_button.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
System.exit(0);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//REPLAY BUTTON ACTION LISTENER
|
|
||||||
replay_button.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
startGame();
|
|
||||||
repaint();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//AUTOMATIC PLAY BUTTON ACTION LISTENER
|
|
||||||
auto_button.addActionListener(new ActionListener() {
|
|
||||||
@Override
|
|
||||||
public void actionPerformed(ActionEvent e) {
|
|
||||||
timer.start();
|
|
||||||
if(game_counter == movements.size()) {
|
|
||||||
timer.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Timer timer = new Timer(ONE_SECOND,new ActionListener() {
|
// Disks in stack3
|
||||||
public void actionPerformed(ActionEvent e) {
|
for (int i = 1; i <= Stack3.size(); i++) {
|
||||||
|
canvas.drawRect(590 - Stack3.get(i - 1) * 10, 670 - i * 12, Stack3.get(i - 1) * 20, 10);
|
||||||
|
canvas.setColor(new Color(64, 26, 0)); // Brown Wolfers
|
||||||
|
canvas.fillRect(590 - Stack3.get(i - 1) * 10, 670 - i * 12, Stack3.get(i - 1) * 20, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Function to initialize the widget properties and the frame.
|
||||||
|
public void initialize() {
|
||||||
|
|
||||||
|
move_button.setIcon(new ImageIcon("../Resources/rsz_move.png"));
|
||||||
|
move_button.setBounds(130, 0, 50, 50);
|
||||||
|
|
||||||
|
auto_button.setIcon(new ImageIcon("../Resources/rsz_loop.png"));
|
||||||
|
auto_button.setBounds(260, 0, 50, 50);
|
||||||
|
|
||||||
|
replay_button.setIcon(new ImageIcon("../Resources/rsz_replay.jpg"));
|
||||||
|
replay_button.setBounds(390, 0, 50, 50);
|
||||||
|
|
||||||
|
exit_button.setIcon(new ImageIcon("../Resources/rsz_exit.png"));
|
||||||
|
exit_button.setBounds(520, 0, 50, 50);
|
||||||
|
|
||||||
|
add(move_button);
|
||||||
|
add(exit_button);
|
||||||
|
add(replay_button);
|
||||||
|
add(auto_button);
|
||||||
|
|
||||||
|
setLayout(null);
|
||||||
|
setSize(720, 720);
|
||||||
|
setVisible(true);
|
||||||
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
}
|
||||||
|
// Main cnstructor.
|
||||||
|
Hanoi() {
|
||||||
|
super("restricted tower of hanoi");
|
||||||
|
initialize();
|
||||||
|
|
||||||
|
// MOVE BUTTON ACTION LISTENER
|
||||||
|
move_button.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
updateStacks();
|
updateStacks();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
public void startGame() {
|
// EXIT BUTTON ACTION LISTENER
|
||||||
|
exit_button.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
System.out.println("New Game Started");
|
// REPLAY BUTTON ACTION LISTENER
|
||||||
timer.stop();
|
replay_button.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
Stack1 = new ArrayList<Integer>();
|
@Override
|
||||||
Stack2 = new ArrayList<Integer>();
|
public void actionPerformed(ActionEvent e) {
|
||||||
Stack3 = new ArrayList<Integer>();
|
startGame();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
movements = new ArrayList<String>();
|
// AUTOMATIC PLAY BUTTON ACTION LISTENER
|
||||||
game_counter = 0;
|
auto_button.addActionListener(
|
||||||
|
new ActionListener() {
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
timer.start();
|
||||||
|
if (game_counter == movements.size()) {
|
||||||
|
timer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for(int i=0;i<number_of_disks;i++) {
|
Timer timer =
|
||||||
Stack1.add(number_of_disks-i);
|
new Timer(
|
||||||
}
|
ONE_SECOND,
|
||||||
|
new ActionListener() {
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
updateStacks();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
towerOfHanoi(number_of_disks,'A','C','B');
|
public void startGame() {
|
||||||
|
|
||||||
|
System.out.println("New Game Started");
|
||||||
|
timer.stop();
|
||||||
|
|
||||||
|
Stack1 = new ArrayList<Integer>();
|
||||||
|
Stack2 = new ArrayList<Integer>();
|
||||||
|
Stack3 = new ArrayList<Integer>();
|
||||||
|
|
||||||
|
movements = new ArrayList<String>();
|
||||||
|
game_counter = 0;
|
||||||
|
|
||||||
|
for (int i = 0; i < number_of_disks; i++) {
|
||||||
|
Stack1.add(number_of_disks - i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String args[]) {
|
towerOfHanoi(number_of_disks, 'A', 'C', 'B');
|
||||||
Hanoi tower = new Hanoi();
|
}
|
||||||
int number = Integer.parseInt(args[0]);
|
|
||||||
tower.number_of_disks = number;
|
|
||||||
tower.startGame();
|
|
||||||
/*for(int i=0;i<tower.movements.size();i++) {
|
|
||||||
System.out.println(tower.movements.get(i));
|
|
||||||
//System.out.println(tower.Stack1.get(i));
|
|
||||||
}*/
|
|
||||||
}
|
|
||||||
|
|
||||||
//Recursive function to formulate restricted tower of hanoi.
|
public static void main(String args[]) {
|
||||||
public void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
|
Hanoi tower = new Hanoi();
|
||||||
if (n == 1) {
|
int number = Integer.parseInt(args[0]);
|
||||||
stringBuilder.setLength(0);
|
tower.number_of_disks = number;
|
||||||
stringBuilder.append(Integer.toString(n));
|
tower.startGame();
|
||||||
stringBuilder.append(from_rod);
|
/*for(int i=0;i<tower.movements.size();i++) {
|
||||||
stringBuilder.append(aux_rod);
|
System.out.println(tower.movements.get(i));
|
||||||
movements.add(stringBuilder.toString());
|
//System.out.println(tower.Stack1.get(i));
|
||||||
//System.out.println("Move disk 1 from rod " + from_rod + " to rod " + aux_rod);
|
}*/
|
||||||
stringBuilder.setLength(0);
|
}
|
||||||
stringBuilder.append(Integer.toString(n));
|
|
||||||
stringBuilder.append(aux_rod);
|
// Recursive function to formulate restricted tower of hanoi.
|
||||||
stringBuilder.append(to_rod);
|
public void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) {
|
||||||
movements.add(stringBuilder.toString());
|
if (n == 1) {
|
||||||
//System.out.println("Move disk 1 from rod " + aux_rod + " to rod " + to_rod);
|
stringBuilder.setLength(0);
|
||||||
return;
|
stringBuilder.append(Integer.toString(n));
|
||||||
}
|
stringBuilder.append(from_rod);
|
||||||
towerOfHanoi(n-1, from_rod, to_rod, aux_rod);
|
stringBuilder.append(aux_rod);
|
||||||
stringBuilder.setLength(0);
|
movements.add(stringBuilder.toString());
|
||||||
stringBuilder.append(Integer.toString(n));
|
// System.out.println("Move disk 1 from rod " + from_rod + " to rod " + aux_rod);
|
||||||
stringBuilder.append(from_rod);
|
stringBuilder.setLength(0);
|
||||||
stringBuilder.append(aux_rod);
|
stringBuilder.append(Integer.toString(n));
|
||||||
movements.add(stringBuilder.toString());
|
stringBuilder.append(aux_rod);
|
||||||
//System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + aux_rod);
|
stringBuilder.append(to_rod);
|
||||||
towerOfHanoi(n-1, to_rod, from_rod, aux_rod);
|
movements.add(stringBuilder.toString());
|
||||||
stringBuilder.setLength(0);
|
// System.out.println("Move disk 1 from rod " + aux_rod + " to rod " + to_rod);
|
||||||
stringBuilder.append(Integer.toString(n));
|
return;
|
||||||
stringBuilder.append(aux_rod);
|
|
||||||
stringBuilder.append(to_rod);
|
|
||||||
movements.add(stringBuilder.toString());
|
|
||||||
//System.out.println("Move disk " + n + " from rod " + aux_rod + " to rod " + to_rod);
|
|
||||||
towerOfHanoi(n-1, from_rod, to_rod, aux_rod);
|
|
||||||
}
|
}
|
||||||
|
towerOfHanoi(n - 1, from_rod, to_rod, aux_rod);
|
||||||
|
stringBuilder.setLength(0);
|
||||||
|
stringBuilder.append(Integer.toString(n));
|
||||||
|
stringBuilder.append(from_rod);
|
||||||
|
stringBuilder.append(aux_rod);
|
||||||
|
movements.add(stringBuilder.toString());
|
||||||
|
// System.out.println("Move disk " + n + " from rod " + from_rod + " to rod " + aux_rod);
|
||||||
|
towerOfHanoi(n - 1, to_rod, from_rod, aux_rod);
|
||||||
|
stringBuilder.setLength(0);
|
||||||
|
stringBuilder.append(Integer.toString(n));
|
||||||
|
stringBuilder.append(aux_rod);
|
||||||
|
stringBuilder.append(to_rod);
|
||||||
|
movements.add(stringBuilder.toString());
|
||||||
|
// System.out.println("Move disk " + n + " from rod " + aux_rod + " to rod " + to_rod);
|
||||||
|
towerOfHanoi(n - 1, from_rod, to_rod, aux_rod);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user