⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 21点.cpp

📁 一个21点的游戏源代码
💻 CPP
字号:
#include"iostream.h"
#include"stdlib.h"
#include"time.h"
class wwpoint{
private:
	float s;                  //每一张牌的点数
    float total;              //总点数
public:
	wwpoint();
	float get()
	{
		return total;
	};
    float continueget(float i)
	{
		s=i;
		total+=s;
		return total;
	};
 };                      //定义一个类,用它来实现玩家得到牌以及计算点数这个过程
wwpoint::wwpoint(){
	total=0;
}
class value{
private:
	int  i;
    float j;
public:
	float distribute()
	{
		i=rand()%13+1;
		if(i<=10)
			j=float(i);    //将i强制转换为float型
		else if(i>10)
			j=0.5;         //当点数大于10时,都变为0.5
		return j;
	}
	float get()
	{
		return j;
	}
};                       //定义一个产生随机数既发牌类
char man(wwpoint *a)
{
	value g;
	char c;
	float t;
    float static b=0.0;
    cout<<"your point is "<<b<<endl;
    cout<<"Want to get a paper again?Please input 'Y' or 'N'."<<endl;
    cin>>c;
    if (c=='Y')
	{
	    g.distribute();
        t=g.get();
	    a->continueget(t);
	}
	b=a->get();
	return c;
}

float computer(wwpoint *f)
{
	value a;
	float g;
	float static t=0.0;
    cout<<"computer's point is "<<t<<endl;
	if(t<=18)
	{	
	   a.distribute();
	   g=a.get();
	   f->continueget(g);
	}                              
    t=f->get();
	return t;
}
void compare(float m,float c)           //比较最后结果
{
if(m<c&&c<=21||m>21&&c<=21)
  cout<<"No.2 win the game"<<endl;
else if(c<m&&m<=21||c>21&&m<=21)
  cout<<"No.1 win the game"<<endl;
else if(c>21&&m>21)
cout<<"No one win the game!"<<endl;
}
void mancomputer()                   //人机游戏
{                          
      char c;
	  float p,q;
	  int t;
	  wwpoint mpoint,cpoint;
      cout<<"BEGIN!!!"<<endl;
	  t=rand()%5+14;
	  for(; ;)
	  {
        q=computer(&cpoint);
	    c=man(&mpoint);
	    if(q>=t&&c=='N')
		     break;
	  } 
	 p=mpoint.get();
     q=cpoint.get();
     compare(q,p);
}


void manman()                 //人人游戏
{
	float p,q,h,y;
	char c,s;
	wwpoint one, another;
	value a,b;
	cout<<"BEGIN!!!"<<endl;
   do
{
	cout<<"No.1,continue?"<<endl;
	cin>>c;
	if(c=='Y')
	{
        a.distribute();
		p=a.get();
		one.continueget(p);
		h=one.get();
	}
	cout<<p<<"   "<<"No.1' point is "<<h<<endl;
	cout<<"No.2,continue?"<<endl;
	cin>>s;
	if(s=='Y')
	{
	    b.distribute();
		q=b.get();
		another.continueget(q);
		y=another.get();
	}
    cout<<q<<"   "<<"No.2' point is "<<y<<endl;
}while(c=='Y'||s=='Y');
	p=one.get();
	q=another.get();
    compare(p,q);
}

 void computercom()                  //机器之间游戏
{
	wwpoint fst,scd;
	value a,b;
	int t;
	float i,j,f,s;
    cout<<"BEGIN!!!"<<endl;
	t=rand()%5+14;                //将电脑的点数控制在14到18以内
    for(; ;)
	{
	   a.distribute();
	   i=a.get();
	   fst.continueget(i);
	   f=fst.get();
	   cout<<i<<"  "<<"No.1's point is "<<f<<endl;
	   b.distribute();
       j=b.get();
	   scd.continueget(j);
	   s=scd.get();
	   cout<<j<<"  "<<"No.2's point is "<<s<<endl;
	   if(f>=t&&s>=t)
		   break; 
	}
   compare(f,s);
}
void select()                     //选择一种游戏方式
{
	   void mancomputer();
	   void manman();
	   void computercom();
	   char c;
	   for(;;)
	   {
	   cout<<"Please select a type"<<endl;
	   cout<<"'0':man-computer  '1':man-man   '2':computer-computer"<<endl;
	   cin>>c;
	   if(c!='0'&&c!='1'&&c!='2')
		   break;
	   switch(c)
	   {
	      case '0' : mancomputer(); break;
		  case '1' : manman(); break;
		  case '2' : computercom(); break;
		  default  : cout<<"error!!!"<<endl;
	   }
	   
	   }
}
void main()
{
	srand(time(NULL));
	select();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -