#include<iostream.h>#include<stdlib.h>#include<conio.h>#include<time.h>#include<iostream.h>#include<string.h>class Game{public:Game(){win=0,lose=0,draw=0,playerturns=0,cputurns=0,money=100,bet=0;player=0,cpu=0;char answer1[]="123456";strcpy(answer,answer1);char answer2[]="654321";strcpy(daan,answer2);};void rules();//显示规则void BET(); //下注void deal();//交易void run(Game &);//进行异常处理void hit1();//拿下一牌void hit2();//确保庄家不会爆庄并且点数大于16void print();//打印出最后结果void results();//最后的胜利结果void check();//检查是否爆庄Game operator=(Game &d);//运算符重载void replay(char &ans);//是否进行新一轮的游戏void clean();//清屏void wait(int);void pause();int GetInt(int,int);int random(long,long);private:int playerturns,cputurns,money;int win,lose,draw,bet;float player,cpu;char answer[7];char daan[7];};//---------------------------------------------------------------------------------------void Game::wait(int milli){clock_t start;start=clock();while( ( clock()-start ) < milli );}//========================================================= void Game:: pause(){cout<<"Press Any Key To Continue"<<endl;getch();}//=========================================================int Game::GetInt(int L=0,int H=100){int ent;cout<<"Please Enter A Number Between "<<L<<" and "<<H<<endl;cin>>ent;while((ent<L)||(ent>H)){cout<<"Error"<<endl;cout<<"Value must be between "<<L<< " and "<<H<<endl;cin>>ent;}return(ent);}//==========================================================int Game::random(long hi,long lo)//随机抽牌{int ran;srand((unsigned)time(NULL));ran=rand()%(hi-(lo-1))+lo;return(ran);}//============================================================= void Game::rules(){cout<<"\t\t\t *********欢迎使用21点游戏软件*********"<<endl;cout<<endl;cout<<endl;cout<<"\t\t\t 游戏规则:"<<endl;cout<<endl;cout<<"\t\t\t ※①:每轮游戏你最多只可以有5牌。
"<<endl;cout<<"\t\t\t ※②:如果你的点数超过21将输掉这轮游戏。
"<<endl;cout<<"\t\t\t ※③:高级玩家可以看下一牌。
"<<endl;cout<<"\t\t\t ※④:胜利者将得到2倍的注金。
"<<endl;cout<<"\t\t\t ※⑤:庄家牌点数不会小于16点。
"<<endl;cout<<"\t\t\t";pause();}//---------------------------------------------------------------------------------------void Game::BET()//下注{system("cls");//执行系统命令clscout<<"\t\t\t你的资本:$"<<money<<endl;cout<<"\t\t\t你想下多少赌注?: ";cin>>bet;if(bet<=0){cout<<"对不起,输入有误";exit(0);}if(money-bet<0){cout<<"对不起,你没有那么多钱!:("; //sees if your brokeexit(0);}money-=bet;}//----------------------------------------------------------------------------------------void Game::deal()//发前两牌{int playercard1,playercard2,cpucard1,cpucard2;float ccard1,ccard2,pcard2,pcard1;playerturns=2;//开始给玩家发两牌cputurns=2;//庄家也发两牌playercard1=random(13,1);//在1到13中找出一个随机数cout<<"\n\t\t\t正在洗牌,请稍候......"<<endl;wait(500);playercard2=random(13,1);wait(500);cout<<"["<<playercard1<<"]";cout<<"["<<playercard2<<"]";if(playercard1>10)//大于十的牌点数全部置为0.5{pcard1=0.5,player=pcard1,playercard1=0;}if(playercard2>10)//大于十的牌点数全部置为0.5{pcard2=0.5,player+=pcard2,playercard2=0;}player+=playercard1+playercard2;//总点数cout<<"\t\t\t你现在的点数为:"<<player<<endl;cout<<endl;cpucard1=random(13,1);//调用函数wait(350);cpucard2=random(13,1);//调用函数cout<<"\t\t\t庄家有一"<<cpucard1<<" 为正面牌!"<<endl;cout<<"[*] "<<" ["<<cpucard1<<"]";if(cpucard1>10)//大于十的牌点数全部置为0.5{ccard1=0.5,cpu=ccard1,cpucard1=0;}if(cpucard2>10)//大于十的牌点数全部置为0.5{ccard2=0.5,cpu+=ccard2,cpucard2=0;}cpu+=cpucard1+cpucard2;//总的点数cout<<endl;}//----------------------------------------------------------------------------------------void Game::run(Game &c2)//进行异常处理{char ans;do{int m=0;system("cls");//执行系统命令clsc2.clean();//进行新的一轮游戏!c2.check();//检验是否爆庄c2.BET(); //进行下注c2.deal(); //洗牌do{if(playerturns==5)cout<<"已达最大牌数,不能再要牌了,请按任意键看你的总的点数!"<<endl;do{try{ //开始捕获异常处理cout<<"\t\t\t你愿意继续吗?(hit愿意,stay否):";m=0; //可以没有的!但把下面的if语句改一下就必须有了cout<<flush;//刷新流cin>>ans;cin.ignore(20,'\n');//把输入行中的其余字符取空,以便后面的输入从新一轮开始if((ans=='h')||(ans=='H')||(ans=='s')||(ans=='S')) break;else throw ans;//产生异常事件}catch(char)//开始处理异常机制{cout<<"你的输入有误!:(请重新输入"<<endl;m=1;}catch(...){cout<<"你的输入有误!:(请重新输入"<<endl;m=2;}}while(m!=0);if((ans=='h')||(ans=='H')){playerturns++;if(playerturns>5){cout<<"\t\t\t你不能多于5牌!"<<endl;}}if((playerturns<6)&&(ans=='h')){cout<<endl;c2.hit1();}}while((ans=='h')||(ans=='H'));for(;(cpu<16)&&(cputurns<6);cputurns++){cout<<endl;cout<<"\t\t\t庄家得到一牌!"<<endl;c2.hit2(); //calls hit function}cout<<endl;cout<<endl;cout<<"\t\t\t庄家的点数为:"<<cpu<<endl; //显示庄家的总点数cout<<"\t\t\t你的点数为:"<<player<<endl;//显示玩家的总点数cout<<endl;c2.results(); //看你是否赢了c2.replay(ans);}while((ans=='y')||(ans=='Y'));}void Game::hit1()//取下一并把它加入总和{int card=0,copycard=0,bet1=0;float cards=0;card=random(13,1);copycard=card;char ans,s1[100];cout<<"如果您是普通玩家请按n."<<endl;cout<<"如果您是高级玩家或超级玩家请按y."<<endl;cin>>ans;label1: if(ans=='y'||ans=='Y'){cin.ignore(20,'\n');//把输入行中的其余字符取空,以便后面的输入从新一路轮开始cout<<"请输入特别玩家的6位密码:";cin.getline(s1,7);//提取密码if(!strcmp(s1,answer)){cout<<"\t\t\t这牌是:"<<copycard<<endl;cout<<"是否加注(y or n)?\n";cin>>ans;if(ans=='y'||ans=='Y'){cout<<"你的现金还有$"<<money<<endl;cout<<"你要加多少注?(你也可以不加)";cin>>bet1;if(money-bet1<0){cout<<"你没有足够的赌注";}money=money-bet1;bet+=bet1;cout<<"\t\t\t你的总点数为:"<<player<<endl;}else{if(cards==0.5)player-=cards;else player-=copycard;cout<<"\t\t\t你的总点数为:"<<player<<endl;}}else{if(strcmp(s1,daan)==0){cout<<endl;cout<<"现在计算机的总牌点为:"<<cpu<<endl;cout<<"您的总牌点为:"<<player<<endl;float card;card=(float)random(13,1);if(card>10) card=0.5;cout<<"下牌面是:"<<"["<<card<<"]"<<endl;cout<<"您想要什么牌?请输入牌点数:";cin>>card;if(card>10) card=0.5;cout<<endl;cout<<"您要的牌为"<<"["<<card<<"]"<<endl;player=player+card;//牌点总数cout<<"总的牌面是:"<< player<<endl;//输出总点数cout<<endl;}else{cout<<"对不起,你输入的密码错误!不能看牌"<<endl;cout<<"是否再次输入密码(yes or no)?"<<endl;cin>>ans;if(ans=='y'||ans=='Y')goto label1;else{cout<<"\t\t\t总点数是:"<<player<<endl;}}}}else{if(card>10){cards=0.5,card=0;};player+=cards+card;cout<<"\t\t\t总点数为:"<<player<<endl;}}//-----------------------------------------------------------------void Game::hit2()//确保庄家不会爆庄并且点数大于16{int card=0,copycard=0;float cards=0;card=random(13,1);//调用函数copycard=card;cout<<"这牌是:"<<card;if(card>10){cards=0.5,card=0;};cpu=cpu+cards+card;if(cpu>21){if(cards==0)cpu-=copycard;//当card小于10但总数大于21时执行else cpu-=cards; //当card大于10且点数大于21时执行cout<<"\t\t\t总点数为:"<<cpu<<endl;}if(cpu<16)//确保庄家点数大于等于16{cpu=16;cout<<"\t\t\t总点数是:"<<cpu<<endl;}}//---------------------------------------------------------------------void Game::results()//找出赢家{if(cpu==player){cout<<"\t\t\t点数一样,你们平了!"<<endl;draw++;money+=bet;}if(player>21){cout<<"\t\t\t啊哦,你爆了!:("<<endl;lose++;}else{if(cpu<player){cout<<"\n\t\t\t恭喜你,你赢了!:)";money=money+(bet*2);win++;}}if(cpu>21){cout<<"\t\t\t恭喜你,庄家爆了!:)"<<endl;if(player<21){cout<<"\n\t\t\t恭喜你,你赢了!:) ";win++;money=money+(bet*2);}}else{if(cpu>player){cout<<"\t\t\t不好意思,你输了!:("<<endl;lose++;}}}//---------------------------------------------------------------------Game Game::operator=(Game &d)//保留上一轮的一些数据{Game t;t.win+=d.win,t.lose+=d.lose,t.draw=+d.draw,t.playerturns=0,t.cputurns=0,t.money+ =d.money,t.player=0,t.cpu=0;return t;}//---------------------------------------------------------------------void Game::clean()//清零{win=this->win,lose=this->lose,draw=this->draw,playerturns=0,cputurns=0,money=th is->money,bet=0,player=0,cpu=0;}//---------------------------------------------------------------------void Game::replay(char &ans)//是否继续游戏{cout<<"\n\t\t\t你是否想继续游戏(yes or no)?";cin>>ans;}//---------------------------------------------------------------------void Game::check(){if(money<=0) //检查钱的多少{cout<<"对不起,你输光了,你不能玩了!:("<<endl; //sees if your broke }}//---------------------------------------------------------------------void Game::print()//打印最后结果{void sign();cout<<"\t\t\t\t赢:"<<win<<endl;cout<<"\t\t\t\t负:"<<lose<<endl;cout<<"\t\t\t\t平:"<<draw<<endl;cout<<"\t\t\t\t余额:"<<money<<endl;cout<<"\t\t\t\t使用"<<endl;if(money<=-1000)cout<<"\t\t\t你输的真惨啊!:("<<endl;}//---------------------------------------------------------------------int main(){Game c2;char ans;system("cls"); //执行系统命令clsc2.rules();//调用函数cout<<"\t\t\t游戏可以开始了吗?(yes or no):";cin>>ans;. . . .if((ans == 'y')||(ans=='Y')) //看你是否想要进行游戏{cout<<"\t\t\t你现在拥有的赌本为$100!"<<endl; //设定起始赌金//cout<<"\t\t\t"<<flush;//刷新流// pause();}else //如果否退出游戏{return(0);}c2.run(c2);//进行游戏c2.print();//输出最后结果cout<<endl;cout<<"\t\t\t\t";return(0);}. 资料. .. .。