/*
Tower of Hanoi
*/
import java.util.*;
class gameexp extends Exception
{
private int exec;
gameexp (int a)
{
exec=a;
}
public String toString()
{
if(exec==1)
return"Disk Heavier";
else if(exec==2)
return"No Element Available";
else if(exec==3)
return "Invalid Disk";
else if(exec==4)
return "Congrats!! you have successfully completed the game";
else if(exec==5)
return "Sorrry Try Again !!!!Game Over!! !";
return " ";
}
}
class toh
{
Scanner s=new Scanner(System.in);
int first[]=new int[10];
int second[]=new int[10];
int third[]=new int[10];
String f1[]=new String[10];String f2[]=new String[10];String f3[]=new String[10];
int top1,top2,top3;
int count,n1,n2,n3;
//count=(int)(Math.pow(2,num)-1);
String disc(int n,int num)
{
String dis="";
for(int i=1;i<(n*2);i++)
{
dis="";
for(int k=n;k<num;k++)
dis+=" ";
for(int j=1;j<=i;j++)
dis+="=";
i++;
}
return dis;
}
/*
=
===
=====
*/
void intial(int num)
{
int n;
for(int i=0;i<num;i++)
{
first[i]=i+1;
second[i]=0;
third[i]=0;
}
top1=0;
top2=num;
top3=num;
n1=num;
n2=0;
n3=0;
count=(int)(Math.pow(2,num)-1);
}
void display(int num)
{
//System.out.print("\n top1 ="+top1+" \t top2 ="+top2+"\ttop3="+top3);
System.out.print("\n MOVES LEFT :"+count);
System.out.print("\n ------------------------------------------------------------------------------");
System.out.print("\n\n\tTower 1\t\tTower 2\t\tTower 3");
for(int i=0;i<num;i++)
{
if(first[i]==0)
f1[i]=" ";
else f1[i]=disc(first[i],num);//Integer.toString(first[i]);
if(second[i]==0)
f2[i]=" ";
else f2[i]=disc(second[i],num);//Integer.toString(second[i]);
if(third[i]==0)
f3[i]=" ";
else f3[i]=disc(third[i],num);//Integer.toString(third[i]);
}
for(int i=0;i<num;i++)
{
System.out.print("\n\t"+f1[i]+"\t\t"+f2[i]+"\t\t"+f3[i]);
}
System.out.print("\n ------------------------------------------------------------------------------");
}
void transfer(int num) throws gameexp
{
System.out.print("\n SET YOUR MOVES:\n" );
int ch,ch1,ch2,f=1;
int check=0;
while(count!=0)
{
try{
System.out.print("\n ---> Move From(which tower) :");
ch1=s.nextInt();
System.out.print("\n ---> Move To (which tower) :");
ch2=s.nextInt();
if(ch1==1&&ch2==2)
ch=1;
else if(ch1==1&&ch2==3)
ch=2;
else if(ch1==2&&ch2==1)
ch=3;
else if(ch1==2&&ch2==3)
ch=4;
else if(ch1==3&&ch2==1)
ch=5;
else if(ch1==3&&ch2==2)
ch=6;
else throw new gameexp(3);
switch(ch)
{
case 1:
//
// System.out.print("\n top1 ="+top1+" \t top2 ="+top2+"\tt1="+first[top1]+"\tt2="+second[top2]);
if(n1!=0){
if(first[top1]<second[top2]||second[top2]==0)
{
top2--;
second[top2]=first[top1];
first[top1]=0;
top1++;n1--;n2++;count--;
display(num);
}
else{
throw new gameexp(1);
}}
else
throw new gameexp(2);
break;
case 2:
if(n1!=0){
if(first[top1]<third[top3]||third[top3]==0)
{
top3--;
third[top3]=first[top1];
first[top1]=0;
top1++;n1--;n3++;
count--;
display(num);
}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 3:
if(n2!=0){
if(second[top2]<first[top1]||first[top1]==0)
{
top1--;
first[top1]=second[top2];
second[top2]=0;
top2++;n2--;n1++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 4:
if(n2!=0){
if(second[top2]<third[top3]||third[top3]==0)
{top3--;
third[top3]=second[top2];
second[top2]=0;
top2++;n2--;n3++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 5:
if(n3!=0){
if(third[top3]<first[top1]||first[top1]==0)
{top1--;
first[top1]=third[top3];
third[top3]=0;
top3++;n3--;n1++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
case 6:
if(n3!=0){
if(third[top3]<second[top2]||second[top2]==0)
{top2--;
second[top2]=third[top3];
third[top3]=0;
top3++;n3--;n2++;
count--;
display(num);}
else{
throw new gameexp(1);
}}
else throw new gameexp(2);
break;
}
}
catch(gameexp e)
{
System.out.println("\nERROR :"+e);
}
}
if(count==0)
for(int i=0;i<num;i++)
if(third[i]==i+1)
check++;
if(count==0&&check==num)
throw new gameexp(4);//won
else
throw new gameexp(5);//fail
}
void rules()
{
System.out.print("\n RULES \n1.You cannot place a larger disk onto a smaller disk \n2.The final completed tower should be on tower 3 \n3.you only get (2^n)-1 moves to finish the game successfully");
}
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
toh t=new toh();
int n,f=1,k;
System.out.print("\n Do you wish to see the rules? \n1.yes \n2.no \nEnter your choice: ");
k=s.nextInt();
if(k==1)
t.rules();
while(f==1)
{
System.out.print("\n\n");
System.out.print("\n Welcome to the Game of tower of Hanoi");
System.out.print("\n Num of Disc =");
n=s.nextInt();
t.intial(n);
t.display(n);
try{
t.transfer(n);
}catch(gameexp e)
{
System.out.print("\n MOVES OVER :"+e);
}
System.out.print("\n Press 1 to continue playing or any other key to exit from the game: ");
f=s.nextInt();
}
}
}
No comments:
Post a Comment