6、掷骰子游戏(续5)

来源:互联网 发布:纳森网络 编辑:程序博客网 时间:2024/05/18 03:33

掷骰子游戏:
1.加进赌注。将程序中运行投骰子游戏的部分打包成函数。将bankBalance初始化为1000美元(赌本),提示游戏者输入赌注wager。用while循环检查wager是否小于或等于bankBalance,如果不是,则提示用户重新输入wager,直到wager有效。输入正确的wager之后,运行投骰子游戏。如果游戏者赢,则在bankBalance中增加wager,如果游戏者输,则在bankBalance中减去wager,并打印新的bankBalance。检查bankBalance是否为0,如果是,则打印消息“Sorry,You Busted!”。游戏进行时,可以打印一些聊天消息,如“Oh, you’re going for broke, huh?”或“Aw cmon,take a changce!”或“You’re up big. Now’s the time to cash in your chips!”。

2.根据以上游戏程序,运行1000次投骰子游戏,并回答以下问题:

 (1)前十二次和十二次以后各输赢多少场?

 (2)赢的机会是多少?(投骰子游戏公平吗?为什么?)

 (3)游戏玩的越久,赢的机会是否越多?

 

代码:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>

class Dice{
private:
 static int count;
 int a,b;
 public:
 void init(int x,int y)
  {
   a=x;
   b=y;
   count++;
   return ;
  }
 void setcount()
 {
  count=0;
 }

 int getdice1()
 {
  return a;
 }

 int getdice2()
 {
  return b;
 }

 int Getpoint()   //骰子的点数
 {
  return a+b;
 }

 void print()   //输出掷骰子的结果
 {
  cout<<"您第 "<<count<<" 次掷骰子的结果为:"<<endl
   <<getdice1()<<" + "
   <<getdice2()<<" = "
   <<Getpoint()<<endl;
  return ;
 }

};

int Dice::count=0;  

void Result(int a)   //输出胜负结果
{
 if(a==1)
  cout<<"   Player Wins !"<<endl;
 else
  cout<<"   Player Loses !"<<endl;
 return ;
}


int game()  //执行游戏
{
 srand( (unsigned)time( NULL ) );
 int d;
 Dice dice01;
 dice01.init( 1+rand()%6, 1+rand()%6 );
 dice01.print();
 int a = dice01.Getpoint();
 if(a==4 || a==5 || a==6 || a==8 || a==9 || a==10)
 {
  int i=0;
  for(i=0; i<6; i++)
  {
   Dice dice02;
   dice02.init(1+rand()%6,1+rand()%6);
   dice02.print();
   if(dice02.Getpoint()==a)
   {
    d=1;
    break;
   }
  }
  if(i==6)
   d=0;
 }
 if(a==2 || a==3 || a==12)
 {
  d=1;
 }
 if(a==7 || a==11)
 {
  d=0;
 }
 Result(d);
 dice01.setcount();   //使表示掷骰子的次数count重新回到0
 return d;
}

int mathgame(int x)  //记算输赢机会
{
 int d=0;
 int k;
 k=x;
 srand( (unsigned)time( NULL ) );
 for(int i=0; i<x; i++)
 {
  Dice dice01;
  dice01.init( 1+rand()%6, 1+rand()%6 );
  int a = dice01.Getpoint();
  if(a==4 || a==5 || a==6 || a==8 || a==9 || a==10)
  {
   int i=0;
   for(i=0; i<6; i++)
   {
    Dice dice02;
    dice02.init(1+rand()%6,1+rand()%6);
    if(dice02.Getpoint()==a)
    {
     d++;
     break;
    }
   }
  }
  if(a==2 || a==3 || a==12)
  {
   d++;
  }

 }
 return d;
}

int putwager(void)   //输入赌注的数目
{
 int wager;
 cout<<"请输入这轮的赌注: "<<endl
  <<"  wager = ";
 cin>>wager;
 return wager;
}

int continuegame(void)   //决定要不要继续玩游戏
{
 cout<<endl<<"是否要继续游戏?"<<endl
  <<endl
  <<" 是 请输入数字 1 ,不是 请输入数字 0 。"<<endl
  <<endl
  <<"请选择 :";
 int x = 0;
 cin>>x;
 while(x!=1 && x!=0)
 {
  cout<<"您输入的数字不符合要求"<<endl;
  x = continuegame();
  continue ;
 }
 return x;
}

static int bankBalance = 1000;  //设置全局变量,也就是赌本,并初始化为1000美元,游戏中以1美元为最小单位

void talking(int x)   //输出聊天语言。当赌注少于200或多余1800时才输出聊天语言。
{
 if(x <= 200)
 {
  cout<<endl<<" Oh, you’re going for broke, huh? "<<endl;
 }
 if(x >= 1800)
 {
  cout<<endl<<" You’re up big. Now’s the time to cash in your chips ! "<<endl;
 }
 return ;
}

 

void main()
 {
  cout<<"  欢迎来玩掷骰子游戏,您有 1000美元 的本钱。"<<endl
   <<"每次的赌注是 整数 。"<<endl<<endl; 
  int p,q;
  p=mathgame(12);
  q=mathgame(1000);
  cout<<endl<<" 本游戏的输赢机会为 : "<<endl<<endl;
  cout<<endl<<" 玩 12次 的输赢机会是 : "<<p<<endl;
  cout<<endl<<" 玩12次以后到1000次 的赢机会为 : "<<q-p<<endl;
  cout<<endl<<" 本游戏 玩1000次 的输赢机会为 : "<<q<<endl;
  cout<<endl<<"******  祝 你 游 戏 愉 快 !******"<<endl<<endl;
 
  int m = putwager();
  while(m > bankBalance)   //判断输入的赌注是不是符合要求
  {
   cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<<endl;
   m = putwager();
  }
  while(m <= bankBalance)   //赌注符合条件后,开始游戏
  {
   int a = game();
   if(a==1)
   {
    bankBalance=bankBalance+m;
   }
   else
   {
    bankBalance=bankBalance-m;
   }
   talking( bankBalance );

   cout<<endl;

   cout<<"本剧结束,您的赌本为:"<<bankBalance<<endl;

   if(bankBalance <= 0)
   {
    cout<<" Sorry,You Busted!* * *  您的赌本已经输完了,本次游戏结束 !"<<endl;
    break ;
   }
   if(bankBalance >= 2000)
   {
    cout<<"You are Lucky !!! * * *  您已经赢得了1000美元以上,本次游戏结束 !"<<endl;
    break ;
   } 
   if(continuegame()==1)
   {
    m = putwager();
    if(m > bankBalance)
    {
     cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<<endl;
     putwager();
    }
    continue ;
   }
   else
    break ;
  }
  cout<<endl<<"  游  戏 结 束 ,欢 迎 再 来 !"<<endl;
  return ;
 }

 

原创粉丝点击