Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could you please guys help me with this C++ problme? In this homework you will m

ID: 3850654 • Letter: C

Question

Could you please guys help me with this C++ problme?

In this homework you will make use of arrays and control flow structures to solve the classic game, Towers of Hanoi. This is a mathematical game that consists of three stacks and a number of disks. The game starts with the disks placed in the leftmost stack. The disks are placed in ascending order, this is, the smallest disk at the top. The goal of this game is to move the entire leftmost stack to the rightmost stack. You must adhere to the following game rules: Only one disk can be moved at a time. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack. Only the uppermost disks can be moved No disk may be placed on top of a smaller disk. Binary Representation This problem can be solved in a number of ways. We are going to solve it through a simple binary representation. These are the rules for the model: There is one b t for each disk. e The most significant bit represents the largest disk. e The least significant bit represents the smallest disk. Start reading the bits from the right. The first bit to have a value of 1 is the source disk (or the disk to be moved). The second bit to have a value of 1 is the destin ation disk. If the number of zeroes in between the first and second bit is even (or zero) then move the source disk on top of the destination disk. f the number of zeroes in between the first and second bit is odd then move the source disk on top of the stack not containing the destination disk. f there is on one bit with the value of 1 move the disk to any stack you see fit In the end, all the disks from the leftmost stack must be placed in the rightmost stack. Let's assume we have only three disks (D3, D2, D1), then we would have something like this:

Explanation / Answer

The required code is as follows for the game:

TowOFH.cpp

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;

// Declaring structures for better use of them in the program.
struct branch
{
int use;
branch *nxt;
}*tp = NULL, *pro = NULL, *nxtptr1 = NULL;
struct branch2
{
int use2;
branch2 *next2;
}*tp1 = NULL, *pro1 = NULL, *nxtptr = NULL;
struct branch3
{
int use2;
branch3 *next3;
}*tp2 = NULL, *pro2 = NULL, *nxtptr2 = NULL;

// used to push Data further into the stack
void pushData(int inputs)
{
nxtptr1 = new branch;
nxtptr1->use = inputs;
nxtptr1->next1 = NULL;
if (tp == NULL)
{
tp = nxtptr1;
}
else
{
nxtptr1->next1 = tp;
tp = nxtptr1;
}
}

// Delete the data that has been done working.
int del()
{
int value = 999;
if (tp == NULL)
{
return value;
}
else
{
pro = tp;
tp = tp->next1;
return(pro->use);
delete(pro);
}
}

//Push the data further in the stack.
void pushData1(int inputs)
{
nxtptr = new branch2;
nxtptr->use2 = inputs;
nxtptr->next2 = NULL;
if (tp1 == NULL)
{
tp1 = nxtptr;
}
else
{
nxtptr->next2 = tp1;
tp1 = nxtptr;
}
}

// Delete the data that has been done working.

int del1()

{
int value = 999;
if (tp1 == NULL)
{
return value;
}
else
{
pro1 = tp1;
tp1 = tp1->next2;
return(pro1->use2);
delete(pro1);
}
}

// Push the data further in the nodes.
void pushData2(int inputs)
{
nxtptr2 = new branch3;
nxtptr2->use2 = inputs;
nxtptr2->next3 = NULL;
if (tp2 == NULL)
{
tp2 = nxtptr2;
}
else
{
nxtptr2->next3 = tp2;
tp2 = nxtptr2;
}
}

// Delete the data that has been done working.
int del2()
{
int value = 999;
if (tp2 == NULL)
{
return value;
}
else
{
pro2 = tp2;
tp2 = tp2->next3;
return(pro2->use2);
delete(pro2);
}
}

//To declare the things that are on the top of the stack
int TOS()
{
if (tp != NULL && tp->use == 1 )
{
return 1;
}
else if (tp1 != NULL && tp1->use2 == 1)
{
return 2;
}
else if (tp2 != NULL && tp2->use2 == 1)
{
return 3;
}
}

// Display the things that are happening at the back.
void dispData()
{
cout<<endl;
branch *pro;
pro = tp;
cout<<"The Tower 1-> "<<"t";
while (pro != NULL)
{
cout<<pro->use<<"t";
pro = pro->next1;
}
cout<<endl;
}
void dispData1()
{
branch2 *pro1;
pro1 = tp1;
cout<<"The Tower 2-> "<<"t";
while (pro1 != NULL)
{
cout<<pro1->use2<<"t";
pro1 = pro1->next2;
}
cout<<endl;
}
void dispData2()
{
branch3 *pro2;
pro2 = tp2;
cout<<"The Tower 3-> "<<"t";
while (pro2 != NULL)
{
cout<<pro2->use2<<"t";
pro2 = pro2->next3;
}
cout<<endl;
cout<<endl;
}

// Main calling of the program to get the code done.
void topOfAll(int no)
{
int uu, pp, qq, value;
for (uu = 0; uu < (pow(2,no)); uu++)
{
dispData();
dispData1();
dispData2();
pp = TOS();
if (uu % 2 == 0)
{
if (pp == 1)
{
pushData2(del());
}
else if (pp == 2)
{
pushData(del1());
}
else if (pp == 3)
{
pushData1(del2());
}
}
else
{
if (pp == 1)
{
qq = del1();
value = del2();
if (qq < value && value != 999)
{
pushData2(value);
pushData2(qq);
}
else if (qq > value && qq != 999)
{
pushData1(qq);
pushData1(value);
}
else if (value == 999)
{
pushData2(qq);
}
else if (qq == 999)
{
pushData1(value);
}
}
else if (pp == 2)
{
qq = del();
value = del2();
if (qq < value && value != 999)
{
pushData2(value);
pushData2(qq);
}
else if (qq > value && qq != 999)
{
pushData(qq);
pushData(value);
}
else if (value == 999)
{
pushData2(qq);
}
else if (qq == 999)
{
pushData(value);
}
}
else if (pp == 3)
{
qq = del();
value = del1();
if (qq < value && value != 999)
{
pushData1(value);
pushData1(qq);
}
else if (qq > value && qq != 999)
{
pushData(qq);
pushData(value);
}
else if (value == 999)
{
pushData1(qq);
}
else if (qq == 999)
{
pushData(value);
}
}
}
}
}

// Diriver or Tester program to check the running of the code.
int main()
{
int no, uu;
cout<<"Enter number of disks in the game: ";
cin>>no;
for (uu = no; uu >= 1; uu--)
{
pushData(uu);
}
topOfAll(no);
return 0;
}

Please rate the answer if it helped.....Thankyou

Hope it helps.....