前一段时间在教科书上遇到一道题~要用一个类来表示一副有52张牌的扑克牌~想来想去都想不到好的方法~大家有什么好的方法啊~
是不是太基础了~大家没兴趣写啊~
既然没人上来讨论~我就把我的笨办法贴一下喽~
#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<<"输入错误!请重新输入!";
}
}