Object-Oriented Programming (C++ Example from Fall 1993)


#include <iostream.h>
#include <assert.h>
#include <stdlib.h>


typedef enum {heart,diamond,club,spade} Suit;
typedef enum {two,three,four,five,six,seven,eight,nine,ten,jack,queen,king,ace} FaceValue;

static char *suitName[4]={"heart","diamond","club","spade"};  //suit names
static char *cardName[13]={"two","three","four","five","six","seven","eight",
		"nine","ten","jack","queen","king","ace"};    //holds strings with card names

class Card
{
friend ostream& operator <<(ostream&,Card);
public:
	Card(){cardSuit=Suit(0);cardFaceValue=FaceValue(0);}           // default constructor
	Card(const Card& a){cardSuit=a.cardSuit;
				  cardFaceValue=a.cardFaceValue;} // copy constructor
	~Card(){}
	void setValue(int a,int b){cardSuit=Suit(a);
					   cardFaceValue=FaceValue(b);}
						//set suit and face value of the card
	Card& operator = (const Card& a){cardSuit=a.cardSuit;
					cardFaceValue=a.cardFaceValue;
					return *this; };// assignment operator

private:

	Suit cardSuit;// cards suit
	FaceValue cardFaceValue; // cards face value
 };
      
class Deck
{
friend ostream& operator <<(ostream&, Deck);
public:
	Deck();
        ~Deck(){}
	void shuffle(); 		      // create the list of cards in random order
	Card& deal(){assert(topCard<52);
		     return cards[topCard++];}// deals the card at topCard and increments topCard
private:
	Card cards [52];
	int topCard;// top of the deck
};


template <int hsz, int pla>
class Game
{
public:
	Game(){	players=pla;
		handSize=hsz;};   // number of players and number of cards in hand

	Game(const Game<hsz,pla>&){	players=pla;
					handSize=hsz;};
	void deal();
	void print(ostream&);
private:
	Deck theDeck;                      //Deck of cards
	int players;                       //# of players
	int handSize;                      //Size of each hand
	Array<Array<Card,hsz>,pla> hands;  //Arrays are nested
};

template <class Type, int sz>          //General array template
class Array {
public:

	Array(){size=sz; ia=new Type[size];}
	~Array() {delete [] ia;}
	Type& operator[](int index){	assert(index<size);
					return ia[index];   }

protected:
	int size;  //number of elements in array
	Type *ia;  //pointer to first array element
};



ostream& operator << (ostream& os, Card c )   //card output
{
	int cs = int(c.cardSuit);
	int fv = int(c.cardFaceValue);
	assert( cs>=0 && cs<4 && fv>=0 && fv<13 );
	os<<suitName[cs]<<' '<<cardName[fv];
	return os;
};

Deck::Deck()                            //construct the deck
{
	int k = 0;
	for (int i = 0; i < 4; ++i)
		for (int j = 0; j < 13;++j)
			{cards[k++].setValue(i,j);
                        assert(i<4 && j<13);}
	topCard = 0;
}

void Deck::shuffle()
{                              //shuffle cards randomly
	randomize();
	for(int i = 0; i < 52;++i)
	{
		int k = random(52);   assert(k<52);
                if (k!=i)
		   {Card t = cards[i];cards[i] = cards[k]; cards[k] = t;}

        }
}

template <int hsz, int pla>             //Deal cards
void Game<hsz,pla>::deal()  {

	theDeck.shuffle();
	for(int i=1;i<=players;++i)      //loop thru players
	       {
		 for(int j=1;j<=handSize;++j)        //loop thru hands
		   hands[i-1][j-1]=theDeck.deal();  }    //deal a card
}

template <int hsz, int pla>
void Game<hsz,pla>::print(ostream& os)  {

   for(int i=1;i<=players;++i)  {cout<<"\nPlayer #"<<i<<"\n";
	for(int j=1;j<=handSize;++j)
		cout<<hands[i-1][j-1]<<"\n"; }

}




//main program for your tests.
void main()
{
	Game<5,3> g;       //declare game
	g.deal();          //deal cards from deck
	g.print(cout);     //display results
}




Return to Dan's Home Page
email
Last Update: November 2, 2003