hello,
pls find out why code is not work for solitaire game...
Thanks in advance.:rolleyes:
code:
#include<iostream>
using namespace std;
/*****CLASS PLAYING CARD*****/
class PlayingCard
{
private:
int rank;//integer 1-13
int suit;//integer0-3
char color;//red('r') or black('b')
public:
PlayingCard(int,int);
PlayingCard();
void display();
~PlayingCard();
const static int diamond;
const static int heart;
const static int spade;
const static int club;
};
/***** CLASS MAKING PILE OF CARDS*****/
class PileofCards
{
private:
PlayingCard *pile;//pointer to array of playing cards
int top;//last element added to array
int size;//no. of cards in pile
int position;//position of pile amongst others
public:
PileofCards(int,int);
~PileofCards();
PlayingCard Peek();
PlayingCard Remove();
void display();
void Add(PlayingCard);
bool IsEmpty();
bool IsFull();
};
/***** CLASS DECK OF CARDS*****/
class Deck
{
private:
PlayingCard *deck[52];
int size;
public:
Deck();
int getSize();
bool IsEmpty();
PlayingCard getCard(int i);
void Display();
PlayingCard removeCard(int i);
~Deck();
};
/*****CLASS SOLITIRE*****/
class Solitire{
private:
Deck deckofCards;
PileofCards shuffled;
public:
Solitire();
void shuffle();
void display();
};
Solitire::Solitire():shuffled(52,1)
{}
/*****INITIALIZING*****/
const int PlayingCard::diamond=0;
const int PlayingCard::heart=1;
const int PlayingCard::spade=2;
const int PlayingCard::club=3;
/*****CONSTRUCTOR PLAYING CARD*****/
PlayingCard::PlayingCard(int X,int Y)
{
rank=X;
suit=Y;
if(Y==0 || Y==1)
{
color='r';
}
else if(Y==2 || Y==3)
{
color='b';
}
else
{
cout<<"invalid suit,object not created"<<endl;
}
}
/*****CONSTRUCTOR OVERLOADED*****/
PlayingCard::PlayingCard()
{
}
/*****destructor*****/
PlayingCard::~PlayingCard()
{
}
/*****CONSTRUCTOR PILE OF CARDS*****/
PileofCards::PileofCards(int X,int Y)
{
size=X;
position=Y;
pile= new PlayingCard[size];
top=-1;//empty array
}
/*****DESTRUCTOR*****/
PileofCards::~PileofCards()
{
delete [] pile;
}
/*****IS EMPTY*****/
bool PileofCards::IsEmpty()
{
if(top==-1)
{
cout<<"pile empty"<<endl;
return true;
}
else
{
return false;
}
}
/*****IS FULL*****/
bool PileofCards::IsFull()
{
if(top==size-1)
{
cout<<"sorry cannot add since pile is full"<<endl;
return true;
}
else
{
return false;
}
}
/*****ADDING PLAYING CARD TO THE PILE*****/
void PileofCards::Add(PlayingCard X)
{
if(!IsFull())
{
pile[top+1]=X;
top++;
}
}
/*****REMOVING CARD FROM THE PILE*****/
PlayingCard PileofCards::Remove()
{
if(!IsEmpty())
{
top--;
}
return pile[top+1];
}
/*****PEEKING TOP OF THE PILE*****/
PlayingCard PileofCards::Peek()
{
return pile [top];
}
/*****DISPLAY....PLAYING CARD*****/
void PlayingCard::display()
{
cout<<rank<<" "<<suit<<" "<<color<<endl;
}
/*****CONSTRUCTOR DECK*****/
Deck::deck()
{
int j=0;
for(int i=1;i<=13;i++)
{
deck[j]=new PlayingCard(i,PlayingCard::spade);
j++;
}
for(int k=1;k<=13;k++)
{
deck[j]=new PlayingCard(k,PlayingCard::diamond);
j++;
}
for(int l=1;l<=13;l++)
{
deck[j]=new PlayingCard(l,PlayingCard::heart);
j++;
}
for(int m=1;m<=13;m++)
{
deck[j]=new PlayingCard(m,PlayingCard::club);
j++;
}
int size=52;
}
/*****DESTRUCTOR DECK*****/
Deck::~Deck()
{
if(!IsEmpty())
{
for(int i=0;i<size;i++)
{
if(deck[i]!=NULL)
{
delete deck[i];
}
}
}
else
{
delete [] deck;
}
}
/*****GET SIZE*****/
int Deck::getSize()
{
return size;
}
/*****IS EMPTY....DECK*****/
bool Deck::IsEmpty()
{
if(getSize()==0)
{
return true;
}
else
return false;
}
/*****GETCARD*****/
PlayingCard Deck::getCard(int j)
{
return *deck[j];
}
/*****DISPLAY....DECK*****/
void Deck:display()
{
for(int i=0;i<size;i++)
{
deck[i]->display();
}
}
/*****REMOVE...DECK*****/
PlayingCard Deck::removeCard(int i)
{
PlayingCard temp;
temp=getCard(i);
delete deck[i];
if(!IsEmpty())
{
for(int j=i;j<size;j++)
{
deck[j]=deck[j+1];
}
}
size--;
return temp;
}
/*****SHUFFLE*****/
void Solitire::shuffle()
{
int i;
while (!deckofCards.IsEmpty())
{
i = rand()%deckofCards.getSize();
// cout<<"Remove Card \n";
shuffled.Add(deckofCards.removeCard(i));
}
}
/*****DISPLAY...SOLITIRE*****/
void Solitire::display()
{
if(!shuffled.IsEmpty())
{
shuffled.display();
}
}
/*****DISPLAY...PILE OF CARDS*****/
void PileofCards::display()
{
int i=0;
while(i<=top)
{
pile->display();
i++;
pile++;
}
cout<<i<<endl;
}
/*****MAIN*****/
int main()
{
PlayingCard A(3,PlayingCard::spade);
PlayingCard B(2,PlayingCard::spade);
PlayingCard C(4,PlayingCard::heart);
PlayingCard D(5,PlayingCard::club);
PlayingCard E(3,PlayingCard::diamond);
PlayingCard temp (3,PlayingCard::heart);
PileofCards pile1(5,1);
//cout<<"blah"<<endl;
pile1.Add(A);
pile1.Add(B);
pile1.Add(C);
pile1.Add(D);
pile1.Add(E);
pile1.Add(temp);//displays error
//cout<<"blah"<<endl;
temp=pile1.Remove();
temp.display();
temp=pile1.Remove();
temp.display();
temp=pile1.Remove();
temp.display();
temp=pile1.Remove();
temp.display();
temp=pile1.Remove();
temp.display();
temp=pile1.Remove();//displays error
Deck D1;
PlayingCard F(1,1);
cout<<"original deck"<<endl;
D1.Display(); // Original Deck- 52 cards should be displayed
cout<<"removed cards"<<endl;//index from 0-51
F = D1.removeCard(1);
F.display();
F = D1.removeCard(1);
F.display();
F = D1.removeCard(1);
F.display(); // three cards removed
cout<<"final deck after removal"<<endl;
D1.Display(); //49 cards should be displayed now. 3 cards are already removed
cout<<"loop empties deck"<<endl;
int i=0;
while(!D1.IsEmpty())
{
A = D1.removeCard(i);
A.display();
}
cout<<"emptied"<<endl;
Solitire S;
S.display(); //shuffled pile is empty- nothing should be displayed
S.shuffle();
S.display(); //Shuffled pile now contains 52 shuffled cards.
return 0;
}