色子游戏(随机数)

//不用随机数
/*#include<iostream>
#include<cstring>
#define TRUE 1
#define ERROR 0
using namespace std;

int Game1()    //第一次投掷骰子的方法规则
{
 int n1,n2;
 cout<<"输入你所投掷的点数为:";
 cin>>n1>>n2;
 int s1=n1+n2;
 if((s1==7)||(s1==11)) return TRUE;
 else
 {
  if((s1==2)||(s1==3)||(s1==12))return ERROR;
  else
  {
   cout<<"你投掷的点数不满足要求,想要赢得比赛,必须继续,继续投掷请输入yes,否则请输入no:";
   char a[5];
   cin>>a;
   if(!strcmp(a,"no"))return ERROR;
   return s1;  //返回第一次掷骰子的两数之和
  }
 }
}
int Game2(int s)   //第二次投掷骰子的方法规则
{
 int n1,n2;
 cout<<"输入你所投掷的点数为:";
 cin>>n1>>n2;
 cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
 int s1=n1+n2;
 if(s1==7)return ERROR;
 else
 {
  if(s1==s)return TRUE;
 }
 return s;
}
int main()
{
 bool frag1(true);  //控制是否继续游戏
 while(frag1)
 {
 bool frag2(true);
 int wins=0,loses=0,k,s=0,s2;
 while(frag2)
 {
  s++;  //记录总共掷了多少次
  if(s==1)k=Game1();
  if(s>1)k=Game2(s2);
  if(s>1&&k==1){wins++;frag2=false;}
  else
   if(k==1) wins++;
   else
    if(k==0) {loses++;frag2=false;}
    else
    s2=k;
 }
 cout<<"你总共投掷了"<<s<<"次,"<<"赢得次数为:"<<wins<<",输的次数为:"<<(s-wins)<<endl;
 cout<<"是否继续,是请输入yes,退出请输入no:";
  char a2[5];
  cin>>a2;
  if(!strcmp(a2,"no")) frag1=false;
 }
 return 0;
}*/
//没改之前
/*#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
#define TRUE 1
#define ERROR 0
using namespace std;

int random(int a,int b) //用定义random函数没有直接常量定义(#define random(x)(rand()%x))的好,这样出现相同的概率很大
{
 return a+(b-a)*rand()/(RAND_MAX+1);
}
int Randomnumber()
{
 int num;
 
 num=random(1,7);
 cout<<num<<endl;
 return num;
}
int Game1()    //第一次投掷骰子的方法规则
{
 srand(unsigned(time(NULL)));
 int n1,n2;
 n1=Randomnumber();
 n2=Randomnumber();
 cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
 int s1=n1+n2;
 if((s1==7)||(s1==11)) return TRUE;
 else
 {
  if((s1==2)||(s1==3)||(s1==12))return ERROR;
  else
  {
   cout<<"你投掷的点数不满足要求,想要赢得比赛,必须继续,继续投掷请输入yes,否则请输入no:";
   char a[5];
   cin>>a;
   if(!strcmp(a,"no"))return ERROR;
   return s1;  //返回第一次掷骰子的两数之和
  }
 }
}
int Game2(int s)   //第二次投掷骰子的方法规则
{
 srand(unsigned(time(NULL)));
 int n1,n2;
 n1=Randomnumber();
 n2=Randomnumber();
 cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
 int s1=n1+n2;
 if(s1==7)return ERROR;
 else
 {
  if(s1==s)return TRUE;
 }
 return s;
}
int main()
{
 bool frag1(true);  //控制是否继续游戏
 while(frag1)
 {
 bool frag2(true);
 int wins=0,loses=0,k,s=0,s2;
 while(frag2)
 {
  s++;  //记录总共掷了多少次
  if(s==1)k=Game1();
  if(s>1)k=Game2(s2);
  if(s>1&&k==1){wins++;frag2=false;}
  else
   if(k==1) {wins++;frag2=false;}
   else
    if(k==0) {loses++;frag2=false;}
    else
    s2=k;
 }
 cout<<"你总共投掷了"<<s<<"次,"<<"赢得次数为:"<<wins<<",输的次数为:"<<(s-wins)<<endl;
 cout<<"是否继续,是请输入yes,退出请输入no:";
  char a2[5];
  cin>>a2;
  if(!strcmp(a2,"no")) frag1=false;
 }
 return 0;
}*/
//该程序是,只要你选择了原来的数不满足要求并且继续执行时,则一直执行下去,直到赢了为止
/*#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
#define TRUE 1
#define ERROR 0
#define random(x)(rand()%x)
using namespace std;

//int random(int a,int b)
//{
// return a+(b-a)*rand()/(RAND_MAX+1);
//}
int Randomnumber()
{
 int num;
 
 num=int(1+random(6));
 cout<<num<<endl;
 return num;
}
int Game1()    //第一次投掷骰子的方法规则
{
 srand(unsigned(time(NULL)));
 int n1,n2;
 n1=Randomnumber();
 n2=Randomnumber();
 cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
 int s1=n1+n2;
 if((s1==7)||(s1==11)) return TRUE;
 else
 {
  if((s1==2)||(s1==3)||(s1==12))return ERROR;
  else
  {
   cout<<"你投掷的点数不满足要求,想要赢得比赛,必须继续,继续投掷请输入yes,否则请输入no:";
   char a[5];
   cin>>a;
   if(!strcmp(a,"no"))return ERROR;
   return s1;  //返回第一次掷骰子的两数之和
  }
 }
}
int Game2(int s)   //第二次投掷骰子的方法规则
{
 srand(unsigned(time(NULL)));
 int n1,n2;
 n1=Randomnumber();
 n2=Randomnumber();
 cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
 int s1=n1+n2;
 if(s1==7)return ERROR;
 else
 {
  if(s1==s)return TRUE;
 }
 return s;
}
int main()
{
 bool frag1(true);  //控制是否继续游戏
 while(frag1)
 {
 bool frag2(true);
 int wins=0,loses=0,k,s=0,s2;
 while(frag2)
 {
  s++;  //记录总共掷了多少次
  if(s==1)k=Game1();
  if(s>1)k=Game2(s2);
  if(s>1&&k==1){wins++;frag2=false;}
  else
   if(k==1) {wins++;frag2=false;}
   else
    if(k==0) {loses++;frag2=false;}
    else
    s2=k;
 }
 cout<<"你总共投掷了"<<s<<"次,"<<"赢得次数为:"<<wins<<",输的次数为:"<<(s-wins)<<endl;
 cout<<"是否继续,是请输入yes,退出请输入no:";
  char a2[5];
  cin>>a2;
  if(!strcmp(a2,"no")) frag1=false;
 }
 return 0;
}*/

