C++小游戏:扑克牌21点

时间:2023-03-09 03:25:55
C++小游戏:扑克牌21点

21点扑克牌游戏:

程序说明:该程序是模拟21点扑克牌游戏,玩家最多可以要5张牌,但是如果牌的点数之和超过21点,则自动出局,在不超过21点的情况下,玩家与庄家比牌的大小,大者为赢家

程序片段分析:

【1】头文件处:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;

cstdlib这个头文件里面,等价与stdlib,因为要生成随机数,需要调用函数rand和srand,百科里面的简介http://baike.baidu.com/view/5855614.htm?fr=aladdin

ctime这里注意是调用时间,格林时间http://baike.baidu.com/view/654014.htm?fr=aladdin#1

【2】之后定义了一个类class

class CCard
{
private:
int naPip[];//一共5张牌
int nNumber;//发了多少张牌
int nDollar;//有多少钱
int nGamble;//赌注
int nWin;//赢的局数
int nLose;//输的局数
int nDraw;//平局数 public:
CCard();//构造函数初始化
void FirstPlayTwo();//最初两张牌
int GetNumber();//返回牌数
int GetPip();//返回点数
void DisplayPip();//显示全部牌面
void DisplayPip(int);//除了第一张,依次全部牌面点数(针对计算机的牌显示)
void TurnPlay();//出一张牌
void Win();//赢了计算赌注
void Lose();//输了
void Draw();//平局
int SetGamble(int);//设置赌注
int GetMoney();//返回钱数
void DisplayInfo();//打印必要信息
int GetCurrentCard();//返回当前牌点数
};

这里有几个地方需要注意:第一私有数据里面包括了数组,第二,出现了这种东西

void DisplayPip();//显示全部牌面
void DisplayPip(int);//除了第一张,依次全部牌面点数(针对计算机的牌显示)

这是C++里面的重载,好象是

【3】比较重要的函数就是生成随机数;

首先初始化随机数种子用srand() 如何生成随机数,这里有解释http://baike.baidu.com/view/1458234.htm

【4】其次要注意的是这个声明 void Judge(CCard &cpu,CCard &player)//判断输赢

void Judge(CCard &cpu,CCard &player)//判断输赢

这里采用的是这个用途:变量的引用,例如

int a;
int &b = a;//声明b是a的引用

&是引用声明符,并不代表地址,不要理解为:把a的值赋值给b的地址,声明变量b为引用类型,并不需要另外开辟内存单元来存放b的值,b和a占内存中的同一个存储单元

【5】flag的引用也比较经典:在函数void Judge(CCard &cpu,CCard &player)//判断输赢中和主函数中都有应用:

 while(chChoice == 'Y'||chChoice =='y')
{
do
{
cout<<"您现在有赌本:$"<<player.GetMoney();
cout<<"\n请下注(赌注不能超过赌本)";
cin>>nMoney;
blLogic = player.SetGamble(nMoney);
if(blLogic)
cout<<"您的赌本不够,请重新下注!\n"; }
while(blLogic);
PlayTurn(cpu,player);
cout<<"是否继续21点游戏(Y/N)?\n";
cin>>chChoice;
}

这是21点游戏的总代码:

