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];//一共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];//一共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 = 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;}
void CCard::DisplayInfo(){ 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;}
void CCard::FirstPlayTwo()//获得最初的两张牌{ naPip[0] = rand()%13+1; naPip[1] = rand()%13+1; nNumber = 2;}
int CCard::GetCurrentCard()//返回已经得到的两张牌{ 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;}
void CCard::DisplayPip()//依次显示牌点数{ for(int i=0;i<nNumber;i++) cout<<naPip[i]<<"\t"; cout<<endl;}
void CCard::TurnPlay()//出一张牌{ 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;}
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;}
void CCard::DisplayPip(int n)//除了第一张牌,其他的显示{ 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;}



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

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; }
if(cpu.GetPip()<16 && cpu.GetNumber()<5) { cpu.TurnPlay(); cout<<"计算机要牌,牌点是:"<<cpu.GetCurrentCard()<<endl; } else blCpu = 0;
if(blCpu&&player.GetNumber()<5&&player.GetPip()<21) 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";
} while(blLogic); PlayTurn(cpu,player); cout<<"是否继续21点游戏(Y/N)?\n"; cin>>chChoice; } player.DisplayInfo(); cout<<"\n\n您是明智的,赌博有碍家庭和睦!\n"; cout<<"欢迎再次使用本程序\n\n\n"; return 0;
}


关注公众号【技术让梦想更伟大】,获取更多Linux/C/C++/Python/FPGA等原创技术文章。后台免费获取经典电子书籍和视频资源,实时更新,原创不易,请多支持,谢谢!