Question: Task 2: Create a single person version of the Towers of Hanoi game using Stacks (Stack class provided below) Read the introduction to the game

Task 2: Create a single person version of the Towers of Hanoi game using Stacks (Stack class provided below)

Read the introduction to the game at https://en.wikipedia.org/wiki/Tower_of_Hanoi.

Create a main program that uses stacks to allows the user to interactively play the game.

It needs to determine the number of disks to use in the game.

It needs to provide a way for the user to indicate the tower to move from and to.

It needs to prevent illegal moves as defined in the Wikipedia rules.

It needs to provide the user with a way to see their progress on the screen.

Test your program to ensure it is working correctly. This should test both expected success conditions and expected error conditions.

Stack class from Task 1:

#include

#include

using namespace std;

template

//stack class

class Stack

{

//declaraing data variable

public:

T **st;//stack variable

int max_size;//to store the max size of stack

int top;//which points to current element in stack

int current;//variable to store current size

//constructor

Stack(int size)

{

//initializing variable

max_size =size;

//creating array

st = new T *[max_size];

top =-1;

current = 0;

}

//method to add to stack

void push(T *a)

{

if(top+1 < max_size)//checking overflow

{

st[top+1] = a;//assigning pointer

top++;

current++;

}

}

//method to remove element from stack

T* pop()

{

if(current !=0)//checking underflow

{

current--;

top--;

return st[top+1];//returning pointer without freeing space

}

}

//method to return length of stack

int length()

{

return current;

}

//method to clean the stack, deletes all elements of the stack

void empty()

{

while(top>-1)

{

delete st[top];

top--;

current--;

}

}

};

int main()

{

//testing stack class

Stack *s = new Stack(3);//creating with size 3

//pushing to stack

int a=5,b=6,c=7,d=8;

int *p ,*q,*x,*y;

s->push(&a);

s->push(&b);

s->push(&c);

s->push(&d);

cout<<"Length before poping:"<length()<

p=s->pop();

cout<<"Length after first poping:"<length()<

cout<<"Element poped: "<<*p<

q=s->pop();

cout<<"Length after second poping:"<length()<

cout<<"Element poped: "<<*q<

x=s->pop();

cout<<"Length after third poping:"<length()<

cout<<"Element poped: "<<*x<

y=s->pop();

s->empty();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!