X Incomplete project
#include<bits/stdc++.h>
#include<conio.h>
using namespace std;
#define ll long long int
stack<ll>peg1,peg2,peg3;
void display();
bool isvalid(ll x,ll y)
{
if(x==y||x<1||x>3||y<1||y>3)return false;
if(x==1)
{
if(peg1.empty())return false;
if(y==2)
{
if(peg2.empty())
{
if(peg1.top()>peg2.top())return false;
}
}
else
{
if(!peg3.empty())
{
if(peg1.top()>peg3.top())return false;
}
}
}
if(x==2)
{
if(peg2.empty())return false;
if(y==1)
{
if(!peg1.empty())
{
if(peg2.top()>peg1.top())return false;
}
}
else
{
if(!peg3.empty())
{
if(peg2.top()>peg3.top())return false;
}
}
}
else
{
if(peg3.empty())return false;
if(y==1)
{
if(!peg1.empty())
{
if(peg3.top()>peg1.top())return false;
}
}
else
{
if(!peg2.empty())
{
if(peg3.top()>peg2.top())return false;
}
}
}
return true;
}
void logic()
{
cout<<"top of the first peg ";
if(!peg1.empty())cout<<peg1.top()<<endl;
else cout<<"Zero\n\n";
cout<<"top of the second peg ";
if(!peg2.empty())cout<<peg2.top()<<endl;
else cout<<"Zero\n\n";
cout<<"top of the third peg ";
if(!peg3.empty())cout<<peg3.top()<<endl;
else cout<<"Zero\n\n";
ll from,to;
cout<<"Enter your Moving \n";
cout<<"FROM ";
cin>>from;
cout<<"TO ";
cin>>to;
if(isvalid(from,to))
{
if(from==1)
{
ll r;
r=peg1.top();
peg1.pop();
if(to==2)
{
peg2.push(r);
}
else
{
peg3.push(r);
}
}
else if(from==2)
{
ll r;
r=peg2.top();
peg2.pop();
if(to==1)
{
peg1.push(r);
}
else
{
peg3.push(r);
}
}
else if(from==3)
{
ll r;
r=peg3.top();
peg3.pop();
if(to==2)
{
peg2.push(r);
}
else
{
peg1.push(r);
}
}
display();
}
else
{
cout<<"Wrong choose.\n";
cout<<"press any key to try again ";
getch();
logic();
}
}
void display()
{
cout<<"IN First peg\t";
if(peg1.empty())cout<<" 0 disk\n";
else
{
vector<ll>v1;
while(!peg1.empty())
{
ll r;
r=peg1.top();
peg1.pop();
v1.push_back(r);
}
cout<<"total disk "<<v1.size()<<endl;
cout<<"Disk are : \n";
for(ll i=0;i<v1.size();i++)
{
cout<<v1[i]<<" ";
peg1.push(v1[i]);
}
cout<<endl;
}
cout<<"\n\nIN second peg\t";
if(peg2.empty())cout<<"0 disk\n";
else
{
vector<ll>v1;
while(!peg2.empty())
{
ll r;
r=peg2.top();
peg2.pop();
v1.push_back(r);
}
cout<<"total disk "<<v1.size()<<endl;
cout<<"Disk are : \n";
for(ll i=0;i<v1.size();i++)
{
cout<<v1[i]<<" ";
peg2.push(v1[i]);
}
cout<<endl;
}
cout<<"\n\nIN third peg\t";
if(peg3.empty())cout<<"0 disk\t";
else
{
vector<ll>v1;
while(!peg3.empty())
{
ll r;
r=peg3.top();
peg3.pop();
v1.push_back(r);
}
cout<<"total disk "<<v1.size()<<endl;
cout<<"Disk are : \n";
for(ll i=0;i<v1.size();i++)
{
cout<<v1[i]<<" ";
peg3.push(v1[i]);
}
cout<<endl;
}
cout<<endl;
getch();
logic();
}
void play()
{
ll disk;
cout<<"Enter Disk Number :";
cin>>disk;
for(ll i=1;i<=disk;i++)
{
peg1.push(i);
}
//cout<<peg1.size()<<endl;
display();
}
void rule()
{
cout<<"You have three peg and all disk were in first peg your task is to transform all disk in third peg with help of second peg\n";
cout<<"You can not transform more than one disk in one move.and can not keep bigger disk over smaller\n";
}
int main()
{
system("cls\n");
cout<<"enter your choose \n";
cout<<"1.play\n";
cout<<"2.rule\n";
ll choose ;
cin>>choose;
if(choose==1)
{
play();
}
else if(choose==2)
{
rule();
}
else cout<<"Invalid\n";
return 0;
}
0 Comments