//转换界面的骰子游戏
#include<iostream>
#include<cstring>
#include<ctime>
#include<cstdlib>
#define TRUE 1
#define ERROR 0
#define random(x)(rand()%x)  //
using namespace std;

class A
{
private:
 int num;
 int n1;
 int n2;
 int s;
public:
 int Randomnumber()
 {
  num=int(1+random(6));
  cout<<num<<endl;
  return num;
 }
 int Game1()    //第一次投掷骰子的方法规则
 {
  srand(unsigned(time(NULL)));  //
  n1=Randomnumber();
  n2=Randomnumber();
  cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
  s=n1+n2;
  if((s==7)||(s==11)) return TRUE;
  else
  {
   if((s==2)||(s==3)||(s==12))return ERROR;
   else
   {
    cout<<"你投掷的点数不满足要求,想要赢得比赛,必须继续,继续投掷请输入yes,否则请输入no:";
    char a[5];
    cin>>a;
    if(!strcmp(a,"no"))return ERROR;
    return s;  //返回第一次掷骰子的两数之和
   }
  }
 }
 int Game2(int s1)   //第二次投掷骰子的方法规则
 {
  srand(unsigned(time(NULL)));  //
  n1=Randomnumber();
  n2=Randomnumber();
  cout<<"你所投掷的点数为:"<<n1<<" "<<n2<<endl;
  s=n1+n2;
  if(s==7)return ERROR;
  else
  {
   if(s1==s)return TRUE;
  }
  return s1;
 }
};
int main()
{
 bool frag1(true);  //控制是否继续游戏
 while(frag1)
 {
 bool frag2(true);
 int wins=0,loses=0,k,s=0,s2;
 A a;
 while(frag2)
 {
  s++;  //记录总共掷了多少次
  if(s==1)k=a.Game1();
  if(s>1)k=a.Game2(s2);
  if(s>1&&k==1){wins++;frag2=false;}
  else
   if(k==1) {wins++;frag2=false;}
   else
    if(k==0) {loses++;frag2=false;}
    else
    s2=k;
 }
 cout<<"你总共投掷了"<<s<<"次,"<<"赢得次数为:"<<wins<<",输的次数为:"<<(s-wins)<<endl;
 cout<<"是否继续,是请输入yes,退出请输入no:";
  char a2[5];
  cin>>a2;
  if(!strcmp(a2,"no")) frag1=false;
  system("cls");  //加了个转换界面的函数,当要再次重新投掷的话,就转换界面重新输入
 }
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值