双骰子是一项非常流行于赌场的骰子游戏。编写程序实现这个游戏的变种,如下所示:
投掷两枚骰子。每个骰子有6个面,分别为1,2,3,4,5,6.检视一下两枚骰子的数量和。如果和是2,3或者12(叫做双骰),你输。如果和为7或者11(叫做自然),你赢;如果和是其他值,那么称这个和为点数。继续投掷两枚骰子,直到你掷出7(你输)或者掷出刚才的点数(你赢)。
下面是一些运行样例:
样例1:
You rolled 5 + 6 = 11
You win
样例2:
You rolled 1 + 2 = 3
You lose
样例3:
You rolled 4 + 4 = 8
point is 8
You rolled 6 + 2 = 8
You win
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int touZi()
{
srand(time(0));
int num1 = rand()%6+1;
int num2 = rand()%6+1;
cout<<"You rolled "<<"num1 "<<"+"<<"num2 = "<<num1+num2<<endl;
return num1+num2;
}
int main()
{
int m = touZi();
if(m==7||m==11)
{
cout<<"You win";
return 0;
}
else if(m==2||m==3||m==12)
{
cout<<"You lose";
return 0;
}
int point = m;
cout << "point is " << point << endl;
do
{
m = touZi();
}
while (m != 7 && m != point);
if (m == 7)
cout << "You lose" << endl;
else
cout << "You win" << endl;
return 0;
}