21点游戏(套模板的魔改板)
#include<cstring> #include<iostream> #include<ctime> #include<cstdlib> #include<stdio.h> using namespace std; int gCard[52]; int num_card; struct sCard { int naPip[5]; //一共5张牌 int nNumber; //发了多少张牌 int nDollar; //有多少钱 int nGamble; //赌注 int nWin; //赢局数 int nLose; //输局数 int nDraw; //平局数 }; void initcards(int a[]); //初始化牌:a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,...} void shuffle(int a[]); //洗牌 void DisplayRule(); //printf游戏规则 void init(sCard &); //初始化结构体sCard int GetMoney(sCard sc); //返回nDollar int SetGamble(sCard &,int); //设置赌本,赌本不够返回-1 void DisplayInfo(sCard); //打印必要的信息 void FirstPlayTwo(sCard &cpu, sCard &player); //最初两张牌 void DisplayPip(sCard); //依次全部显示牌面点数 void DisplayPip_C(sCard); //除了第一张牌,依次全部显示牌面点数(针对计算机牌的显示) void PlayTurn(sCard &,sCard &); //开始一局游戏 int GetNumber(sCard); //返回牌数 void TurnPlay(sCard &cpu, sCard &player, bool flag); //要一张牌,flag为true时player要牌,flag为false时cpu要牌 int GetCurrentCard(sCard); //返回当前牌点 void Draw(sCard &); //平局 void Win(sCard &); //嬴 void Lose(sCard &); //输 int GetPip(sCard &); //返回点数 void Judge(sCard &,sCard &); int main() { srand((unsigned)time(NULL)); sCard cpu, player; init(cpu);///玩家初始化 init(player);///电脑初始化 int blLogic; int nMoney; DisplayRule();///游戏规则 char chChoice; cout<<"是否现在开始游戏(Y/N)?\n"; cin>>chChoice; while(chChoice=='Y'||chChoice=='y') { num_card=0; do { cout<<"您现在有赌本:$"<<GetMoney(player); cout<<"\n请下注(赌注不能超过赌本):"; cin>>nMoney; blLogic = SetGamble(player, nMoney); if(blLogic) cout<<"您的赌本不够,请重新下注!\n"; } while(blLogic);///直到输入正确的赌本为止 PlayTurn(cpu,player); cout<<"是否继续21点游戏(Y/N)?\n"; cin>>chChoice; } DisplayInfo(player); cout<<"\n\n您是明智的,赌博是不好的!回去好好学习去~!\n"<<endl<<endl; cout<<"欢迎再次使用本程序,该程序由蒲大人抄录!\n\n"; return 0; } void initcards(int a[52])///初始化牌 { int i,j,k; for(i=0; i<4; i++) { for(j=0; j<13; j++) { gCard[k++]=j+1; } } } void shuffle(int a[])///洗牌函数 { int x[4][13],i,j,num,k; int b[52]= {0}; srand(time(NULL)); for(i=0; i<4; i++)//用来储存扑克牌 { for(j=0; j<13; j++) { x[i][j]=j+1; } } num=0; while(1)//产生52张扑克牌 { if(num>=52) { break; } k=rand()%52; for(j=0; j<num; j++)//之前产生的扑克牌将不会再次出现,在这里只会出现0~52 { if(k==b[j]) { break; } } if(j==num) { b[num]=k; gCard[num]=x[k/13][k%13]; num++; } } return ; } void init(sCard &sc) { sc.nNumber = 0; sc.nDollar = 100;//设赌注为100块钱 for(int i = 0; i<5; i++) sc.naPip[i] = 0; sc.nGamble = 0; sc.nWin = 0; sc.nLose = 0; sc.nDraw = 0; } void DisplayRule() { printf("澳门皇家赌场正式上线了\n"); printf("先推出21点小游戏\n"); printf("游戏规则如下:\n"); printf("玩家最多可以有5张牌\n"); printf("如果牌点总数大于21则爆点,失败\n"); printf("新手,祝你好运!\n"); } int GetMoney(sCard sc)///返回赌资 { return sc.nDollar; } int SetGamble(sCard &sc,int gamble)//设置赌本 { if((sc.nDollar - gamble<0)||gamble<0)///不够赌本 { return -1; } sc.nGamble=gamble; sc.nDollar -= sc.nGamble;///玩一局都会将赌注当做押金 return 0; } void PlayTurn(sCard &cpu,sCard &player) { char chChoice; int blCpu = 1,blPlayer = 1; initcards(&gCard[52]); shuffle(&gCard[52]); FirstPlayTwo(cpu,player); do { cout<<"您的牌点:\t"; DisplayPip(player); cout<<"计算机牌点:\t"; DisplayPip_C(cpu); cout<<"您的牌点数是:"<<GetPip(player)<<endl; if(blPlayer) { cout<<"\n\n您是否继续要牌(Y/N)\n"; cin >>chChoice; if((chChoice == 'Y'||chChoice == 'y')) { if(GetNumber(player)<5) { TurnPlay(cpu,player,true); cout<<"这是您想要的牌:"<<GetCurrentCard(player)<<endl; if(GetPip(player)>21) { blPlayer = 0;///爆牌 } } else { cout<<"对不起,您已经要了5张牌了,不能在要牌了!"; blPlayer = 0; } } } if((chChoice =='N') ||(chChoice == 'n'))//按n退出 { blPlayer = 0; } if(GetPip(cpu)<16 && GetNumber(cpu)<5)///对计算机要牌的限定 { TurnPlay(cpu,player,false); cout<<"计算机要牌,牌点是:"<<GetCurrentCard(cpu)<<endl; } else { blCpu = 0; } if(blCpu&&GetNumber(player)<5&&GetPip(player)<21) blPlayer = 1; } while(blCpu||blPlayer); Judge(cpu,player);//判定胜负 } void DisplayInfo(sCard sc) { cout<<"您一共玩了"<<sc.nWin+sc.nLose+sc.nDraw<<"局,"<<"赢了"<<sc.nWin<<"局,"<<"输了"<<sc.nLose<<"局,"<<"平局"<<sc.nDraw<<"局"<<endl; cout<<"您的赌资共计有:$"<<sc.nDollar<<".\n"; } void FirstPlayTwo(sCard &cpu, sCard &player) { player.naPip[0] = gCard[num_card++]; player.naPip[1] = gCard[num_card++]; player.nNumber = 2; cpu.naPip[0] = gCard[num_card++]; cpu.naPip[1] = gCard[num_card++]; cpu.nNumber = 2; } void DisplayPip(sCard sc) { int i; for(i = 0; i<sc.nNumber; i++) cout<<sc.naPip[i]<<'\t'; cout<<endl; } void DisplayPip_C(sCard sc) { int i; cout<<"[*]"<<'\t'; for(i = 1; i<sc.nNumber; i++) cout<<sc.naPip[i]<<'\t'; cout<<endl; } int GetPip(sCard &sc) { int a = 0; int i; for(i = 0; i<sc.nNumber; i++) { if(sc.naPip[i]>=10)///10以上的控制在10 { a +=10; } else { a+=sc.naPip[i]; } } return a; } int GetNumber(sCard sc)//返回已经发的牌 { return sc.nNumber; } void TurnPlay(sCard &cpu, sCard &player, bool flag) { if(flag==true) { player.nNumber++; player.naPip[player.nNumber-1] = gCard[num_card++]; } else if(flag==false) { cpu.nNumber++; cpu.naPip[player.nNumber-1] = gCard[num_card++]; } } int GetCurrentCard(sCard sc)//返回当前的牌点 { return sc.naPip[sc.nNumber-1]; } void Judge(sCard &cpu,sCard &player) { printf("\n"); if((GetPip(cpu)>21&&GetPip(player)>21) || GetPip(cpu) == GetPip(player))///平局局面 { printf("\n\n双方平局!\n"); printf("计算机数据:\t"); DisplayPip(cpu); cout<<"牌面点数:"<<GetPip(cpu)<<endl; cout<<"\n您的数据\t"; Draw(player); cout<<endl; } else if((GetPip(cpu)>21)||(GetPip(player)>GetPip(cpu) && GetPip(player)<=21))///胜利 { printf("\n\n恭喜您,您赢了!\n"); cout<<"计算机数据:\t"; DisplayPip(cpu); cout<<"牌面点数:"<<GetPip(cpu)<<endl; cout<<"\n您的数据\t"; Win(player); cout<<endl; } else//失败 { cout<<"\n\n很遗憾,您输了!\n"; cout<<"计算机数据:\t"; DisplayPip(cpu); cout<<"牌面点数:"<<GetPip(cpu)<<endl; cout<<"\n您的数据\t"; Lose(player); cout<<endl; } } void Draw(sCard &sc)///平局局面 { sc.nDraw++; sc.nDollar+=sc.nGamble; DisplayPip(sc); if(GetPip(sc)>21) { cout<<"爆了! \n"; cout<<"牌面点数:"<<GetPip(sc)<<endl; } else { cout<<"牌面点数:"<<GetPip(sc)<<endl; } cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局 "<<sc.nDraw<<"次 "<<endl; cout<<endl<<endl; } void Win(sCard &sc) { sc.nWin++; DisplayPip(sc); cout<<"牌面点数: "<<GetPip(sc)<<endl; sc.nDollar = sc.nDollar +2*sc.nGamble; cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局"<<sc.nDraw<<"次"<<endl; cout <<endl<<endl; } void Lose(sCard &sc) { sc.nLose ++; DisplayPip(sc); if(GetPip(sc)>21) { cout<<"爆了!\n"; cout<<"牌面点数: "<<GetPip(sc)<<endl; } else { cout<<"牌面点数: "<<GetPip(sc)<<endl; } cout<<"赌本:$"<<sc.nDollar<<" 赢了"<<sc.nWin<<"次"<<"输了"<<sc.nLose<<"次 "<<"平局 "<<sc.nDraw<<"次 "<<endl; cout<<endl<<endl; }