21点牌标准代码

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#include
#include
#include"string.h"
#include
using namespace std;
///////////////////////
////////////////////////////////////
class Card
{
private:
char m_type;
int m_num;
public:
void card(char a,int b){m_type=a,m_num=b;}
int outnum(){return m_num;}
int GetText();
};
class Poker
{
private:
int count;
Card card[54];
public:
Poker();
void Xipai();
int Fapai();
int sum(int,int,int);
Card *getcard();
};

///////////////////////////////////////////////////
Card *h;
int get(int max,int min);
// TODO: reference additional headers your program requires here
///////////////////////////////////////////
class Player
{
public:
Card *card[20];
int count;
char name[10];
int score;
Player(){
count=score=0;
for(int i=0;i<20;i++)
card[i]=NULL;
}
int showpoint();
void showpoker();
void reset();
};
class Newgame
{
private:
Poker *poker;
Player *player[5];
int num,n;
char boss[10],a[10];
public:
Newgame();
void login();
void begin();
char *victory();
bool callpoker(Player &);
void show();
};
////////////////////////
void Newgame::show()
{
for(int f=0;f<54;f++)
poker->Fapai();
}
/////////////////////////////////
int get(int max,int min)
{

int n=rand();
n=n%(max-min+1)+min;
return n;
}

