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

本文深入解析了机会游戏中的投骰子规则,包括如何计算点数之和,以及根据点数和判断游戏者是否输赢的具体流程。通过随机数生成模拟游戏过程,直观展示了不同情况下游戏的走向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

机会游戏(投骰子):游戏者投两枚骰子,每个骰子有六面,这些面包含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 ;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值