图片附件: 游客没有浏览图片的权限,请
登录 或
注册
把DEBUG设成1就可以分步显示了。
程序代码:
#include \"stdafx.h\"
#include <deque>
#include <sstream>
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#define DEBUG 0
typedef enum CardType
{
CardTypeHearts = 3,
CardTypeDiamond,
CardTypeClubs,
CardTypeSpades,
END_PLUS_ONE_CardType
} cardtype;
const char *chararCardFace[] = {\"A \", \"2 \", \"3 \", \"4 \", \"5 \", \"6 \", \"7 \", \"8 \", \"9 \", \"10\", \"J \", \"Q \"};
const cardtype intCardTypeStart = CardTypeHearts;
const int intCardTypeNum = END_PLUS_ONE_CardType - intCardTypeStart;
const int intCardIDNum = sizeof(chararCardFace)/sizeof(char *);
int random(int intUpper)
{
static bInitialized = false;
if (! bInitialized)
{
srand( (unsigned)time( NULL ) );
bInitialized = true;
}
return rand() % intUpper;
}
class CCard
{
public:
CCard(cardtype type, int intID, bool bUp = false) : m_cType(type), m_intID(intID), m_bUp(bUp) {}
void TurnUp() {m_bUp = true;}
int GetID() {return m_intID;}
bool IsUp() {return m_bUp;}
string ToString() const;
private:
cardtype m_cType;
int m_intID;
bool m_bUp;
};
string CCard::ToString() const
{
stringstream ssOutput;
char chrType = m_cType;
if (chrType < intCardTypeStart || chrType > END_PLUS_ONE_CardType
|| m_intID < 0 || m_intID >= intCardIDNum)
return \"UNKNOWN\";
ssOutput << chrType << chararCardFace[m_intID];
if (m_bUp)
ssOutput << \"(Up)\";
else
ssOutput << \"(Dn)\";
return ssOutput.str();
}
ostream& operator << (ostream &o, const CCard &card)
{
return o << card.ToString();
}
class CCardDeck
{
public:
CCardDeck();
string ToString() const;
void Serve();
void Shuffle();
void Play();
private:
deque<CCard *> m_deqpCardColumns[intCardIDNum];
CCard *m_pSEQs[intCardTypeNum * intCardIDNum];
int m_intPlayedColumn;
};
CCardDeck::CCardDeck() : m_intPlayedColumn(0)
{
for (int intTypeCount = 0; intTypeCount < intCardTypeNum; intTypeCount++)
{
for (int intIDCount = 0; intIDCount < intCardIDNum; intIDCount++)
m_pSEQs[intTypeCount * intCardIDNum + intIDCount] = new CCard((cardtype)(intCardTypeStart+intTypeCount), intIDCount);
}
}
string CCardDeck::ToString() const
{
stringstream ssOutput;
for (int intColumnCount = 0; intColumnCount < intCardIDNum; intColumnCount++)
{
ssOutput << \"Column \" << intColumnCount;
for (int intCardCount = 0; intCardCount < m_deqpCardColumns[intColumnCount].size(); intCardCount++)
{
if (m_deqpCardColumns[intColumnCount][intCardCount])
ssOutput << \" \" << *m_deqpCardColumns[intColumnCount][intCardCount];
}
ssOutput << endl;
}
return ssOutput.str();
}
void CCardDeck::Serve()
{
for (int intCardCount = 0; intCardCount < intCardTypeNum; intCardCount++)
{
for (int intColumnCount = 0; intColumnCount < intCardIDNum; intColumnCount++)
m_deqpCardColumns[intColumnCount].push_back(m_pSEQs[intCardCount * intCardIDNum + intColumnCount]);
}
}
void CCardDeck::Shuffle()
{
for (int intCardCount = 0; intCardCount < intCardIDNum*intCardTypeNum; intCardCount++)
swap(m_pSEQs[intCardCount], m_pSEQs[random(intCardIDNum*intCardTypeNum)]);
}
void CCardDeck::Play()
{
CCard *pCard;
int intUpAs = 0;
while (intUpAs < intCardTypeNum)
{
pCard= m_deqpCardColumns[m_intPlayedColumn][m_deqpCardColumns[m_intPlayedColumn].size()-1];
m_deqpCardColumns[m_intPlayedColumn].pop_back();
pCard->TurnUp();
m_intPlayedColumn = pCard->GetID();
m_deqpCardColumns[m_intPlayedColumn].push_front(pCard);
if (!strcmp(chararCardFace[pCard->GetID()], \"A \"))
intUpAs++;
if (DEBUG)
cout << ToString() << endl;
}
}
ostream& operator << (ostream &o, const CCardDeck &deck)
{
return o << deck.ToString();
}
int main(int argc, char* argv[])
{
CCardDeck deck;
deck.Shuffle();
deck.Serve();
cout << deck << endl;
deck.Play();
cout << deck << endl;
return 0;
}
[此贴子已经被作者于2006-4-15 23:40:45编辑过]