实例介绍
【实例简介】二十一点 控制台小游戏
【实例截图】不要在玩时打n,会循环
【核心代码】
int CCard::GetNumber()//返回已经发的牌数
{
return nNumber;
}
int CCard::GetPip()//返回当前点数
{
int nPip = 0;
for(int i = 0;i<nNumber;i )
{
if(naPip[i]>=10)
{
nPip =10;
}
void Judge(CCard &cpu,CCard &player)//判断输赢
{
cout<<endl;
if((cpu.GetPip()>21&&player.GetPip()>21) || 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()>21)||(player.GetPip()>cpu.GetPip() && player.GetPip()<=21))
{
cout<<"\n\n恭喜您,您赢了!\n";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Win();
cout<<endl;
void PlayTurn(CCard &cpu,CCard &player)//玩一局
{
char chChoice;
int blCpu = 1,blPlayer = 1;
cpu.FirstPlayTwo();
player.FirstPlayTwo();
do
{
cout<<"您的牌点:\t";
player.DisplayPip();
cout<<"计算机牌点:\t";
cpu.DisplayPip(1);
cout<<"您的牌点数是:"<<player.GetPip()<<endl;
if(blPlayer)
{
cout<<"\n\n您是否继续要牌(Y/N)";
cin >>chChoice;
if((chChoice == 'Y'||chChoice == 'y'))
{
if(player.GetNumber()<5)
{
player.TurnPlay();
cout<<"这是您想要的牌:"<<player.GetCurrentCard()<<endl;
if(player.GetPip()>21)
blPlayer = 0;
}
else
{
cout<<"对不起,您已经要了5张牌了,不能在要牌了!";
blPlayer = 0;
}
}
}
【实例截图】不要在玩时打n,会循环

【核心代码】
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
class CCard
{
private:
int naPip[5];//一共5张牌
int nNumber;//发了多少张牌
int nDollar;//有多少钱
int nGamble;//赌注
int nWin;//赢的局数
int nLose;//输的局数
int nDraw;//平局数
#include<cstdlib>
#include<ctime>
using namespace std;
class CCard
{
private:
int naPip[5];//一共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();//构造函数初始化
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 = 0;
nDollar = 100;//设赌注为100块钱
for(int i = 0;i<5;i )
naPip[i] = 0;
nGamble = 0;
nWin = 0;
nLose = 0;
nDraw = 0;
{
nNumber = 0;
nDollar = 100;//设赌注为100块钱
for(int i = 0;i<5;i )
naPip[i] = 0;
nGamble = 0;
nWin = 0;
nLose = 0;
nDraw = 0;
}
int CCard::GetMoney()//
{
return nDollar;
}
int CCard::GetMoney()//
{
return nDollar;
}
void CCard::DisplayInfo()
{
cout<<"您一共玩了"<<nWin nLose nDraw<<"局,"<<"赢了"<<nWin<<"局,"<<"输了"<<nLose<<"局,"<<"平局"<<nDraw<<"局"<<endl;
cout<<"您的赌资共计有:$"<<nDollar<<".\n";
{
cout<<"您一共玩了"<<nWin nLose nDraw<<"局,"<<"赢了"<<nWin<<"局,"<<"输了"<<nLose<<"局,"<<"平局"<<nDraw<<"局"<<endl;
cout<<"您的赌资共计有:$"<<nDollar<<".\n";
}
int CCard::SetGamble(int gamble)//设赌注
{
if(nDollar - gamble<0)
return -1;
if(gamble<0)
{
if(nDollar - 20 <0)
return -1;
nGamble = 20;
}
else
nGamble =gamble;
nDollar -= nGamble;
return 0;
}
{
if(nDollar - gamble<0)
return -1;
if(gamble<0)
{
if(nDollar - 20 <0)
return -1;
nGamble = 20;
}
else
nGamble =gamble;
nDollar -= nGamble;
return 0;
}
void CCard::FirstPlayTwo()//获得最初的两张牌
{
naPip[0] = rand()%13 1;
naPip[1] = rand()%13 1;
nNumber = 2;
}
{
naPip[0] = rand()%13 1;
naPip[1] = rand()%13 1;
nNumber = 2;
}
int CCard::GetCurrentCard()//返回已经得到的两张牌
{
return naPip[nNumber-1];
}
{
return naPip[nNumber-1];
}
int CCard::GetNumber()//返回已经发的牌数
{
return nNumber;
}
int CCard::GetPip()//返回当前点数
{
int nPip = 0;
for(int i = 0;i<nNumber;i )
{
if(naPip[i]>=10)
{
nPip =10;
}
else
nPip =naPip[i];
}
return nPip;
}
nPip =naPip[i];
}
return nPip;
}
void CCard::DisplayPip()//依次显示牌点数
{
for(int i=0;i<nNumber;i )
cout<<naPip[i]<<"\t";
cout<<endl;
}
{
for(int i=0;i<nNumber;i )
cout<<naPip[i]<<"\t";
cout<<endl;
}
void CCard::TurnPlay()//出一张牌
{
nNumber ;
naPip[nNumber-1] = rand()%13 1;
{
nNumber ;
naPip[nNumber-1] = rand()%13 1;
}
void CCard::Win()//赢了计算赌资
{
cout<<"赢家牌面:";
DisplayPip();
cout<<"牌面点数: "<<GetPip()<<endl;
nDollar = nDollar 2*nGamble;
nWin ;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局"<<nDraw<<"次"<<endl;
cout <<endl<<endl;
}
void CCard::Lose()//输了计算赌资
{
nLose ;
cout<<"输家牌面:";
DisplayPip();
if(GetPip()>21)
cout<<"爆了!\n";
else
cout<<"牌面点数: "<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局 "<<nDraw<<"次 "<<endl;
cout<<endl<<endl;
}
{
cout<<"赢家牌面:";
DisplayPip();
cout<<"牌面点数: "<<GetPip()<<endl;
nDollar = nDollar 2*nGamble;
nWin ;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局"<<nDraw<<"次"<<endl;
cout <<endl<<endl;
}
void CCard::Lose()//输了计算赌资
{
nLose ;
cout<<"输家牌面:";
DisplayPip();
if(GetPip()>21)
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()>21)
cout<<"爆了! \n";
else
cout<<"牌面点数:"<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次"<<"输了"<<nLose<<"次 "<<"平局 "<<nDraw<<"次 "<<endl;
cout<<endl<<endl;
}
{
nDraw ;
nDollar =nGamble;
cout<<"平局牌面:";
DisplayPip();
if(GetPip()>21)
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 = 1;i<nNumber;i )
cout<<naPip[i]<<'\t';
cout<<endl;
}
{
cout<<"[*]"<<'\t';
for(int i = 1;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;
}
{
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()>21&&player.GetPip()>21) || 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()>21)||(player.GetPip()>cpu.GetPip() && player.GetPip()<=21))
{
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;
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 = 1,blPlayer = 1;
cpu.FirstPlayTwo();
player.FirstPlayTwo();
do
{
cout<<"您的牌点:\t";
player.DisplayPip();
cout<<"计算机牌点:\t";
cpu.DisplayPip(1);
cout<<"您的牌点数是:"<<player.GetPip()<<endl;
if(blPlayer)
{
cout<<"\n\n您是否继续要牌(Y/N)";
cin >>chChoice;
if((chChoice == 'Y'||chChoice == 'y'))
{
if(player.GetNumber()<5)
{
player.TurnPlay();
cout<<"这是您想要的牌:"<<player.GetCurrentCard()<<endl;
if(player.GetPip()>21)
blPlayer = 0;
}
else
{
cout<<"对不起,您已经要了5张牌了,不能在要牌了!";
blPlayer = 0;
}
}
}
if((chChoice =='N') ||(chChoice == 'n'))
{
blPlayer = 0;
}
{
blPlayer = 0;
}
if(cpu.GetPip()<16 && cpu.GetNumber()<5)
{
cpu.TurnPlay();
cout<<"计算机要牌,牌点是:"<<cpu.GetCurrentCard()<<endl;
}
else
blCpu = 0;
{
cpu.TurnPlay();
cout<<"计算机要牌,牌点是:"<<cpu.GetCurrentCard()<<endl;
}
else
blCpu = 0;
if(blCpu&&player.GetNumber()<5&&player.GetPip()<21)
blPlayer = 1;
blPlayer = 1;
}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";
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 0;
while(blLogic);
PlayTurn(cpu,player);
cout<<"是否继续21点游戏(Y/N)?\n";
cin>>chChoice;
}
player.DisplayInfo();
cout<<"\n\n您是明智的,赌博有碍家庭和睦!\n";
cout<<"欢迎再次使用本程序\n\n\n";
return 0;
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论