當前位置:遊戲中心平台 - 遊戲盒子 - 24點遊戲C語言

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

  • 上一篇:應聘遊戲策劃中的作品指的到底是什麽?
  • 下一篇:小班七彩樂園教案
  • copyright 2024遊戲中心平台