#include <ctime>
#include <cstdlib>
#include<stdio.h>
int TimeRand()//根据系统时间随机取1-6中的数字
{
int t; //seed
srand((unsigned)time(NULL));
t = rand() % 6+ 1; // random number 1-10
return t;
}
int game()
{
srand((unsigned)time(NULL));
int *a=new int;
int *b=new int;
int *sum=new int;
printf("请掷第1次个骰子");
a[0]=rand()% 6+ 1;
b[0]=rand()% 6+ 1;
sum[0]=a[0]+b[0];
printf("结果为:%d和%d\n",a[0],b[0]);
if(sum[0]==7||sum[0]==11)
return 1;
else if(sum[0]==2||sum[0]==3||sum[0]==12)
return 0;
else
{
int count=1;
bool IsEnd=false;
while(!IsEnd)
{
printf("请掷第%d次个骰子",count+1);
a[count]=rand()% 6+ 1;
b[count]=rand()% 6+ 1;
sum[count]=a[count]+b[count];
printf("结果为:%d和%d\n",a[count],b[count]);
if(sum[count]==sum[0])
{
return 1;
}
else if(sum[count]==7)
{
return 0;
}
else
{
count=count+1;
}
}
}
}
void main()
{
int a=game();
if(a==1)
printf("Yes,congratulation!\n");
else
{
printf("sorry,you are fail\n");
}
}

 

这里值得自己注意的是:

随机数的产生。

一开始想用函数产生随机数的,但是原来函数是这么写的

int TimeRand()//根据系统时间随机取1-6中的数字
{
int t; //seed
srand((unsigned)time(NULL));
t = rand() % 6+ 1; // random number 1-10
return t;
}

发现得到的两个随机数是一样的。

所以现在直接在game函数里写随机数

srand((unsigned)time(NULL)); 

a[0]=rand()% 6+ 1;
b[0]=rand()% 6+ 1;

这样就可以产生两个不同的随机数啦

posted on 2016-05-08 19:37  薄樱  阅读(361)  评论(0编辑  收藏  举报