游戏系列~21点扑克牌

Posted yszhyhm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游戏系列~21点扑克牌相关的知识,希望对你有一定的参考价值。

#include<ctime>
//#include<stdio>
#include<iostream>
//#include<stdlib>
#include<conio.h>
#include <bits/stdc++.h>
using namespace std;
//定义扑克类
class game
{
private:
int wins,lose,draw,money;
int bet,playerturns,cputurns;
float player,cpu,card;//此处CARD在和BET交换数据的时候会有警告,可多定义一个INT型避免。
char flag;
public:
game();
void results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose);
void wait(int milli);
void sign();
void special(float &player,float &cpu);
void rules();
void pause();
float random(long hign,long low);
void print(int wins,int lose,int draw,int money);
void replay(char &flag);
void hit(float &total);
void results(float player,int cpu,int bet,int &money,int &draw,int &win,int &lose);
void BET(int &bet,int &money);
void deal(float &player,float &cpu,int &playerturns,int &cputurns);
void deal2(float &player,float &cpu,int &playerturns,int &cputurns);
void ask();
};
//---------------------------------------------------------------------
//完成虚构函数
game::game()
{
wins=0;
lose=0;
draw=0;
money=0;
player=0.0;
cpu=0.0;
bet=0;
playerturns=0;
cputurns=0;
card=0;
flag=‘a‘;
}
//---------------------------------------------------------------------
void game::rules()
{
cout<<"\t*********************************************************"<<endl;
cout<<"\t*\t\t欢迎玩这个扑克21点游戏\t\t\t*"<<endl;
cout<<"\t*\t\t\t规则如下\t\t\t*"<<endl;
cout<<"\t*\t\t一:你不能超过5张牌.\t\t\t*"<<endl;
cout<<"\t*\t\t二:可以追加赌注.\t\t\t*"<<endl;
cout<<"\t*\t\t三:同意按y.\t\t\t\t\*"<<endl;
cout<<"\t*********************************************************"<<endl;
cout<<"\t\t\t";
pause();//此处暂停几秒
ask();
}
//---------------------------------------------------------------------
void game::ask()
{
cout<<endl<<endl;
cout<<"\t\t\t你想玩这个游戏么?"<<endl;
cin>>flag;
if((flag==‘y‘)||(flag==‘Y‘)) //判断是否进入游戏
{
cout<<"\t\t\t你将有100元开始这个游戏"<<endl;
money=100;//设置最初赌注
cout<<"\t\t\t";
pause();
}
else exit(1);
BET(bet,money);
deal(player,cpu,playerturns,cputurns);//开始处理
}
//---------------------------------------------------------------------
void game::BET(int &bet,int &money)
{
int sign;
system("cls");
if(money<=0)
{
cout<<"\t\t\t你已经没有赌注了,请关闭后重新开始"<<endl;
exit(1);
}
cout<<"\t\t\t你现在有:"<<money<<"$赌注"<<endl;
cout<<"\t\t\t你想下注多少:";
do{ //判断输入赌注是否正确
sign=0;
cin>>bet;
if((bet<0)&&(bet*-1<=money))
{
bet=bet*-1;
break;
}
else if((bet>0)&&(bet<=money))
break;
else
{
cout<<"输入错误,请重新输入:";
sign=1;
}
}while(sign=1);
money=money-bet;
}
//---------------------------------------------------------------------
void game::deal(float &player,float &cpu,int &playerturns,int &cputurns)
{
float playercard1,playercard2,cpucard1,cpucard2;
playerturns=2;
cputurns=2;
playercard1=random(13,1); //开始利用随机数分牌
cout<<"\n\t\t\t正在处理中"<<endl;
wait(350);
playercard2=random(13,1);
wait(150);
cpucard1=random(13,1);
wait(350);
cpucard2=random(13,1);
if(playercard1>=10)
{
playercard1=0.5;
}
if(playercard2>=10)
{
playercard2=0.5;
}
if(cpucard1>=10)
{
cpucard1=0.5;
}
if(cpucard2>=10)
{
cpucard2=0.5;
}
player=playercard1+playercard2;
cpu=cpucard1+cpucard2;
cout<<"\t\t\t你的点数为:"<<player<<endl<<endl;
cout<<"你的两张牌是:";
cout<<"["<<playercard1<<"]";
cout<<"["<<playercard2<<"]";
cout<<endl;
cout<<"\t\t\t电脑有一张"<<cpucard1<<"显示"<<endl;
cout<<endl;
cout<<"电脑的牌是:";
cout<<"[*]"<<"["<<cpucard1<<"]"<<endl;
deal2(player,cpu,playerturns,cputurns);//调用发牌函数
}
//---------------------------------------------------------------------
void game::deal2(float &player,float &cpu,int &playerturns,int &cputurns)
{
do{
cout<<"\t\t\t你想继续要牌么?";
cin>>flag;
if(((flag==‘o‘)||(flag==‘O‘))&&(playerturns<5)) //判断是否高级玩家
{
do
{
card=random(13,1);
if(card>10)
{
card=0.5;
}
cout<<"\t\t\t下张牌是:"<<card<<endl; //显示下张牌
cout<<"\t\t\t你想要这张牌么?(输入p继续要牌)"<<endl;
cin>>flag;
if((flag==‘p‘)||(flag==‘P‘))
{
playerturns++;
player=player+card;
cout<<"\t\t\t你现在的点数是:"<<player<<endl;
}
if(playerturns>=5)
{
cout<<"\t\t\t\n你的牌已经到了五张,不能再要牌了"<<endl;break;
}
}while(flag==‘p‘);
}
else if((flag==‘y‘)||(flag==‘Y‘)) //此处设置普通玩家
{
playerturns++;
if(player>21)
{
cout<<"\t\t\t你暴庄了,你输了";
lose++;
replay(flag);
}
if(playerturns>5)
{
cout<<"\t\t\t你的牌已经到了五张,不能再要牌了"<<endl;break;
}
else if((playerturns<6)&&((flag==‘y‘)||(flag==‘Y‘)))
{
cout<<endl;
hit(player);
}
}
}while((flag==‘y‘)||(flag==‘Y‘));
cout<<"\t\t\t你想追加赌注么?"<<endl; //问是否需要追加赌注
cin>>flag;
if((flag==‘y‘)||(flag==‘Y‘))
{
cout<<"\t\t\t请输入你想追加的赌注:";
do{
cin>>card;
if(money==0) //判断是否能追加赌注
{
cout<<"\t\t\t你已经没有赌注了,电脑开始要牌。"<<endl;
break;
}
if((card)>money)
{
cout<<"\t\t\t你的赌注不够。"<<endl;
cout<<"\t\t\t请输入0到"<<money<<"之间的数字:"<<endl;
}
}while((card)>money);
bet=bet+card;
money=money-card;
}
for(;(cpu<16)&&(cputurns<5);cputurns++) //给电脑发牌
{
cout<<endl;
cout<<"\t\t\t电脑要了一张牌"<<endl;
hit(cpu);
if(cpu>21)
{
cpu=cpu-card;
break;
}
}
cout<<endl;
cout<<endl;
cout<<"\t\t\t电脑的牌点数为:"<<cpu<<endl;
cout<<"\t\t\t你的点数为:"<<player<<endl;
cout<<endl;
if(((player<=21)&&(cpu<=21))||(playerturns==6)||(cputurns==5)) //特殊判断
{
special(player,cpu);
}
results(player,cpu,bet,money,draw,wins,lose);
replay(flag);
print(wins,lose,draw,money);
cout<<endl;
}
//---------------------------------------------------------------------
void game::hit(float &total) //分牌函数
{
float card;
card=random(13,1);
if(card>=10)
{
card=0.5;
}
total=total+card;
cout<<"\t\t\t要的牌是:"<<card<<endl;
cout<<"\t\t\t总共的点数是:"<<total<<endl;
}
//---------------------------------------------------------------------
void game::pause()
{
cout<<"按任意键继续"<<endl;
getch();
}
//---------------------------------------------------------------------
void game::wait(int milli)
{
clock_t start; //调用TIME.H内函数
start=clock();
while((clock()-start)<milli)
;
}
//---------------------------------------------------------------------
float game::random(long high,long low) //得到随机数
{
float ran;
srand((unsigned)time(NULL));
ran= rand()%(high-(low-1))+low;
return(ran);
}
//---------------------------------------------------------------------
void game::replay(char &flag)
{
cout<<"\n\t\t\t你想继续玩这个游戏么:";
cin>>flag;
while((flag==‘y‘)||(flag==‘Y‘)) //此处设置游戏循环
{
BET(bet,money);
deal(player,cpu,playerturns,cputurns);
}
print(wins,lose,draw,money);
}
//---------------------------------------------------------------------
void game::print(int wins,int lose,int draw,int money) //输出结果
{
cout<<"\t\t\t赢:"<<wins<<endl;
cout<<"\t\t\t输:"<<lose<<endl;
cout<<"\t\t\t和:"<<draw<<endl;
cout<<"\t\t\t剩余钱数:"<<money<<endl;
sign();
}
//---------------------------------------------------------------------
void game::results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose)
{
if(cpu==player) //输出结果
{
cout<<"\t\t\t点数相同,庄家赢"<<endl;
draw++;
}
if(player>21)
{
cout<<"\t\t\t你暴庄了,加油"<<endl;
lose++;
}
else
{
if(cpu<player)
{
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
}
if(cpu>21)
{
cout<<"\t\t\t庄家加油"<<endl;
if(player<21)
{
cout<<"\n\t\t\t你赢了";
wins++;
money=money+(bet*2);
}
}
else
{
if(cpu>player)
{
cout<<"\t\t\t你输了"<<endl;
lose++;
}
}
}
//---------------------------------------------------------------------
void game::sign()
{
cout<<"\n\n\n\n\t\t\t新手第一次写,多多指教"<<endl;
exit(1);
}
//---------------------------------------------------------------------
void game::special(float &player,float &cpu)
/*如果要五张牌还不超过21点而对方没到五张牌,那就算赢.如果两个人都五张牌且都没
有超过21点,那点数小的赢.*/
{
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns<5))
{
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
if((player<=21)&&(cpu<=21)&&(playerturns<6)&&(cputurns==5))
{
cout<<"\t\t\t你输了"<<endl;
lose++;
}
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns==5))
{
if(player<cpu)
{
cout<<"\n\t\t\t恭喜,你赢了";
money=money+(bet*2);
wins++;
}
else
{
cout<<"\t\t\t你输了"<<endl;
lose++;
}
}
replay(flag);
}
//---------------------------------------------------------------------
int main()
{
game deck;
deck.rules();
}
//---------------------------------------------------------------------

以上是关于游戏系列~21点扑克牌的主要内容,如果未能解决你的问题,请参考以下文章

C# 之 扑克游戏 -- 21点规则介绍和代码实现

C# 之 扑克游戏 -- 21点规则介绍和代码实现

团队项目----基于德州扑克的数据结构分析

电影 《决胜21点》里面的算法是怎么弄的?

扑克牌游戏研究先告一段落

在OpenCV里实现扑克牌识别1