//this code is designed by codenation.
//7:42 15/12/2020
//tower of hanoi for max 4 disks.
#include<stdio.h>
#include<strings.h>
int play();
int display();
void rules();
int mvdisk();
int game_over=0;
int moves=0;
int top1=3;
int top2=-1;
int top3=-1;
char *d1[4];
char *d2[4];
char *d3[4];
int main()
{
int ch;
d1[3]= " ****";
d1[2]= " ******";
d1[1]= " ********";
d1[0]= "**********";
printf("*$*WELCOME TO THE TOWER OF HANOI*$*");
while(game_over==0)
{
printf("\n//TOWER OF HANOI MAIN MENU//");
printf("\nenter your choice :\n");
printf("1.PLAY GAME\n2.VIEW RULES\n3.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
play();
break;
case 2:
rules();
break;
case 3:
return 0;
default:
printf("ENTER THE CORRECT CHOICE\n");
}
}
printf("******************************************************************\n\n");
return 0;
}
int play()
{
int i,j,ch2;
char choice;
printf("LET'S PLAY\n");
printf("ARE YOU READY TO START?(y/n)\n");
scanf(" %c",&choice);
if(choice=='y' || choice=='Y')
{
while(game_over==0)
{
printf("\nenter your choice :");
printf("\n1.MOVE DISKS\n2.VIEW PEGS\n3.RETURN TO MAIN MENU\n");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
mvdisk();
break;
case 2:
display();
break;
case 3:
printf("\nyour game is saving.....\n");
printf("...........\n");
printf("...........\n");
printf("SAVED SUCCESSFULLY\n");
return 0;
default:
printf("NO SUCH OPTION!!!");
}
if((d3[3]==" ****") && (d3[2]==" ******") && (d3[1]==" ********") && (d3[0]=="**********"))
{
display();
printf("\n\nYOU WON THE GAME\n");
printf("$$ THANKS FOR PLAYING $$\n");
game_over=1;
}
else if(moves==15)
{
printf("\n!! SORRY YOU LOST !!\n");
printf("!YOU HAVE REACHED MAXIMUM MOVES!\n");
printf("$$ THANKS FOR PLAYING $$\n");
game_over=1;
}
}
}
return 0;
}
int mvdisk()
{
char disk;
int num1,num2;
printf("enter the peg number from which you want to move disk :\n");
scanf("%d",&num1);
printf("enter the peg number to which you want to move the disk :\n");
scanf("%d",&num2);
switch(num1)
{
case 1:
if(top1==-1)
{
printf("\nno disks in PEG 1\n");
}
else
{
switch(num2)
{
case 2:
if(top2==-1 || (strlen(d2[top2])>strlen(d1[top1])))
{
top2++;
d2[top2]=d1[top1];
top1--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
case 3:
if(top3==-1 || (strlen(d3[top3])>strlen(d1[top1])))
{
top3++;
d3[top3]=d1[top1];
top1--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
}
}
break;
case 2:
if(top2==-1)
{
printf("\nno disks in PEG 2\n");
break;
}
else
{
switch(num2)
{
case 1:
if(top1==-1 || (strlen(d1[top1])>strlen(d2[top2])))
{
top1++;
d1[top1]=d2[top2];
top2--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
case 3:
if(top3==-1 || (strlen(d3[top3])>strlen(d2[top2])))
{
top3++;
d3[top3]=d2[top2];
top2--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
}
}
break;
case 3:
if(top3==-1)
{
printf("\nno disks in PEG 3\n");
break;
}
else
{
switch(num2)
{
case 1:
if(top1==-1 || (strlen(d1[top1])>strlen(d3[top3])))
{
top1++;
d1[top1]=d3[top3];
top3--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
case 2:
if(top2==-1 || (strlen(d2[top2])>strlen(d3[top3])))
{
top2++;
d2[top2]=d3[top3];
top3--;
moves++;
}
else
{
printf("\nINVALID MOVE!\n");
}
break;
}
}
break;
default:
return 0;
}
}
int display()
{
int i,j;
printf("\nPEG 1\n");
if(top1==-1)
{
printf("THE PEG 1 IS EMPTY\n");
}
else
{
for(i=top1;i>=0;i--)
{
printf("%s\n",d1[i]);
}
}
printf("\n");
printf("\nPEG 2\n");
if(top2==-1)
{
printf("THE PEG 2 IS EMPTY\n");
}
else
{
for(i=top2;i>=0;i--)
{
printf("%s\n",d2[i]);
}
}
printf("\n");
printf("PEG 3\n");
if(top3==-1)
{
printf("THE PEG 3 IS EMPTY\n");
}
else
{
for(i=top3;i>=0;i--)
{
printf("%s\n",d3[i]);
}
}
printf("\n");
}
void rules()
{
printf("\n//THE RULES OF TOWER OF HANOI//\n");
printf("The goal of the puzzle is to move all the disks from the peg 1 to the\n peg 3,\nadhering to the following rules:\n1.Move only one disk at a time.\n2.A larger disk may not be placed on top of a smaller disk.\n3.All disks, except the one being moved, must be on a peg.\n4.you have 15 moves to solve the puzzle.\n");
printf("\n\nHOW TO PLAY\n");
printf("select 'PLAY GAME' to start.\n");
printf("select 'MOVE DISKS' to move disk from one peg to another.\n");
printf("select 'VIEW PEGS' to view the pegs.\n");
printf("if you select 'RETURN TO MAIN MENU' your current game will be saved.\n");
printf("if you select 'EXIT' from main menu your progress will be lost and you will exit from game.\n");
printf("\n\n******************************************************************\n\n");
}
//this code is developed by codenation.
Comments
Post a Comment