简单C++程序——骰子游戏

1.首先理解种子seed的概念。我们首先可以用一个循环语句和rand函数去得到一个随机数组,如果我们反复运行它得到的这个数组是一样的,所以就出现了种子,根据不同的数据可以产生不同的数组,从而满足随机性
2.骰子是从一到六,本程序灵活的使用了rand()随机生成函数,即int die1 = 1 + rand()%6从而得到了1~6的数
#include < iostream >
#include < cstdlib >
using namespace std;
enum gamestatus {WIN,LOSE,PLAYING};
int rolldice();
int main()
{
int sum,mypoint;
gamestatus status;
unsigned seed;
cout<<“please input an unsigned integer:”;
cin>>seed;
srand(seed);
sum=rolldice();
switch(sum)
{
case 7:
case 11:
status=WIN;
break;
case 2:
case 3:
case 12:
status=LOSE;
break;
default:
status=PLAYING;
mypoint=sum;
cout<<“point is “< break;
}
while (status == PLAYING)
{
sum = rolldice();
if (sum == mypoint)
status = WIN;
else if(sum==7)
status=LOSE;
}
if(status == WIN)
cout<<“player wins”< else
cout<<“player loses”< return 0;
}
int rolldice()
{
int die1=1+rand()%6;
int die2=1+rand()%6;
int sum=die1+die2;
cout<<“player rolled”< return sum;
}

你可能感兴趣的:(c++,c语言)