You are on page 1of 1

/**

* Author - Marcus Grant


* Date - October 7, 2014
* Class - ITCS 2215
*/
import java.util.*;
public class ProgramHanoiTower {
static Stack<Integer> peg1 = new Stack<Integer>();
static Stack<Integer> peg2 = new Stack<Integer>();
static Stack<Integer> peg3 = new Stack<Integer>();
public static void main(String[] args) {
int disksNum;
Scanner input = new Scanner(System.in);
System.out.println("Program demonstrates the
Towers of Hanoi puzzle");
System.out.println("Smaller numbers indicate
smaller disk");
System.out.print("Enter the number of disk
(1-8): ");
disksNum = input.nextInt();
System.out.println("----------------------------------------------");
System.out.println("STARTING POSITION");
for (int i = disksNum; i > 0; i--) {
peg1.push(i);
}
display();
System.out.println();
hanoiPuzzle(disksNum, peg1, peg2, peg3);
input.close();
}
public static void hanoiPuzzle(int disk, Stack<Integer> a,
Stack<Integer> b, Stack<Integer> c) {
if (disk > 0) {
hanoiPuzzle(disk - 1, a, c, b);
int d = a.pop();
c.push(d);
display();
hanoiPuzzle(disk - 1, b, a, c);
}
}
public static void display() {
System.out.println("Peg 1: " + peg1);
System.out.println("Peg 2: " + peg2);
System.out.println("Peg 3: " + peg3);
System.out.println("--------------");
}
}

You might also like