5.掷骰子游戏(随即取值)

来源:互联网 发布:win8网络共享无法访问 编辑:程序博客网 时间:2024/05/07 18:47

机会游戏(投骰子):游戏者投两枚骰子,每个骰子有六面,这些面包含123456个点。投两枚骰子之后,计算点数之和。如果第一次投时的和为7或者11,则游戏者输,庄家赢。如果第一次投时的和为2312,则游戏者获胜。如果第一投时的和为4568910,则这个和成为游戏者的点数,要想赢,就要继续投骰子,直到投出当前所拥有的点数,但如果在连投7次还没有投出该点数,则游戏者输。

 

 

 

#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 ;
  }

 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 ;
}


void main()
{
 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);
 return ;
}

原创粉丝点击