概述
//不用随机数
/*#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;
}
最后
以上就是威武香氛为你收集整理的色子游戏(随机数)的全部内容,希望文章能够帮你解决色子游戏(随机数)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复