C++实现简单双骰游戏
C++实现简单双骰游戏
随机数的生成:双骰游戏。投掷两个骰子,规则:
(1)第一次如果投出 7 或 11 点,玩家胜
(2)第一次如果投出 2,3 或 12 点,玩家输
(3)第一次如果投出 4, 5, 6, 8, 9, 10 点:
① 该点成为玩家的点数
② 玩家必须在投出 7 点之前投出玩家的点数,才能赢
#include<iostream>
#include<cstdlib>
using namespace std;
int dice(){
srand(int(time(0)));
return rand()%12+1;
}
void diceGame(){
cout<<"请按任意键开始游戏"<<endl;
cin.get();
int Dnum = dice(); //the number of dice
cout<<"您投掷的点数为:"<<Dnum<<endl;
int Pnum1 = 0; //the first number of player
int Pnum2 = 0; //the second number of player
cout<<"按任意键继续"<<endl;
cin.get();
if(Dnum == 7||Dnum == 11){
cout<<"恭喜您获得胜利!"<<endl;
}else if(Dnum == 2||Dnum == 3||Dnum == 12){
cout<<"很遗憾,您输了。"<<endl;
}else{ //Dnum is 4,5,6,8,9,10
Pnum1 = Dnum;
while(1){
cout<<"投掷结果为4到10,将重新投掷骰子"<<endl;
cout<<"按任意键继续"<<endl;
cin.get();
Pnum2 = dice();
cout<<"重新投掷骰子的结果为:"<<Pnum2<<endl;
cout<<"按任意键继续"<<endl;
cin.get();
if(Pnum2 == 7){
cout<<"很遗憾,您输了。"<<endl;
}else{
if(Pnum2 == Pnum1){
cout<<"恭喜您获得胜利!"<<endl;
break;
}
}
}
}
}
int main(){
int i;
for(i=0;i<5;i++){
diceGame();
}
return 0;
}