Thread: solitire

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    5

    solitire

    i am writing the code for solitire.....basically when i randomly select the cards from the original deck an put them in a pile ......when the last card is added from the deck to the pile the program halts.....i dont know why is this happening...can smbody plz help.......

    Code:
    #include<iostream>
    #include<iomanip>
    #include<cstdlib>
    
    using namespace std;
    
    class PlayingCard  
    {
    private: 
      		int rank; 
    		int suit;  
    		char color; // red or black - ‘b’ for black , ‘r’ for red
    public:
    	PlayingCard copy(PlayingCard );
    	PlayingCard::PlayingCard();
    	PlayingCard(int,int);
    	void display();
    	static int diamond;
    	static int heart;
    	static int spade;
    	static int club;
    };
    class PileofCards 
    {
    private: 
    	PlayingCard *pile; //PlayingCard removed
    	int top;
    	int size; 
    	int position; 
    public:
    	
    	PileofCards (int size , int position); 
    	~PileofCards();
    	PlayingCard Peek();
    	void display(); 
    	PlayingCard Remove(); 
    	void Add(PlayingCard);   
    	bool IsEmpty(); 
    	bool IsFull();
    };
    void PileofCards::display()
    {
    	for(int i=0;i<=top;i++)
    	{
    		pile[i].display();
    	}
    }
    class Deck
    {
    private: 
    	PlayingCard *deck[52];
    	int size; 
    public: 
    	Deck();
    	void display();
    	int getSize(); 
    	bool IsEmpty();
    	PlayingCard getCard(int i);
    	PlayingCard removeCard(int i);
    	~Deck();
    };
    class solitire
    {
    private:
    	Deck deckofcards;
    	PileofCards shuffled;
    public:
    	solitire();
    	void shuffle();
    	void display();
    };
    solitire::solitire():shuffled(52,1){}
    void solitire::shuffle()
    {
    	int i;
    	while(deckofcards.IsEmpty())
    	{
    		if(deckofcards.getSize()>0)
    		{
    			i=rand()%deckofcards.getSize();
    			cout<<"Remove Card \n";
    			shuffled.Add(deckofcards.removeCard(i));
    		}
    	}
    }
    void solitire::display()
    {
    	shuffled.display();
    }
    
    ////////////////////constructor for playingcard////////////////////
    
    
    PlayingCard::PlayingCard(){}
    PlayingCard::PlayingCard(int r, int s)
    {
    	if(s==PlayingCard::diamond || s==PlayingCard::heart)
    	{
    		if(r>=0 && r<=13)
    		{
    			rank=r;
    			suit=s;
    			color='r';
    		}
    		
    	}
    	else if(s==PlayingCard::spade || s==PlayingCard::club)
    	{
    		if(r>=0 && r<=13)
    		{
    			rank=r;
    			suit=s;
    			color='b';
    		}
    		
    	}
    	else
    		cout<<"invalid valuess"<<endl;
    }
    
    void PlayingCard::display()
    {
    	switch(rank)
    	{
    	case 11: cout<<setw(2)<<"J";break;
    	case 12: cout<<setw(2)<<"Q";break;
    	case 13: cout<<setw(2)<<"K";break;
    	case 1: cout<<setw(2)<<"A";break;
    	default: cout<<setw(2)<<rank;
    	}
    	if(suit==0)
    		cout<<setw(3)<<char(4);
    	else if(suit==1)
    		cout<<setw(3)<<char(3);
    	else if(suit==2)
    		cout<<setw(3)<<char(5);
    	else 
    		cout<<setw(3)<<char(6);
    	
    	cout<<setw(3)<<color<<endl;
    }
    PlayingCard PlayingCard::copy(PlayingCard x)
    {
    	x.rank=rank;
    	x.suit=suit;
    	x.color=color;
    	return x;
    }
    ////////////////////////// constructor for pileofcards//////////////////////
    PileofCards::PileofCards(int s, int p)
    {
    	pile=new PlayingCard[s];
    	top=(-1);
    	position=p;
    	size=s;
    }
    PileofCards::~PileofCards()
    {
    	delete [] pile;
    }
    void PileofCards::Add(PlayingCard c)
    {
    	if(top!=size)
    		if(IsFull()==false )
    		{
    			pile[top+1]=c;
    			top++;
    		}
    		else if(IsFull()==true)
    			//IsFull();
    			cout<<"error"<<endl;
    }
    bool PileofCards::IsEmpty()
    {
    	if(top==(-1))
    		return true;
    	else
    		return false;
    }
    bool PileofCards::IsFull()
    {
    	if(top>=size-1)
    	{
    		cout<<"ERROR: Pile already full! "<<endl;
    		return true;
    	}
    	else	
    		return false;
    	
    }
    PlayingCard PileofCards::Remove()
    {
    	if(IsEmpty())
    		cout<<"ERROR: Cannot remove. Pile already empty"<<endl;
    	else if(!IsEmpty() && top!=(-1))
    	{
    		top--;
    		return pile[top+1];
    	}
    	
    }
    PlayingCard PileofCards::Peek()
    {
    	return pile[top];
    }
    ///////////////// Constructor for deck////////////////////
    Deck::Deck()
    {
    	size=52;
    	int i=1,k=1,l=1,m=1;
    	for (int j=0; j<size;j++)
    	{
    		
    		if(j<13)
    		{
    			deck[j]= new PlayingCard(i,PlayingCard::heart); 
    			i++;
    		}
            else if(j>12 && j<26)
    		{
    			deck[j]= new PlayingCard(k,PlayingCard::diamond);
    			k++;
    		}
    		else if(j>25 && j<39)
    		{
    			deck[j]= new PlayingCard(l,PlayingCard::club);
    			l++;
    		}
    		else if(j>38 && j<52)
    		{
    			deck[j]= new PlayingCard(m,PlayingCard::spade); 
    			m++;
    		}
    		
    	}     
    }
    Deck::~Deck()
    {
    	for(int i=0;i<size;i++)
    		delete deck[i];
    }
    
    void Deck::display()
    {
    	for(int i=0;i<size;i++)
    	{
    		if(i==13 || i==26 || i==39)
    			cout<<"\n--------\n";
    		deck[i]->display();
    	}
    }
    
    
    PlayingCard Deck::removeCard(int i)
    {
    	if(i<size)
    	{
    		if(i>=0 && i<53)
    		{
    			
    			
    			PlayingCard temp(*deck[i]);
    			
    			delete deck[i];
    			for(int j=i;j<size;j++)
    			{
    				deck[j]=deck[j+1];
    			}
    			size--;
    			cout<<"\ncard removed\n";
    			temp.display();
    			return temp;
    		}
    		else
    			cout<<"ERROR: Deck empty cannot remove!"<<endl;
    	}
    	else 
    		cout<<"ERROR: Invalid entry"<<endl;
    	
    }
    int Deck::getSize()
    {
    	return size;
    }
    bool Deck::IsEmpty()
    {
    	if(size>-1)
    		return true;
    	else
    		return false;
    }
    
    int PlayingCard::diamond=0;
    int PlayingCard::heart=1;
    int PlayingCard::spade=2;
    int PlayingCard::club=3;
    
    int main()
    {
    	solitire S;
    	S.display();
    	S.shuffle();
    	S.display();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    bool Deck::IsEmpty()
    {
    	if(size >0)  // This solves your program halt problem
    	//if(size>-1)
    		return true;
    	else
    		return false;
    }

Popular pages Recent additions subscribe to a feed