/*该程序是模拟21点扑克牌游戏,玩家最多可以要5张牌,
但是如果牌的点数之和超过21,则自动出局,在不超过21点
的情况下,玩与庄家比牌的点数大小,大者为赢家。
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class CCard
{
private:
int naPip[5]; //定义一个数组,一共是5张牌。
int nNumber; //发了多少张牌。
int nDollar; //有多少钱
int nGamble; //赌注
int nWin; //赢局数
int nLose; //输局数
int nDraw; //输局数
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::CCard()
{
nNumber=0;//开始没有发牌
nDollar=100;//初始赌本为0
for(int i=0;i<5;i++)
naPip[i]=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<<"赢了"<<nLose<<"局"<<endl;
cout<<"平了"<<nDraw<<"局"<<endl;
cout<<"您还有赌本"<<nDollar<<endl;
}
int CCard::setGamble(int gamble)
{
if(nDollar-gamble<0)
return -1;
if(gamble<0)
{
if(nDollar-20<0)
return -1;
nGamble=20;
}
else
nGamble=gamble;
nDollar-=nGamble;
return 0;
}
void CCard::FirstPlayTwo()
{
naPip[0]=rand()%13+1;
naPip[1]=rand()%13+1;
nNumber=2; //现在有两张牌
}
int CCard::GetCurrentCard()
{return naPip[nNumber-1];}
int CCard::GetNumber() //返回牌数
{return nNumber;}
int CCard::GetPip() //返回点数
{
int nPip=0;
for(int i=0;i<nNumber;i++)
{if(naPip[i]>=10)
nPip=nPip+10;
else
nPip+=naPip[i];}
return nPip;
}
void CCard::DisplayPip() //依次显示牌面点数
{
for(int i=0;i<nNumber;i++)
cout<<naPip[i]<<"\t";
cout<<endl;
}
void CCard::TurnPlay() //出一张牌面
{
nNumber++;
naPip[nNumber-1]=rand()%13+1;
}
void CCard::Win()
{
cout<<"赢家牌面";
DisplayPip();
cout<<"牌面点数"<<GetPip()<<endl;
nDollar=nDollar+2*nGamble;
nWin++;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次 "<<"输了"
<<nLose<<"次 "<<"平局"<<nDraw<<"次 "<<endl;
cout<<endl<<endl<<endl;
}
void CCard::Lose()
{
nLose++;
cout<<"输家牌面:";
DisplayPip();
if(GetPip()>21)
cout<<"暴了!"<<endl;
else
cout<<"牌面点数:"<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次 "<<"输了"
<<nLose<<"次 "<<"平局"<<nDraw<<"次 "<<endl;
cout<<endl<<endl<<endl;
}
void CCard::Draw()
{
nDraw++;
nDollar+=nGamble;
cout<<"平局牌面"<<endl;
DisplayPip();
if(GetPip()>21)
cout<<"暴了!"<<endl;
else
cout<<"牌面点数:"<<GetPip()<<endl;
cout<<"赌本:$"<<nDollar<<" 赢了"<<nWin<<"次 "<<"输了"
<<nLose<<"次 "<<"平局"<<nDraw<<"次 "<<endl;
cout<<endl<<endl<<endl;
}
void CCard::DisplayPip(int n)
{
cout<<"[*]"<<"\t";
cout<<"[*]"<<"\t";
for(int i=2;i<nNumber;i++)
cout<<naPip[i]<<"\t";
cout<<endl;
}
//非成员函数区域
void DisplayRule()
{
cout<<"\t欢迎进入21点游戏世界!\n\n"<<endl;
cout<<"\t游戏规则\n";
cout<<"\t玩家最多可以要五张牌\n";
cout<<"\t如果牌的总点数超过21点则自动判输\n";
cout<<"\t赢家可得双倍赌注\n";
cout<<"\t计算机在大于等于16点时不要牌\n";
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平局\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!";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Win();
cout<<endl;
}
else
{
cout<<"\n\n很遗憾,您输了\n!";
cout<<"计算机数据:\t";
cpu.DisplayPip();
cout<<"牌面点数:"<<cpu.GetPip()<<endl;
cout<<"\n您的数据\t";
player.Lose();
cout<<endl;
}
}
void playTurn(CCard &cpu,CCard &player)
{
char chChoice;
int blCpu=1,blPlayer=1;
cpu.FirstPlayTwo();
do
{
cout<<"您的牌点为:\t"<<endl;
player.DisplayPip();
cout<<"计算机的牌点为"<<endl;
cpu.DisplayPip(1);
cout<<"您的牌面点数是"<<player.GetPip()<<endl;
if(blPlayer)
{
cout<<"\n\n您是否继续要牌?(Y/N)"<<endl;
cin>>chChoice;
if((chChoice=='Y'||chChoice=='y'))
{ if(player.GetNumber()<5)
{ player.TurnPlay();
cout<<"您要的这张牌是"<<player.GetCurrentCard()<<endl;
if(player.GetPip()>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(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;
cout<<"是否开始游戏 (Y/N)?\n";
cin>>chChoice;
while(chChoice=='Y'||chChoice=='y')
{
do
{
cout<<"您现在有的赌本:"<<player.getMoney()<<endl;
cout<<"请下注:(赌注不能超过赌本)"<<endl;
cin>>nMoney;
blLogic=player.setGamble(nMoney);
if(blLogic)
cout<<"您的赌本不够!请重新下注:"<<endl;
}while(blLogic); //注意do while的使用,后面有分号。
playTurn(cpu,player); //玩一局
cout<<"是否继续玩游戏(Y/N)"<<endl;
cin>>chChoice;
}
player.DisplayInfo();//显示赌完之后的结果。
cout<<"\\您的选择是明智的,赌博有害身体健康!"<<endl;
cout<<"我们不欢迎你再次使用本程序!"<<endl;
return 0;
}