The objective of the assignment is to model the puzzle Tower
The objective of the assignment is to model the puzzle Towers of Hanoi using Object Oriented Programming (Java) and recursion.
There have to be 3 classes:
Disk.java
- Fields: An integer representing the size (width) of the disk
- Methods: A get and set method for the size of the disk
- Constructor: A constructor is called when creating a new instance of the Disk.
Rod.java
- Fields: A collection to store the disks
- Methods: getDisk() and addDisk() methods to return the top disk from the rod and add a disk to top of the rod. Also a method to return the contents of the rod toString().
- Constructor: Creates a rod with n number of disk
Hanoi.java
- Fields: The hanoi class should hold 3 rods
- Constructor: It creates a rod with n number of disks accepted from the user
- Methods: printStatus() to show the contents of the game status after every move. move() to move disk from one rod to another and recursiveTOH(), which will find the solution to the Towers of Hanoi puzzle recursively.
The program should start by asking the user to enter the number of disks on the source rod. For every move show the status of each of the rod and which disk were moved from one rod to another.
*Make sure this puzzle is solved with classes and interesection*
Solution
Hanoi.java
import java.io.*;
import java.lang.*;
import java.util.*;
class Hanoi
{
static int movecount = 0;
static public void recursiveTOH(Stack source, Stack temp, Stackdest)
{
temp.push(source.pop());
movecount++;
PrintStatuss();
dest.push(source.pop());
movecount++;
PrintStatuss();
dest.push(temp.pop());
movecount++;
PrintStatuss();
}
static public int move(int nDiscs, Stack source, Stack temp, Stackdest)
{
if (nDiscs <= 4)
{
if ((nDiscs % 2) == 0)
{
recursiveTOH(source, temp, dest);
nDiscs = nDiscs - 1;
if (nDiscs == 1)
return 1;
temp.push(source.pop());
movecount++;
PrintStatus();
//new source is dest, new temp is source, new dest is temp;
recursiveTOH(dest, source, temp);
dest.push(source.pop());
movecount++;
PrintStatus();
//new source is temp, new temp is source, new dest is dest;
move(nDiscs, temp, source, dest);
}
else
{
if (nDiscs == 1)
return -1;
recursiveTOH(source, dest, temp);
nDiscs = nDiscs - 1;
dest.push(source.pop());
movecount++;
PrintStatus();
recursiveTOH(temp, source, dest);
}
return 1;
}
else if (nDiscs >= 5)
{
move(nDiscs - 2, source, temp, dest);
temp.push(source.pop());
movecount++;
PrintStatus();
move(nDiscs - 2, dest, source, temp);
dest.push(source.pop());
movecount++;
PrintStatus();
move(nDiscs - 1, temp, source, dest);
}
return 1;
}
static public Stack A = new Stack();
static public Stack B = new Stack();
static public Stack C = new Stack();
static public void PrintStatus()
{
if (countA != A.size() ||
countB != B.size() ||
countC != C.size())
{
int diffA = A.size() - countA;
int diffB = B.size() - countB;
int diffC = C.size() - countC;
if (diffA == 1)
{
if (diffB == -1)
System.out.print(\"Move Disc \" + A.peek() + \" From B To A\");
else
System.out.print(\"Move Disc \" + A.peek() + \" From C To A\");
}
else if (diffB == 1)
{
if (diffA == -1)
System.out.print(\"Move Disc \" + B.peek() + \" From A To B\");
else
System.out.print(\"Move Disc \" + B.peek() + \" From C To B\");
}
else //if (diffC == 1)
{
if (diffA == -1)
System.out.print(\"Move Disc \" + C.peek() + \" From A To C\");
else
System.out.print(\"Move Disc \" + C.peek() + \" From B To C\");
}
countA = A.size();
countB = B.size();
countC = C.size();
System.out.println();
}
PrintStatus(A);
System.out.print(\" , \");
PrintStatus(B);
System.out.print(\" , \");
PrintStatus(C);
System.out.print(\" , \");
}
static int countA = 0;
static int countB = 0;
static int countC = 0;
static public void PrintStatus(Stack s)
{
System.out.print(s.toString());
}
public static void main(String[] args)
{
try
{
while (true)
{
System.out.print(\"\ Enter the number 1of discs (-1 to exit): \");
int maxdisc = 0;
String inpstring = \"\";
GivengiveninputStreamReader givengiveninput = newGivengiveninputStreamReader(System.in);
Hanoi hanoi = new hanoi(givengiveninput);
inpstring = reader.readLine();
movecount = 0;
maxdisc = Integer.parseInt(inpstring);
if (maxdisc == -1)
{
System.out.println(\"Good Bye!\");
return;
}
if (maxdisc <= 1 || maxdisc >= 10)
{
System.out.println(\"Enter between 2 - 9\");
continue;
}
for (int i = maxdisc; i >= 1; i--)
A.push(i);
countA = A.size();
countB = B.size();
countC = C.size();
PrintStatus();
move(maxdisc, A, B, C);
System.out.println(\"Total Moves = \" + movecount);
while (C.size() > 0)
C.pop();
}
}
catch (Exception e)
{
e.PrintStatusTrace();
}
}
}
Disk.java
Import java.awt.*;
public class Disk{
public void setSize(maxdisc)
{
Maxdisc=100;
}
public void getSize()
{
System.out.println(\"count of A = \" + countA);
System.out.println(\"count of B = \" + countB);
System.out.println(\"count of C = \" + countC);
}
public static void main(String[] args)
{
try
{
while (true)
{
System.out.print(\"\ Enter the number 1of discs (-1 to exit): \");
int maxdisc = 0;
String inpstring = \"\";
GivengiveninputStreamReader givengiveninput = newGivengiveninputStreamReader(System.in);
Disk disk = new disk(givengiveninput);
inpstring = reader.readLine();
movecount = 0;
maxdisc = Integer.parseInt(inpstring);
if (maxdisc == -1)
{
System.out.println(\"Good Bye!\");
return;
}
if (maxdisc <= 1 || maxdisc >= 10)
{
System.out.println(\"Enter between 2 - 9\");
continue;
}
for (int i = maxdisc; i >= 1; i--)
A.push(i);
countA = A.getSize();
countB = B.getSize();
countC = C.getSize();
PrintStatus();
move(maxdisc, A, B, C);
System.out.println(\"Total Moves = \" + movecount);
while (C.getSIZE() > 0)
C.pop();
}
}
catch (Exception e)
{
e.PrintStatusTrace();
}
}
}
rod.java
import java.io.*;
import java.lang.*;
import java.util.*;
class Rod
{
static int movecount = 0;
static public void recursiveTOH(Stack source, Stack temp, Stackdest)
{
temp.addDisk(source.pop());
movecount++;
PrintStatuss();
dest.addDisk(source.pop());
movecount++;
PrintStatuss();
dest.addDisk(temp.pop());
movecount++;
PrintStatuss();
}
static public int move(int nDiscs, Stack source, Stack temp, Stackdest)
{
if (nDiscs <= 4)
{
if ((nDiscs % 2) == 0)
{
recursiveTOH(source, temp, dest);
nDiscs = nDiscs - 1;
if (nDiscs == 1)
return 1;
temp.addDisk(source.pop());
movecount++;
PrintStatus();
//new source is dest, new temp is source, new dest is temp;
recursiveTOH(dest, source, temp);
dest.addDisk(source.pop());
movecount++;
PrintStatus();
//new source is temp, new temp is source, new dest is dest;
move(nDiscs, temp, source, dest);
}
else
{
if (nDiscs == 1)
return -1;
recursiveTOH(source, dest, temp);
nDiscs = nDiscs - 1;
dest.addDisk(source.pop());
movecount++;
PrintStatus();
recursiveTOH(temp, source, dest);
}
return 1;
}
else if (nDiscs >= 5)
{
move(nDiscs - 2, source, temp, dest);
temp.addDisk(source.pop());
movecount++;
PrintStatus();
move(nDiscs - 2, dest, source, temp);
dest.addDisk(source.pop());
movecount++;
PrintStatus();
move(nDiscs - 1, temp, source, dest);
}
return 1;
}
static public Stack A = new Stack();
static public Stack B = new Stack();
static public Stack C = new Stack();
static public void PrintStatus()
{
if (countA != A.size() ||
countB != B.size() ||
countC != C.size())
{
int diffA = A.size() - countA;
int diffB = B.size() - countB;
int diffC = C.size() - countC;
if (diffA == 1)
{
if (diffB == -1)
System.out.print(\"Move Disc \" + A.peek() + \" From B To A\");
else
System.out.print(\"Move Disc \" + A.peek() + \" From C To A\");
}
else if (diffB == 1)
{
if (diffA == -1)
System.out.print(\"Move Disc \" + B.peek() + \" From A To B\");
else
System.out.print(\"Move Disc \" + B.peek() + \" From C To B\");
}
else //if (diffC == 1)
{
if (diffA == -1)
System.out.print(\"Move Disc \" + C.peek() + \" From A To C\");
else
System.out.print(\"Move Disc \" + C.peek() + \" From B To C\");
}
countA = A.size();
countB = B.size();
countC = C.size();
System.out.println();
}
PrintStatus(A);
System.out.print(\" , \");
PrintStatus(B);
System.out.print(\" , \");
PrintStatus(C);
System.out.print(\" , \");
}
static int countA = 0;
static int countB = 0;
static int countC = 0;
static public void PrintStatus(Stack s)
{
System.out.print(s.toString());
}
}
public static void main(String[] args)
{
try
{
while (true)
{
System.out.print(\"\ Enter the number 1of discs (-1 to exit): \");
int maxdisc = 0;
String inpstring = \"\";
GivengiveninputStreamReader givengiveninput = newGivengiveninputStreamReader(System.in);
Rod rod = new rod(givengiveninput);
inpstring = reader.readLine();
movecount = 0;
maxdisc = Integer.parseInt(inpstring);
if (maxdisc == -1)
{
System.out.println(\"Good Bye!\");
return;
}
if (maxdisc <= 1 || maxdisc >= 10)
{
System.out.println(\"Enter between 2 - 9\");
continue;
}
for (int i = maxdisc; i >= 1; i--)
A.addDisk(i);
countA = A.size();
countB = B.size();
countC = C.size();
PrintStatus();
move(maxdisc, A, B, C);
System.out.println(\"Total Moves = \" + movecount);
while (C.size() > 0)
C.pop();
}
}
catch (Exception e)
{
e.PrintStatusTrace();
}
}













