1、21点游戏课程设计/*玩家最多可以要5张牌,但是如果牌的点数之和超过21,则自动出局,在不超过21点的情况下,玩与庄家比牌的点数大小,大者为赢家。 */#include #include #include using namespace std;class CCardprivate: int naPip5; /定义一个数组,一共是5张牌。 int nNumber; /发了多少张牌。 int nDollar; /有多少钱 int nGamble; /赌注 int nWin; /赢局数 int nLose; /输局数 int nDraw; /输局数public: CCard(); /构造函数 vo
2、id 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();
3、/打印必要的信息 int GetCurrentCard();/返回当前牌点。 ; CCard:CCard() nNumber=0;/开始没有发牌 nDollar=100;/初始赌本为0 for(int i=0;i5;i+) naPipi=0; /五张牌全部为0 nGamble=0; nWin=nLose=nDraw=0; /没输没赢没有平局 int CCard:getMoney()return nDollar;void CCard:DisplayInfo()/打印相关信息。 cout您一共玩了nWin+nLose+nDraw局endl; cout其中输了nWin局endl; cout赢了nLo
4、se局endl; cout平了nDraw局endl; cout您还有赌本nDollarendl; int CCard:setGamble(int gamble) if(nDollar-gamble0) return -1; if(gamble0) if(nDollar-200) return -1; nGamble=20; else nGamble=gamble; nDollar-=nGamble; return 0;void CCard:FirstPlayTwo() naPip0=rand()%13+1; naPip1=rand()%13+1; nNumber=2; /现在有两张牌 int
5、CCard:GetCurrentCard()return naPipnNumber-1; int CCard:GetNumber() /返回牌数 return nNumber;int CCard:GetPip() /返回点数 int nPip=0; for(int i=0;i=10) nPip=nPip+10; else nPip+=naPipi; return nPip;void CCard:DisplayPip() /依次显示牌面点数 for(int i=0;inNumber;i+) coutnaPipit; coutendl; void CCard:TurnPlay() /出一张牌面 n
6、Number+; naPipnNumber-1=rand()%13+1;void CCard:Win() cout赢家牌面; DisplayPip(); cout牌面点数GetPip()endl; nDollar=nDollar+2*nGamble; nWin+; cout赌本:$nDollar 赢了nWin次 输了 nLose次 平局nDraw次 endl; coutendlendlendl;void CCard:Lose() nLose+; cout21) cout暴了!endl; else cout牌面点数:GetPip()endl; cout赌本:$nDollar 赢了nWin次 输了
7、 nLose次 平局nDraw次 endl; coutendlendlendl;void CCard:Draw() nDraw+; nDollar+=nGamble; cout平局牌面21) cout暴了!endl; else cout牌面点数:GetPip()endl; cout赌本:$nDollar 赢了nWin次 输了 nLose次 平局nDraw次 endl; coutendlendlendl;void CCard:DisplayPip(int n) cout*t; cout*t; for(int i=2;inNumber;i+) coutnaPipit; coutendl;/非成员函
8、数区域void DisplayRule() coutt欢迎进入21点游戏世界!nnendl; coutt游戏规则n; coutt玩家最多可以要五张牌n; coutt如果牌的总点数超过21点则自动判输n; coutt赢家可得双倍赌注n; coutt计算机在大于等于16点时不要牌n; coutt朱文丽祝您好运!n; coutendlendl;void Judge(CCard &cpu,CCard &player) cout21&player.GetPip()21)|cpu.GetPip()=player.GetPip() coutnn平局n!; cout计算机数据:t; cpu.DisplayPi
9、p(); cout牌面点数:cpu.GetPip()endl; coutn您的数据t; player.Draw(); cout21|(player.GetPip()cpu.GetPip()&player.GetPip()=21) coutnn恭喜您,您赢了n!; cout计算机数据:t; cpu.DisplayPip(); cout牌面点数:cpu.GetPip()endl; coutn您的数据t; player.Win(); coutendl; else coutnn很遗憾,您输了n!; cout计算机数据:t; cpu.DisplayPip(); cout牌面点数:cpu.GetPip()
10、endl; coutn您的数据t; player.Lose(); coutendl; void playTurn(CCard &cpu,CCard &player) char chChoice; int blCpu=1,blPlayer=1; cpu.FirstPlayTwo(); do cout您的牌点为:tendl; player.DisplayPip(); cout计算机的牌点为endl; cpu.DisplayPip(1); cout您的牌面点数是player.GetPip()endl; if(blPlayer) coutnn您是否继续要牌?(Y/N)chChoice; if(chCh
11、oice=Y|chChoice=y) if(player.GetNumber()5) player.TurnPlay(); cout您要的这张牌是player.GetCurrentCard()21) blPlayer=0; else cout对不起,您已经要了五张牌了!endl; 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(
12、blCpu&player.GetNumber()5&player.GetPip()21) blPlayer=1; while(blCpu|blPlayer); Judge(cpu,player);int main(int argc, char *argv) srand(unsigned)time(NULL); CCard cpu,player; /人和计算机赌 int blLogic; int nMoney; /下注的数目 DisplayRule();/打印规则 char chChoice; coutchChoice; while(chChoice=Y|chChoice=y) do cout您
13、现在有的赌本:player.getMoney()endl; cout请下注:(赌注不能超过赌本)nMoney; blLogic=player.setGamble(nMoney); if(blLogic) cout您的赌本不够!请重新下注:endl; while(blLogic); /注意do while的使用,后面有分号。 playTurn(cpu,player); /玩一局 cout是否继续玩游戏(Y/N)chChoice; player.DisplayInfo();/显示赌完之后的结果。 cout您的选择是明智的,赌博有害身体健康!endl; cout我们不欢迎你再次使用本程序!endl; return 0;