#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class CCard
{
private:
int naPip[];//一共5张牌
int nNumber;//发了多少张牌
int nDollar;//有多少钱
int nGamble;//赌注
int nWin;//赢的局数
int nLose;//输的局数
int nDraw;//平局数 public:
CCard();//构造函数初始化
void FirstPlayTwo();//最初两张牌
int GetNumber();//返回牌数
int GetPip();//返回点数
void DisplayPip();//显示全部牌面
void DisplayPip(int);//除了第一张,依次全部牌面点数(针对计算机的牌显示)
void TurnPlay();//出一张牌
void Win();//赢了计算赌注
void Lose();//输了
void Draw();//平局
int SetGamble(int);//设置赌注
int GetMoney();//返回钱数
void DisplayInfo();//打印必要信息
int GetCurrentCard();//返回当前牌点数
}; CCard::CCard()//构造函数初始化class CCard
{
nNumber = ;
nDollar = ;//设赌注为100块钱
for(int i = ;i<;i++)
naPip[i] = ;
nGamble = ;
nWin = ;
nLose = ;
nDraw = ; }
int CCard::GetMoney()//
{
return nDollar;
} void CCard::DisplayInfo()
{
cout<<"您一共玩了"<<nWin+nLose+nDraw<<"局,"<<"赢了"<<nWin<<"局,"<<"输了"<<nLose<<"局,"<<"平局"<<nDraw<<"局"<<endl;
cout<<"您的赌资共计有:$"<<nDollar<<".\n"; } int CCard::SetGamble(int gamble)//设赌注
{
if(nDollar - gamble<)
return -;
if(gamble<)
{
if(nDollar - <)
return -;
nGamble = ;
}
else
nGamble =gamble;
nDollar -= nGamble;
return ;
} void CCard::FirstPlayTwo()//获得最初的两张牌
{
naPip[] = rand()%+;
naPip[] = rand()%+;
nNumber = ;
} int CCard::GetCurrentCard()//返回已经得到的两张牌
{
return naPip[nNumber-];
} int CCard::GetNumber()//返回已经发的牌数
{
return nNumber;
}
int CCard::GetPip()//返回当前点数
{
int nPip = ;
for(int i = ;i<nNumber;i++)
{
if(naPip[i]>=)
{
nPip +=;
} else
nPip+=naPip[i];
}
return nPip;
} void CCard::DisplayPip()//依次显示牌点数
{
for(int i=;i<nNumber;i++)
cout<<naPip[i]<<"\t";
cout<<endl;
} void CCard::TurnPlay()//出一张牌
{
nNumber++;
naPip[nNumber-] = rand()%+; } void CCard::Win()//赢了计算赌资
{
cout<<"赢家牌面:";
DisplayPip();
cout<<"牌面点数: "<<GetPip()<<endl;
nDollar = nDollar +*nGamble;
nWin++;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局"<<nDraw<<"次"<<endl;
cout <<endl<<endl;
}
void CCard::Lose()//输了计算赌资
{
nLose ++;
cout<<"输家牌面:";
DisplayPip();
if(GetPip()>)
cout<<"爆了!\n";
else
cout<<"牌面点数: "<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局 "<<nDraw<<"次 "<<endl;
cout<<endl<<endl;
} void CCard::Draw()//平局
{
nDraw++;
nDollar+=nGamble;
cout<<"平局牌面:";
DisplayPip();
if(GetPip()>)
cout<<"爆了! \n";
else
cout<<"牌面点数:"<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局 "<<nDraw<<"次 "<<endl;
cout<<endl<<endl;
} void CCard::DisplayPip(int n)//除了第一张牌,其他的显示
{
cout<<"[*]"<<'\t';
for(int i = ;i<nNumber;i++)
cout<<naPip[i]<<'\t';
cout<<endl;
} void DisplayRule(void)
{
cout<<"\t欢迎进入21点游戏世界!\n\n";
cout<<"\t游戏规则:\n";
cout<<"\t1.玩家最多可以要5张牌;";
cout<<"\t2.如果牌点的总数大于21则爆点,自动判输;";
cout<<"\t3.赢家可得双倍赌注;";
cout<<"\t3.计算机方在牌点大于等于16时不再要牌。";
cout<<"\t4.祝您好运!\n";
cout<<endl<<endl;
} void Judge(CCard &cpu,CCard &player)//判断输赢
{
cout<<endl;
if((cpu.GetPip()>&&player.GetPip()>) || cpu.GetPip() == player.GetPip())
{
cout<<"\n\n平局!\n";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Draw();
cout<<endl;
}
else if((cpu.GetPip()>)||(player.GetPip()>cpu.GetPip() && player.GetPip()<=))
{
cout<<"\n\n恭喜您,您赢了!\n";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Win();
cout<<endl; }
else
{
cout<<"\n\n很遗憾,您输了!\n";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Lose();
cout<<endl; } } void PlayTurn(CCard &cpu,CCard &player)//玩一局
{
char chChoice;
int blCpu = ,blPlayer = ;
cpu.FirstPlayTwo();
player.FirstPlayTwo();
do
{
cout<<"您的牌点:\t";
player.DisplayPip();
cout<<"计算机牌点:\t";
cpu.DisplayPip();
cout<<"您的牌点数是:"<<player.GetPip()<<endl;
if(blPlayer)
{
cout<<"\n\n您是否继续要牌(Y/N)";
cin >>chChoice;
if((chChoice == 'Y'||chChoice == 'y'))
{
if(player.GetNumber()<)
{
player.TurnPlay();
cout<<"这是您想要的牌:"<<player.GetCurrentCard()<<endl;
if(player.GetPip()>)
blPlayer = ;
}
else
{
cout<<"对不起,您已经要了5张牌了,不能在要牌了!";
blPlayer = ;
}
}
} if((chChoice =='N') ||(chChoice == 'n'))
{
blPlayer = ;
} if(cpu.GetPip()< && cpu.GetNumber()<)
{
cpu.TurnPlay();
cout<<"计算机要牌,牌点是:"<<cpu.GetCurrentCard()<<endl;
}
else
blCpu = ; if(blCpu&&player.GetNumber()<&&player.GetPip()<)
blPlayer = ; }while(blCpu||blPlayer);
Judge(cpu,player);
}
int main()
{
srand((unsigned )time(NULL));//初始化随机数种子
CCard cpu,player;
int blLogic;
int nMoney;
DisplayRule();//打印规则
char chChoice;
cout<<"是否开始游戏(Y/N)?";
cin >>chChoice;
while(chChoice == 'Y'||chChoice =='y')
{
do
{
cout<<"您现在有赌本:$"<<player.GetMoney();
cout<<"\n请下注(赌注不能超过赌本)";
cin>>nMoney;
blLogic = player.SetGamble(nMoney);
if(blLogic)
cout<<"您的赌本不够,请重新下注!\n"; }
while(blLogic);
PlayTurn(cpu,player);
cout<<"是否继续21点游戏(Y/N)?\n";
cin>>chChoice;
}
player.DisplayInfo();
cout<<"\n\n您是明智的,赌博有碍家庭和睦!\n";
cout<<"欢迎再次使用本程序\n\n\n";
return ; }

本文链接地址:http://www.cnblogs.com/fengdashen/p/3894961.html