Jump to content

tower of Hanoi game by Stack &C++

ebdaa3sea
       // From the software distribution accompanying the textbook         // "A Practical Introduction to Data Structures and Algorithm Analysis,         // Third Edition (C++)" by Clifford A. Shaffer.         // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.         #ifndef  LINK_H         #define  LINK_H         #include<iostream>         using namespace std;         // Singly linked list node         template <typename E> class Link         {         public:          E element;      // Value for this node        Link *next;        // Pointer to next node in list          // Constructors          Link(const E& elemval, Link* nextval = NULL) {                   element = elemval;  next = nextval;          }          Link(Link* nextval = NULL) { next = nextval; }         };         #endif                  #ifndef  STACK_H         #define  STACK_H         // Stack abtract class         template <typename E> class Stack {         private:          void operator =(const Stack&) {}     // Protect assignment          Stack(const Stack&) {}         public:          Stack() {}          virtual ~Stack() {}          // Protect copy constructor          // Default constructor          // Base destructor          // Reinitialize the stack.  The user is responsible for          // reclaiming the storage used by the stack elements.          virtual void clear() = 0;          // Push an element onto the top of the stack.          // it: The element being pushed onto the stack.          virtual void push(const E& it) = 0;          // Remove the element at the top of the stack.          // Return: The element at the top of the stack.          virtual E pop() = 0;          // Return: A copy of the top element.          virtual const E& topValue() const = 0;          // Return: The number of elements in the stack.          virtual int length() const = 0;         };         #endif                  #ifndef  LSTACK_H         #define  LSTACK_H         #include "Stack.h"         #include "Link.h"                           // Linked stack implementation         template <typename E> class LStack : public Stack<E> {         private:          Link<E>* top;            // Pointer to first element          int size;                   // Number of elements         public:          LStack(int sz = 3) // Constructor          {          top = NULL; size = 0;          }          ~LStack() { clear(); }          void clear() {         while (top != NULL) {          // Destructor         // Reinitialize          // Delete link nodes          Link<E>* temp = top;          top = top->next;          delete temp;         }         size = 0;         }         void push(const E& it) { // Put "it" on stack         top = new Link<E>(it, top);         size++;         }         E pop() {                // Remove "it" from stack         if (top == NULL){ cout << "Stack is empty"; exit(1); }         E it = top->element;         Link<E>* ltemp = top->next;         delete top;        top = ltemp;        size--;        return it;         }         const E& topValue() const { // Return top value         if (top == 0){ cout << "Stack is empty"; exit(1); }         return top->element;         }                int length() const { return size; } // Return length       };       #endif        //This my Stack ADT        //This is My Code Depends on Stack         #include<iostream>        #include"LStack.h"        using namespace std;                        void hanoi(int des,LStack<int>&ob1,LStack<int>&ob2,LStack<int>&ob3){           if(des>0)          {ob3.push(ob1.pop());}           else          { hanoi(des-1,ob1,ob2,ob3);           ob3.push(ob1.pop());           hanoi(des-1,ob1,ob2,ob3);           }                         }                 int main(){           int s=4;           LStack <int> b1,b2,b3;           for(int i=0;i<s;i++)          { b1.push(i);}          hanoi(s,b1,b2,b3);                 system("pause");          return 0;         }

my Qeustion

1- how i can output this code as graphics?

2- if correct how i can connected with SFML ??

3- how can i move disk ??

4- what i need to complete the code if it is not completed

 
Link to comment
Share on other sites

Link to post
Share on other sites

You should know if your code is correct or not yourself. If it does what you want it to do, then it's correct. If it doesn't, it's not correct. If it's not correct, then you need to start debugging the code to see where the problems are.

Link to comment
Share on other sites

Link to post
Share on other sites

       // From the software distribution accompanying the textbook         // "A Practical Introduction to Data Structures and Algorithm Analysis,         // Third Edition (C++)" by Clifford A. Shaffer.         // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer.

This is not even your code, you ripped this right out of a textbook. We are not going to write a program for you, simple as that as you have not done your due diligence of even attempting to write a graphics renderer for the program. You have there a command line based example of the towers of hanoi, it is your job to write the accompanying windows GUI based classes associated with displaying the stacks in the program, look up examples with windows forms if you want to make a basic GUI with button interaction. If you have an exact question we can help you with it but understand that we are not going to make it for you.

 

It seems from your questions you have not even done the job of searching how to do this as a few days ago you were asking for how to write the code for the game and I gave a high level overview of how to construct it from the point of data structures.

Link to comment
Share on other sites

Link to post
Share on other sites

This is not even your code, you ripped this right out of a textbook. We are not going to write a program for you, simple as that as you have not done your due diligence of even attempting to write a graphics renderer for the program. You have there a command line based example of the towers of hanoi, it is your job to write the accompanying windows GUI based classes associated with displaying the stacks in the program, look up examples with windows forms if you want to make a basic GUI with button interaction. If you have an exact question we can help you with it but understand that we are not going to make it for you.

 

It seems from your questions you have not even done the job of searching how to do this as a few days ago you were asking for how to write the code for the game and I gave a high level overview of how to construct it from the point of data structures.

The ADT not from me i know i ask about the code depends  on this ADT 

Link to comment
Share on other sites

Link to post
Share on other sites

The ADT not from me i know i ask about the code depends  on this ADT 

 

Yes the Hanoi game requires the use of a Stack, from the code it looks like you are declaring your own Stack class instead of using a standard #include <stack>  

as in your code you have  

         #ifndef  LSTACK_H         #define  LSTACK_H         #include "Stack.h"         #include "Link.h"

In this you are defining your stack class to be used instead of using those in the standard libraries.

 

So you should know if you are using your own stack class correctly  :huh:

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×