投骰子游戲隨機數
阿新 • • 發佈:2019-02-12
#include"iostream"
#include"ctime"
#include"cstdlib"
using namespace std;
int roll()
{ int i=rand()%6+1;
int j=rand()%6+1;
int k=i+j;
cout<<"投擲的兩點數之和為:"<<k<<endl;
return k;
}
enum result{ win,lose,playing
};
main()
{ int sum,l;
result t;
int unsigned seed;
cout<<"請輸入一個整數" <<endl;
cin>>seed;
srand(seed);
sum=roll();
switch(sum)
{ case 7:
case 11:
t=win;
break;
case 2:
case 3:
case 12:
t=lose;
break;
default:
l=sum;
t=playing;
break;
}
while(t==playing)
{
sum=roll();
if(sum==l)
t=win;
else if(sum==7)
t=lose;
}
if(t==win)
cout<<"你贏了1"<<endl;
else if(t==lose)
cout<<"你輸了!"<<endl;
return 0;
}