| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 473 人关注过本帖
标题:[讨论]看看大家的意见
只看楼主 加入收藏
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
 问题点数:0 回复次数:3 
[讨论]看看大家的意见

前一段时间在教科书上遇到一道题~要用一个类来表示一副有52张牌的扑克牌~想来想去都想不到好的方法~大家有什么好的方法啊~

搜索更多相关主题的帖子: 意见 
2006-05-08 11:03
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

是不是太基础了~大家没兴趣写啊~

2006-05-08 17:20
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分: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 ", "K "};
const cardtype intCardTypeStart = CardTypeHearts;
const int intCardTypeNum = END_PLUS_ONE_CardType - intCardTypeStart;
const int intCardIDNum = sizeof(chararCardFace)/sizeof(char *);

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];
};


http://myajax95./
2006-05-09 03:11
不得不编
Rank: 1
等 级:新手上路
帖 子:65
专家分:0
注 册:2006-4-4
收藏
得分:0 

既然没人上来讨论~我就把我的笨办法贴一下喽~
#include<iostream>
#include<string>
#include<ctime>
using namespace std;

class deck
{
public:
deck()
{
cout<<"请输入玩家名称:";
cin>>name;
cout<<"\n\n";
s[0]="A (方块)";s[1]="2 (方块)";s[2]="3 (方块)";s[3]="4 (方块)";s[4]="5 (方块)";s[5]="6 (方块)";s[6]="7 (方块)";s[7]="8 (方块)";s[8]="9 (方块)";s[9]="10(方块)";s[10]="J(方块)";s[11]="Q(方块)";s[12]="K(方块)";
s[13]="A (梅花)";s[14]="2 (梅花)";s[15]="3 (梅花)";s[16]="4 (梅花)";s[17]="5 (梅花)";s[18]="6 (梅花)";s[19]="7 (梅花)";s[20]="8 (梅花)";s[21]="9 (梅花)";s[22]="10(梅花)";s[23]="J(梅花)";s[24]="Q(梅花)";s[25]="K(梅花)";
s[26]="A (红心)";s[27]="2 (红心)";s[28]="3 (红心)";s[29]="4 (红心)";s[30]="5 (红心)";s[31]="6 (红心)";s[32]="7 (红心)";s[33]="8 (红心)";s[34]="9 (红心)";s[35]="10(红心)";s[36]="J(红心)";s[37]="Q(红心)";s[38]="K(红心)";
s[39]="A (黑桃)";s[40]="2 (黑桃)";s[41]="3 (黑桃)";s[42]="4 (黑桃)";s[43]="5 (黑桃)";s[44]="6 (黑桃)";s[45]="7 (黑桃)";s[46]="8 (黑桃)";s[47]="9 (黑桃)";s[48]="10(黑桃)";s[49]="J(黑桃)";s[50]="Q(黑桃)";s[51]="K(黑桃)";
}
void send()
{
srand(time(0));
i=rand()%52+1;
j=rand()%52+1;
cout<<"电脑的牌:"<<s[i]<<"\n\n\n"
<<"玩家 "<<name<<" 的牌:"<<s[j]<<"\n\n";
si=s[i].substr(0,1);
sj=s[j].substr(0,1);
switch(si[0])
{
case '1':ii=10;
case 'A':ii=1;
case 'J':ii=11;
case 'Q':ii=12;
case 'K':ii=13;
default:ii=si[0];
}
switch(sj[0])
{
case '1':jj=10;
case 'A':jj=1;
case 'J':jj=11;
case 'Q':jj=12;
case 'K':jj=13;
default:jj=sj[0];
}


if(ii==jj)
cout<<"*****电脑与玩家不分胜负!*****\n\n";
else if(ii>jj)
cout<<"*****很遗憾,你输了!*********\n\n";
else
cout<<"*****恭喜你赢了!*************\n\n";
}
protected:
string s[52];
string name,si,sj;
int i,j,ii,jj;
};

void main()
{
deck d;
string choice;
cout<<"发牌:\n\n\n";
d.send();


while(true)
{
cout<<"要再发牌吗?(y/n)";
cin>>choice;
cout<<"\n\n";
if(choice=="y")
d.send();
else if(choice=="n")
break;
else
cout<<"输入错误!请重新输入!";
}
}

2006-05-09 22:30
快速回复:[讨论]看看大家的意见
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.034812 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved