24点游戏C语言

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

class CCard{private: int m_Pip[5];//一***五张牌 int m_Number;//发了多少张牌 int m_Dollar;//赌本 int m_Gamble;//赌注 int m_Win;//赢局数 int m_Lose;//输局数 int m_Draw;//平局数public: CCard();//构造函数。 void FirstPlayTwo();//最初的两张牌 int GetNumber();//返回牌张 int GetPip();//返回点数 void DisplayPip();//依次全部显示牌面的点数 void DisplayPip(int);//除了第一张牌,依次显示全部牌面点数(针对计算机牌的显示) void TurnPlay();//出一张牌。 void Win();//赢了计算赌注 void Lose();//输了 void Draw();//平局 int SetGamble(int);//设置赌本,赌本不够返回-1 int GetMoney();//返回钱数 void DisplayInfo();//打印必要的信息 int GetCurrentCard();//返回当前的牌点};

CCard::GetNumber(){ return m_Number;}

CCard::CCard()//构造函数,初始化{ m_Number = 0; m_Dollar = 100;//初始赌注为100美元 for(int i=0;i<5;i++) m_Pip[i] = 0; m_Gamble = 0; m_Win = m_Lose = m_Draw = 0;}

int CCard::GetMoney(){ return m_Dollar;}

void CCard::DisplayInfo()//打印必要的信息{ cout<<"\n\n\n\t\t\t您一***玩了"<<m_Win+m_Lose+m_Draw<<"局 "<<"赢了"<<m_Win<<"局 "<<"输了"<<m_Lose<<"局 "<<"平局"<<m_Draw<<"次。"<<endl; cout<<"\n\t\t\t\t您的赌本***计有$"<<m_Draw<<"。\n"<<endl;}

int CCard::SetGamble(int gamble){ if(gamble<0) { cout<<"\n输入金额有误"<<endl; return -1; } if(m_Dollar-gamble<0) { cout<<"\n金额不足"<<endl; return -1; } else m_Gamble = gamble; m_Dollar -= gamble; return 0;}

void CCard::FirstPlayTwo()//最初两张牌{ m_Pip[0] = rand()%13+1; m_Pip[1] = rand()%13+1; m_Number = 2;}

int CCard::GetPip(){ int SumPip = 0; for(int i=0;i<m_Number;i++) { SumPip += m_Pip[i]; } return SumPip;}

void CCard::DisplayPip(){ int i; for(i=0;i<m_Number;i++) { cout<<m_Pip[i]<<'\t'; } cout<<endl;}

void CCard::TurnPlay(){ m_Number++; m_Pip[m_Number-1] = rand()%13+1;}

void CCard::Win(){ cout<<"赢家牌面:"; DisplayPip(); cout<<"\n牌面点数:"<<GetPip()<<endl; m_Dollar = m_Dollar + 2 * m_Gamble; m_Win++; cout<<"\n赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl; cout<<endl;}

void CCard::Lose(){ m_Lose++; cout<<"\n输家的牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\t\t\t\t\t\t\t\t暴了!"<<endl; else cout<<"牌面点数:"<<GetPip()<<endl; cout<<"\n赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}

void CCard::Draw(){ m_Draw++; m_Dollar += m_Gamble; cout<<"\n平局牌面:"; DisplayPip(); if(GetPip()>21) cout<<"\n暴了!"<<endl; else cout<<"牌面点数:"<<GetPip()<<endl; cout<<"赌本:$"<<m_Dollar<<" 赢了"<<m_Win<<"次 "<<"输了"<<m_Lose<<"次 "<<"平局"<<m_Draw<<"次"<<endl; cout<<endl<<endl;}

void DisplayRule(void){ cout<<endl<<endl; cout<<"\t※※※※※※※※※※欢迎进入21点游戏世界!※※※※※※※※※※\n\n"; cout<<"\t\t 游戏规则:\n"; cout<<endl; cout<<"\t\t 1.玩家最多可以要5张牌;\n"; cout<<endl; cout<<"\t\t 2.如果牌点数的总数超过21点则暴点,自动判数;\n"; cout<<endl; cout<<"\t\t 3.赢家可得双倍的赌注;\n"; cout<<endl; cout<<"\t\t 4.计算机方在大于等于16点时不再要牌。\n"; cout<<endl; cout<<"\t※※※※※※※※※※※※※ 祝您好运! ※※※※※※※※※※\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\t\t\t\t\t\t\t\t平局!\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\t\t\t\t\t\t\t\t恭喜您赢了!\n\n"; cout<<"计算机数据:\t"; cpu.DisplayPip(); cout<<"牌面点数:"<<cpu.GetPip()<<endl; cout<<"\n您的数据:\t"; player.Win(); cout<<endl; } else { cout<<"\n\n\t\t\t\t\t\t\t\t很遗憾您输了!\n"; cout<<"计算机数据:\t"; cpu.DisplayPip(); cout<<"牌面点数:"<<cpu.GetPip()<<endl; cout<<"\n您的数据:\t"; player.Lose(); cout<<endl; }}void CCard::DisplayPip(int n){ int i; cout<<"[*]"<<'\t'; for(i=1;i<m_Number;i++) cout<<m_Pip[i]<<'\t'; cout<<endl;}

void PlayTurn(CCard &cpu,CCard & player)//玩一局{ char chChoice; int blCpu = 1;//判断是否要牌 int blPlayer = 1; cpu.FirstPlayTwo();//计算机和玩家各要两张牌 player.FirstPlayTwo(); do { cout<<"\n您的牌点为:\t"; player.DisplayPip(); cout<<"\n"; cout<<"您的牌面点数是:"<<player.GetPip()<<endl; cout<<"\n计算机的牌点为:\t"; cpu.DisplayPip(1); if(blPlayer) { cout<<"\n\n\t您是否继续要牌(Y/N)?\t\t\t"; cin>>chChoice; if((chChoice == 'Y'||chChoice == 'y')) { if(player.GetNumber()<5) { player.TurnPlay(); cout<<"\n您要的这张牌是:"<<player.GetPip()<<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<<"\n计算机要牌,牌点是:"<<cpu.GetPip()<<endl; } else blCpu = 0; if(blCpu&&player.GetNumber()<5&&player.GetPip()<21) blPlayer = 1; }while(blCpu||blPlayer); Judge(cpu,player); return ;}

int main(){ srand((unsigned)time(NULL));//初始化随机数种子 CCard cpu,player; int blLogic; int nMoney;// DisplayRule();// char chChoice; cout<<"是否现在开始游戏(Y/N)?\t\t"; cin>>chChoice; while(chChoice=='Y'||chChoice=='y') { do { cout<<endl; cout<<"\t\t\t您现在有的赌本:$"<<player.GetMoney(); cout<<"\n\n请下注(赌注不能超过赌本);"; cin>>nMoney; blLogic = player.SetGamble(nMoney); if(blLogic) cout<<"您的赌本不够,请重新下注!\n"; }while(blLogic); PlayTurn(cpu,player);// cout<<"是否继续21点游戏(Y/N)?\t\t\t"; cin>>chChoice; } player.DisplayInfo(); cout<<"\t\t\t您的选择是明智的,赌博有碍家庭和睦!\n"; cout<<"\n\n\t\t\t\t欢迎再次使用此程序!"<<endl<<endl<<endl;

return 0;}