Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To make an interacting user game on *Tower of hanoi* .

To make an interacting user game on *Tower of hanoi* .

Scheduled Pinned Locked Moved C / C++ / MFC
questiongame-devhelp
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.

    /****************************************** TOWER OF HANOI **********************************************************/
    #include
    #include
    #include

    int find(int name);
    int check(int temp[], int i);
    void del(int name);
    int repetetion(int temp[] , int name);
    int indexing(int temp[]);

    int m[3], r[3], l[3];
    int mp=0, rp=0, lp=3 ; //indicates the latest position occuipied

    int main()
    {
    int i=0;
    int temp=1, name;
    char position, answer;

    //initializing to 0
    while(i < 4)
    {
    	l\[i\] = 4-i;
    	m\[i\] = 0;
    	r\[i\] = 0;
    }
    
    printf("\\nWelcome to the towers of hanoi. . . \\n\\n");
    
    //looping until the left most tower have all the blocks in decending order.
    while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y')
    {
    	//taking name of the block to move.
    	printf("\\nEnter the block name:\\n");
    	scanf("%d", &name);
    	find(name);
    
    	//looping until correct block is moved.
    	while(find(name) == 1){
    		printf("This block cannot be moved! Please chose again. . .\\n");
    		scanf("%d", &name);
    		find(name);
    	}
    
    	//taking the position of the tower to move the block in.
    	printf("\\n\\nEnter the position you want to move it in..\\n");
    	scanf("%1s", &position);
    	while(temp=1)
    	{
    		if(toupper(position)=='M'){
    			if(find(name)==0 && (temp=repetetion(m, name))==0){		//if the block is movable and if the block is moved to different tower than the one it is in.
    				del(name);				//del the block from that tower
    				m\[mp\] = name;			//move that block to the prescripted one
    				mp++;					//filling the block-space of that tower, hence 4-mp more blocks can be inserted.
    				continue;
    			}
    		}
    		else if(toupper(position) == 'L'){
    			if(find(name)==0 && (temp =repetetion(l, name))==0){
    				del(name);
    				l\[lp\] = name;
    				lp++;
    				continue;
    			}
    		}
    		else if(toupper(position) == 'R'){
    			if(find(name)==0 && (temp = repetetion(r, name))==0){
    				del(name);
    				r\[rp\] = name;
    				rp++;
    				continue;
    			}
    		}
    
    		printf("Invalid entry! Please enter again. . .");		//if the desired position is already occupied
    		scanf("%1s", &position);
    	}
    
    	if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1)
    	{
    		printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . .");
    		printf("\\n\\nWant to play again? (Y/N)	");
    		scanf("%1s", &answ
    
    J L D 3 Replies Last reply
    0
    • T Tarun Jha

      I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.

      /****************************************** TOWER OF HANOI **********************************************************/
      #include
      #include
      #include

      int find(int name);
      int check(int temp[], int i);
      void del(int name);
      int repetetion(int temp[] , int name);
      int indexing(int temp[]);

      int m[3], r[3], l[3];
      int mp=0, rp=0, lp=3 ; //indicates the latest position occuipied

      int main()
      {
      int i=0;
      int temp=1, name;
      char position, answer;

      //initializing to 0
      while(i < 4)
      {
      	l\[i\] = 4-i;
      	m\[i\] = 0;
      	r\[i\] = 0;
      }
      
      printf("\\nWelcome to the towers of hanoi. . . \\n\\n");
      
      //looping until the left most tower have all the blocks in decending order.
      while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y')
      {
      	//taking name of the block to move.
      	printf("\\nEnter the block name:\\n");
      	scanf("%d", &name);
      	find(name);
      
      	//looping until correct block is moved.
      	while(find(name) == 1){
      		printf("This block cannot be moved! Please chose again. . .\\n");
      		scanf("%d", &name);
      		find(name);
      	}
      
      	//taking the position of the tower to move the block in.
      	printf("\\n\\nEnter the position you want to move it in..\\n");
      	scanf("%1s", &position);
      	while(temp=1)
      	{
      		if(toupper(position)=='M'){
      			if(find(name)==0 && (temp=repetetion(m, name))==0){		//if the block is movable and if the block is moved to different tower than the one it is in.
      				del(name);				//del the block from that tower
      				m\[mp\] = name;			//move that block to the prescripted one
      				mp++;					//filling the block-space of that tower, hence 4-mp more blocks can be inserted.
      				continue;
      			}
      		}
      		else if(toupper(position) == 'L'){
      			if(find(name)==0 && (temp =repetetion(l, name))==0){
      				del(name);
      				l\[lp\] = name;
      				lp++;
      				continue;
      			}
      		}
      		else if(toupper(position) == 'R'){
      			if(find(name)==0 && (temp = repetetion(r, name))==0){
      				del(name);
      				r\[rp\] = name;
      				rp++;
      				continue;
      			}
      		}
      
      		printf("Invalid entry! Please enter again. . .");		//if the desired position is already occupied
      		scanf("%1s", &position);
      	}
      
      	if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1)
      	{
      		printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . .");
      		printf("\\n\\nWant to play again? (Y/N)	");
      		scanf("%1s", &answ
      
      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      If you got compiler errors and warnings, inspect them or add them to your question so that we know them. They contain the line number where the error occured. Inspect that line and the previous one(s) (some errors are sourced on previous lines and detected later). Read the error message. If you do not understand them, do some web research first. However, there is one big beginner's mistake in your code: The size of the arrays and the access to the array elements. With C/C++, arrays are accessed by zero based indexes. So the allowed indexes for a size of three are 0, 1, and 2. But your code uses also the index 3. As a result, your code will never work as expected or even crash. So change the array sizes to

      int m[4], r[4], l[4];

      before handling any other errors.

      T 1 Reply Last reply
      0
      • J Jochen Arndt

        If you got compiler errors and warnings, inspect them or add them to your question so that we know them. They contain the line number where the error occured. Inspect that line and the previous one(s) (some errors are sourced on previous lines and detected later). Read the error message. If you do not understand them, do some web research first. However, there is one big beginner's mistake in your code: The size of the arrays and the access to the array elements. With C/C++, arrays are accessed by zero based indexes. So the allowed indexes for a size of three are 0, 1, and 2. But your code uses also the index 3. As a result, your code will never work as expected or even crash. So change the array sizes to

        int m[4], r[4], l[4];

        before handling any other errors.

        T Offline
        T Offline
        Tarun Jha
        wrote on last edited by
        #3

        the compiler just keeps on running , i.e. just the cursor appears and nothing else

        J 1 Reply Last reply
        0
        • T Tarun Jha

          I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.

          /****************************************** TOWER OF HANOI **********************************************************/
          #include
          #include
          #include

          int find(int name);
          int check(int temp[], int i);
          void del(int name);
          int repetetion(int temp[] , int name);
          int indexing(int temp[]);

          int m[3], r[3], l[3];
          int mp=0, rp=0, lp=3 ; //indicates the latest position occuipied

          int main()
          {
          int i=0;
          int temp=1, name;
          char position, answer;

          //initializing to 0
          while(i < 4)
          {
          	l\[i\] = 4-i;
          	m\[i\] = 0;
          	r\[i\] = 0;
          }
          
          printf("\\nWelcome to the towers of hanoi. . . \\n\\n");
          
          //looping until the left most tower have all the blocks in decending order.
          while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y')
          {
          	//taking name of the block to move.
          	printf("\\nEnter the block name:\\n");
          	scanf("%d", &name);
          	find(name);
          
          	//looping until correct block is moved.
          	while(find(name) == 1){
          		printf("This block cannot be moved! Please chose again. . .\\n");
          		scanf("%d", &name);
          		find(name);
          	}
          
          	//taking the position of the tower to move the block in.
          	printf("\\n\\nEnter the position you want to move it in..\\n");
          	scanf("%1s", &position);
          	while(temp=1)
          	{
          		if(toupper(position)=='M'){
          			if(find(name)==0 && (temp=repetetion(m, name))==0){		//if the block is movable and if the block is moved to different tower than the one it is in.
          				del(name);				//del the block from that tower
          				m\[mp\] = name;			//move that block to the prescripted one
          				mp++;					//filling the block-space of that tower, hence 4-mp more blocks can be inserted.
          				continue;
          			}
          		}
          		else if(toupper(position) == 'L'){
          			if(find(name)==0 && (temp =repetetion(l, name))==0){
          				del(name);
          				l\[lp\] = name;
          				lp++;
          				continue;
          			}
          		}
          		else if(toupper(position) == 'R'){
          			if(find(name)==0 && (temp = repetetion(r, name))==0){
          				del(name);
          				r\[rp\] = name;
          				rp++;
          				continue;
          			}
          		}
          
          		printf("Invalid entry! Please enter again. . .");		//if the desired position is already occupied
          		scanf("%1s", &position);
          	}
          
          	if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1)
          	{
          		printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . .");
          		printf("\\n\\nWant to play again? (Y/N)	");
          		scanf("%1s", &answ
          
          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          while(i < 4)
          {
          l[i] = 4-i;
          m[i] = 0;
          r[i] = 0;
          }

          You never increment variable i so this loop will run for ever.

          T 1 Reply Last reply
          0
          • T Tarun Jha

            the compiler just keeps on running , i.e. just the cursor appears and nothing else

            J Offline
            J Offline
            Jochen Arndt
            wrote on last edited by
            #5

            That is not the compiler, but your program when executed after compiling. The solution to that problem is provided below by Richard. But my answer is still valid and important. And there might be more errors.

            1 Reply Last reply
            0
            • L Lost User

              while(i < 4)
              {
              l[i] = 4-i;
              m[i] = 0;
              r[i] = 0;
              }

              You never increment variable i so this loop will run for ever.

              T Offline
              T Offline
              Tarun Jha
              wrote on last edited by
              #6

              thank you

              1 Reply Last reply
              0
              • T Tarun Jha

                I have tried this the below code but the compiler is showing logic error. I am unable to find my error. And please tell me what is the right logic.

                /****************************************** TOWER OF HANOI **********************************************************/
                #include
                #include
                #include

                int find(int name);
                int check(int temp[], int i);
                void del(int name);
                int repetetion(int temp[] , int name);
                int indexing(int temp[]);

                int m[3], r[3], l[3];
                int mp=0, rp=0, lp=3 ; //indicates the latest position occuipied

                int main()
                {
                int i=0;
                int temp=1, name;
                char position, answer;

                //initializing to 0
                while(i < 4)
                {
                	l\[i\] = 4-i;
                	m\[i\] = 0;
                	r\[i\] = 0;
                }
                
                printf("\\nWelcome to the towers of hanoi. . . \\n\\n");
                
                //looping until the left most tower have all the blocks in decending order.
                while((r\[0\] != 4 && r\[1\] != 3 && r\[2\] != 2 && r\[3\] != 1) && toupper(answer) == 'Y')
                {
                	//taking name of the block to move.
                	printf("\\nEnter the block name:\\n");
                	scanf("%d", &name);
                	find(name);
                
                	//looping until correct block is moved.
                	while(find(name) == 1){
                		printf("This block cannot be moved! Please chose again. . .\\n");
                		scanf("%d", &name);
                		find(name);
                	}
                
                	//taking the position of the tower to move the block in.
                	printf("\\n\\nEnter the position you want to move it in..\\n");
                	scanf("%1s", &position);
                	while(temp=1)
                	{
                		if(toupper(position)=='M'){
                			if(find(name)==0 && (temp=repetetion(m, name))==0){		//if the block is movable and if the block is moved to different tower than the one it is in.
                				del(name);				//del the block from that tower
                				m\[mp\] = name;			//move that block to the prescripted one
                				mp++;					//filling the block-space of that tower, hence 4-mp more blocks can be inserted.
                				continue;
                			}
                		}
                		else if(toupper(position) == 'L'){
                			if(find(name)==0 && (temp =repetetion(l, name))==0){
                				del(name);
                				l\[lp\] = name;
                				lp++;
                				continue;
                			}
                		}
                		else if(toupper(position) == 'R'){
                			if(find(name)==0 && (temp = repetetion(r, name))==0){
                				del(name);
                				r\[rp\] = name;
                				rp++;
                				continue;
                			}
                		}
                
                		printf("Invalid entry! Please enter again. . .");		//if the desired position is already occupied
                		scanf("%1s", &position);
                	}
                
                	if( r\[0\] == 4 && r\[1\] == 3 && r\[2\] == 2 && r\[3\] == 1)
                	{
                		printf("\\n\\nYou have solved the HANOI TOWER PUZZLE. . .");
                		printf("\\n\\nWant to play again? (Y/N)	");
                		scanf("%1s", &answ
                
                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Tarun Jha wrote:

                ...the compiler is showing logic error.

                Highly unlikely. If anything, it is showing you syntax errors (e.g., endless loop).

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                L 1 Reply Last reply
                0
                • D David Crow

                  Tarun Jha wrote:

                  ...the compiler is showing logic error.

                  Highly unlikely. If anything, it is showing you syntax errors (e.g., endless loop).

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  OP doesn't understand what is happening. The code compiles OK, it's the execution that fails.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups