Population

Poke---Shuffler

    A.First Edition

    This is actually first edition of my poke class and it has only a trivial function of shuffling.

    B.The problem

    Everybody plays poke, right? And I simply want to write a program that can play the simple game. There have been

    countless such programs.

C.The idea of program

    How to design your classes? I think it is a good practice for me to design such container classes, such as

    Suit is in Card, Card is in Player. You see one Suit has 13 cards, and one Card has four Suits.

    I used a lot of operator overloading, and the friend function is really annoying! I even re-installed the VC++!

    I cannot figure out why it always gives me a wrong message on the friend function of a class. It seems that compiler

    cannot recognize this function as friend function unless it is defined as inline function. Why????

    Another painful thing is that I cannot declare variables or constants in header file cause the driver .cpp file

    need to include the header file and then this variables will be re-declared! Why???

    D.The major functions

    กก

    //Poke.h
    #ifndef  POKE_H
    #define POKE_H
    #include <iostream>

    using namespace std;

    enum SuitColor
    {Spade, Heart, Diamond, Club};

    enum ColorCard
    {J =11, Q = 12, K= 13, A=14};

    const int SuitLength = 13;

    enum Status
    {InHand, Played};

    enum Position
    {UpHand, Opposit, DownHand, SelfHand};

    //a help class
    class Suit
    {
    	friend ostream& operator<<(ostream& out, const Suit& mySuit)
    	{
    		out<<"suit name is "<<mySuit.suitName<<" length is "<<mySuit.length<<"\n";
    		for (int i=0; i<mySuit.length; i++)
    		{
    			if (mySuit.suit[i]>1&&mySuit.suit[i]<11)
    			{
    				out<<mySuit.suit[i]<<" ";
    			}
    			else
    			{
    				switch (mySuit.suit[i])
    				{
    				case 14:
    					out<<"A"<<" ";
    					break;
    				case 11:
    					out<<"J"<<" ";
    					break;
    				case 12:
    					out<<"Q"<<" ";
    					break;
    				case 13:
    					out<<"K"<<" ";
    					break;
    				}
    			}
    		}
    		return out;
    	}

    private:
    	int suit[SuitLength];
    	int length;
    	char* suitName;
    public:
    	const char* getName() const { return suitName;}
    	void setName(const char* name);
    	const int& operator[](int index) const  {return suit[index];}
    	int& operator[](int index) { return suit[index];}
    	int getLength() const { return length;}
    	void operator()(int newCard);
    };

    class Card
    {
    	friend ostream& operator<<(ostream& out, const Card& myCard)
    	{
    		for (int i=0; i<4; i++)
    		{
    			out<<myCard.cards[i]<<"\n";
    		}
    		out<<"\n";
    		return out;
    	}
    private:
    	Suit cards[4];
    	int cardNumber;
    public:
    	const Suit& operator[](int suit) const {return cards[suit];}
    	Suit& operator[](int suit) { return cards[suit];}
    	bool addCard(int suitColor, int card);
    	Card();
    };

    class Poke
    {
    	//I don't know why I have to define it as an inline function
    	friend ostream& operator<<(ostream& out, const Poke& poke)
    	{
    		for (int i=0; i<4; i++)
    		{
    			for (int j=0; j<poke.myHand[InHand][i].getLength(); j++)
    			{
    				out<<poke.myHand[InHand][i][j]<<" ";
    			}
    		}
    		return out;
    	}

    private:
    	static int round;
    	Card myHand[2];
    	Card enemy[3];
    public:
    	void setHand(const Card&  aHand);
    };

    class Player
    {
    	friend ostream& operator<<(ostream& out, const Player& player)
    	{
    		for (int i=0; i<4; i++)
    		{
    			out<<shuffledCards[i]<<"\n";
    		}
    		return out;
    	}

    private:
    	static Card shuffledCards[4];
    	void initialize();
    public:
    	void shuffle();
    	Card& operator[](int index) { return shuffledCards[index];}

    };

    #endif

    //poke.cpp

    #include <iostream>
    #include "Poke.h"

    const char* SuitName[4] = {"Spade", "Heart", "Diamond", "Club"};

    Card Player::shuffledCards[4];
    int Poke::round = 0;

    void Poke::setHand(const Card& aHand)
    {
    	for (int i=0; i<4; i++)
    	{
    		for (int j=0; j<aHand[i].getLength(); j++)
    		{
    			myHand[InHand][i][j] = aHand[i][j];
    		}
    	}
    }

    void Player::initialize()
    {
    	for (int i=0; i<4; i++)
    	{
    		for (int j=1; j<14; j++)
    		{
    			shuffledCards[i].addCard(i, j);
    		}
    	}
    }

    void Player::shuffle()
    {
    	int index = rand()%4;
    //	initialize();
    	for (int i=0; i<4; i++)
    	{
    		for (int j=2; j<=SuitLength+1; j++)
    		{
    			//will do until adding succeeded
    			while (true)
    			{
    				index = rand()%4;
    				if (shuffledCards[index].addCard(i, j))
    				{
    					break;
    				}
    			}
    		}
    	}
    }

    bool Card::addCard(int suitColor, int card)
    {
    	if (cardNumber==SuitLength)
    	{
    		return false;
    	}
    	else
    	{
    		cards[suitColor](card);
    		cardNumber++;
    		return true;
    	}
    }

    Card::Card()
    {
    	cardNumber = 0;
    	for (int i=0; i<4; i++)
    	{
    		cards[i].setName(SuitName[i]);
    	}
    }

    void Suit::setName(const char* name)
    {
    	delete []suitName;
    	suitName = new char(strlen(name)+1);
    	strcpy(suitName, name);
    }

    void Suit::operator ()(int newCard)
    {
    	int first= newCard, second;
    	bool insertNow = false;
    	bool small = true;
    	for (int i=0; i<=length; i++)
    	{
    		if (!insertNow&& newCard<suit[i])
    		{
    			small = true;
    		}
    		if (!insertNow&&small&&newCard>suit[i])
    		{
    			insertNow = true;
    		}
    		if (insertNow)
    		{
    			//insert here
    			second = suit[i];
    			suit[i] = first;
    			first = second;
    		}
    	}
    	length++;

    }

    //driver.cpp

    #include <ctime>
    #include "Poke.h"

    #include <iostream>

    using namespace std;

    int main()
    {
    	srand(time(0));
    	Player P;
    	P.shuffle();
    	cout<<P;
    	return 0;
    }

    กก

    The following is result of program:

    suit name is Spade length is 3
    A Q 2
    suit name is Heart length is 3
    8 7 5
    suit name is Diamond length is 5
    K 10 8 4 2
    suit name is Club length is 2
    Q 4

    suit name is Spade length is 3
    J 9 3
    suit name is Heart length is 5
    A K J 10 6
    suit name is Diamond length is 3
    Q 9 5
    suit name is Club length is 2
    7 6

    suit name is Spade length is 4
    K 8 7 6
    suit name is Heart length is 1
    2
    suit name is Diamond length is 2
    A 6
    suit name is Club length is 6
    A K J 9 3 2

    suit name is Spade length is 3
    10 5 4
    suit name is Heart length is 4
    Q 9 4 3
    suit name is Diamond length is 3
    J 7 3
    suit name is Club length is 3
    10 8 5

    Press any key to continue

กก