int Card::GetText()
{
if((1==m_num)&&(m_type=='K')){ printf("大王 ");return 1;}
if((0==m_num)&&(m_type=='K')){ printf("小王 ");return 1;}
int k=m_num%13;
switch(k)
{
case 1:{if(m_type=='H') cout<<"红桃A ";
if(m_type=='C') cout<<"梅花A ";
if(m_type=='D') cout<<"方块A ";
if(m_type=='S') cout<<"黑桃A ";
return 1;}
case 11:{
if(m_type=='H'){ cout<<"红桃J "; return 1;}
if(m_type=='C'){ cout<<"梅花J "; return 1;}
if(m_type=='D'){ cout<<"方块J "; return 1;}
if(m_type=='S'){ cout<<"黑桃J "; return 1;}
}
case 12:
{
if(m_type=='H'){ cout<<"红桃Q "; return 1;}
if(m_type=='C'){ cout<<"梅花Q "; return 1;}
if(m_type=='D'){ cout<<"方块Q "; return 1;}
if(m_type=='S'){ cout<<"黑桃Q "; return 1;}
}
case 0:{if(m_type='H'){ cout<<"红桃K "; return 1;}
if(m_type=='C'){ cout<<"梅花K "; return 1;}
if(m_type=='D'){ cout<<"方块K "; return 1;}
if(m_type=='S'){ cout<<"黑桃K "; return 1;}
}
return 1;
}
if(m_num<=10&&m_num>=2)
{
if(m_type=='H'){ cout<<"红桃"<if(m_type=='C'){ cout<<"梅花"<if(m_type=='D'){ cout<<"方块"<if(m_type=='S'){ cout<<"黑桃"<}
return 1;
}
Poker::Poker(){
count=54;
for(int i=0;i<13;i++)
card[i].card('H',(i+1));
for(i=13;i<26;i++)
card[i].card('C',((i%13)+1));
for(i=26;i<39;i++)
card[i].card('D',((i%13)+1));
for(i=39;i<52;i++)
card[i].card('S',(i%13)+1);
card[52].card('K',1);
card[53].card('K',0);
}

void Poker::Xipai()


{
count=54;
for(int k=0;k<54;k++)
{
int m=get(54,1)%54;
Card r;
r=card[k];
card[k]=card[m];
card[m]=r;
}
}
int Poker::Fapai()
{
if(count<=54&&count>=1){
card[54-count].GetText();
count--;
}
else
cout<<"牌已经发完!"<return count;
}
Card *Poker::getcard(void)
{
if(count<=54&&count>0)
{
h=&card[54-count];
count--;
return h;
}
else
cout<<"牌已经发完!"<return NULL;
}
int Poker::sum(int a,int b,int c)
{
return card[a].outnum()+card[b].outnum()+card[c].outnum();
}
///////////////////////////////////////////////////
Newgame::Newgame()
{
poker=new Poker;
login();
num=n+1;
player[0]=new Player;
strcpy(player[0]->name,a);
for(int i=1;i<=n;i++)
{
player[i]=new Player;
sprintf(player[i]->name,"Computer%d",i);
}
for(i=num;i<5;i++)
player[i]=NULL;
}
void Newgame::login()
{
system("COLOR 1D");
cout<<"21点游戏...."<cout<<"请输入你的名字:"<cin>>a;
cout<<"你想同多少个电脑一起游戏?(1~4个)"<cin>>n;
while(n<1||n>4)
{
cout<<"输入出错!请你输入一个1~4的数字,以便开始游戏!"<cin>>n;
}
}
bool Newgame::callpoker(Player &t)
{
if(strcmp(,player[0]->name)!=0)
{
if(t.showpoint()<11||
(t.showpoint()>10&&get(15,1)<=(21-(t.showpoint() ))&&t.showpoint()<=21))
{
t.card[t.count]=poker->getcard();
cout<<<<"抓了一个牌!"<t.count++;
return 1;
}
else return 0;
}
else
{if(t.showpoint()>=21) return 0;
else{
cout<<"你想继续抓牌吗?你现在的点数为:"<char a;
cin>>a;
while(a!='y'&&a!='Y'&&a!='N'&&a!='n')
{
cout<<"请你再次确认输入是否出错?(N or Y)"<cin>>a;
}
if(a=='Y'||a=='y')
{
t.card[t.count]=poker->getcard();
cout<<"你抓到了一张 ";
t.card[t.count]->GetText();
cout<t.count++;
return 1;
}
else
return 0;
}
}
}
int Player::showpoint()
{
int j=0,sum=0;
while(card[j]!=NULL)
{
if(card[j]->outnum()>10||card[j]->outnum()==0) sum+=1;
else sum+=card[j]->outnum();
j++;
}
return sum;
}
void Player::showpoker()
{
int j=0;
while(card[j]!=NULL)
{
card[j]->GetText();
j++;
}
cout<}
void Player::reset()
{
for(int i=0;i<20;i++)
card[i]=NULL;
count=0;
}
char *Newgame::victory()
{
int k=1;Player *r=player[0];
while(k{
if(r->showpoint()==player[k]->showpoint())
{
if(!strcmp(player[k]->name,boss))
{ r=player[k]; k++;continue;}
else
{ k++;continue;}
}
if(r->showpoint()<=21&&player[k]->showpoint()<=21)
{ if(r->showpoint()showpoint())
r=player[k];
}
else
{
if(r->showpoint()<=21) r=r;
else r=player[k];
}
k++;
}
r->score++;
return r->name;
}
void Newgame::begin()
{
cout<<"现在你将开始游戏!"<poker->Xipai();
int w=0;
bool end=true;
while(1)
{
strc

py(boss,player[w]->name);
cout<<"现在是"<name<<"作庄!"<while(1)
{
for(int l=w;l{
if(callpoker(*player[l%num]))
end=false;
}
if(end)
break;
else end=true;
}
cout<<"赢家是:"<cout<<"各自的点数及牌为:"<for(int k=0;k{
cout<name<<": "<showpoint()<<" ";
player[k]->showpoker();
}
for(int u=0;u{
cout<name<<"的积分为:"<score<player[u]->reset();
}
cout<<"是否继续游戏(Y or N)?"<char h;
cin>>h;
while(h!='y'&&h!='Y'&&h!='n'&&h!='N')
{
cout<<"请确认输入是否正确:(Y or N)?"<cin>>h;
}
if(h=='Y'||h=='y')
{
w=(w+1)%num;
end=true;
poker->Xipai();
system("cls");
}
else
{
cout<<"谢谢使用!"<break;
}
}
}

void main()
{
srand((unsigned)time(NULL));
Newgame game;
game.begin();
}

相关文档
最新文档