A Card Game Example

A Card Game Example

 It is a long progrmming example.It does not inroduce any new concept but it almost use all the programming ideas which are discussed earlier in programming fundamental and oop.

 


#include <iostream>
using namespace std;


enum suit{ clubs,diamonds,hearts,spades};
const int jack=11 ; // from 2 to 10 are 


const int queen=12;
const int king=13;
const int ace=14;
class card
{
private:
int number;
suit suit;
public:
card()
{}
card(int n ,suit s):number(n), suit(s)
{}
void print();
bool isequal(card);
};
void card::print()
{
if (number>=2 && number<=10)
cout<< number<<"of";
else
switch(number)
{
case jack: cout<<"jack of";break;
case queen: cout<<"queen of";break;
case king: cout<<"king of";break;
case ace: cout<<"ace is ";break;
}
switch(suit)
{
case clubs: cout<<"clubs";break;
case diamonds: cout<<"diamonds";break;
case hearts: cout<<"hearts";break;
case spades: cout<<"spades";break;
}
}
bool card::isequal(card c2)
{
return (number==c2.number && suit==c2.suit) ? true :false;
}
void main()
{
card temp,chosen,prize;
int position;
card card1(7,clubs);
cout<<"card 1 is the";


card1.print( );
card card2(jack,hearts);


cout<<"card 2 is the";
card2.print( );


card card3(ace,spades);


cout<<"card 3 is the";
card3.print( );


prize=card3

;
cout<<"i am swaping card 1 and card 3";


temp=card3;card3=card1;card1=temp;


cout<<"i am swaping card 2 and card 3";


temp=card3;card3=card2;card2=temp;


cout<<"i am swaping card 1 and card 2";


temp=card2;card2=card1;card1=temp;


cout<<"now ,where(1 , 2,or 3) is the";


prize.print();
cout<<"?";


cin>>position;


switch (position)
{
case 1: chosen =card1;break;
case 2: chosen =card2;break;


case 3: chosen =card3;break;
}


if( chosen.isequal(prize))


cout<<"That is right! you win ";


else
cout<<"sorry . you lose."; 


cout<<"you chose the";
chosen.print( );


cout<<endl;
}