6、掷骰子游戏(续5)
掷骰子游戏:
1.加进赌注。将程序中运行投骰子游戏的部分打包成函数。将bankBalance初始化为1000美元(赌本),提示游戏者输入赌注wager。用while循环检查wager是否小于或等于bankBalance,如果不是,则提示用户重新输入wager,直到wager有效。输入正确的wager之后,运行投骰子游戏。如果游戏者赢,则在bankBalance中增加wager,如果游戏者输,则在bankBalance中减去wager,并打印新的bankBalance。检查bankBalance是否为0,如果是,则打印消息“Sorry,You Busted!”。游戏进行时,可以打印一些聊天消息,如“Oh, you’re going for broke, huh?”或“Aw cmon,take a changce!”或“You’re up big. Now’s the time to cash in your chips!”。
2.根据以上游戏程序,运行1000次投骰子游戏,并回答以下问题:
(1)前十二次和十二次以后各输赢多少场?
(2)赢的机会是多少?(投骰子游戏公平吗?为什么?)
(3)游戏玩的越久,赢的机会是否越多?
代码:
#include
#include
#include
class Dice{
private:
static int count;
int a,b;
public:
void init(int x,int y)
{
a=x;
b=y;
count++;
return ;
}
void setcount()
{
count=0;
}
int getdice1()
{
return a;
}
int getdice2()
{
return b;
}
int Getpoint() //骰子的点数
{
return a+b;
}
void print() //输出掷骰子的结果
{
cout<<"您第 "<
}
};
int Dice::count=0;
void Result(int a) //输出胜负结果
{
if(a==1)
cout<<" Player Wins !"<
cout<<" Player Loses !"<
}
int game() //执行游戏
{
srand( (unsigned)time( NULL ) );
int d;
Dice dice01;
dice01.init( 1+rand()%6, 1+rand()%6 );
dice01.print();
int a = dice01.Getpoint();
if(a==4 || a==5 || a==6 || a==8 || a==9 || a==10)
{
int i=0;
for(i=0; i<6; i++)
{
Dice dice02;
dice02.init(1+rand()%6,1+rand()%6);
dice02.print();
if(dice02.Getpoint()==a)
{
d=1;
break;
}
}
if(i==6)
d=0;
}
if(a==2 || a==3 || a==12)
{
d=1;
}
if(a==7 || a==11)
{
d=0;
}
Result(d);
dice01.setcount(); //使表示掷骰子的次数count重新回到0
return d;
}
int mathgame(int x) //记算输赢机会
{
int d=0;
int k;
k=x;
srand( (unsigned)time( NULL ) );
for(int i=0; i
Dice dice01;
dice01.init( 1+rand()%6, 1+rand()%6 );
int a = dice01.Getpoint();
if(a==4 || a==5 || a==6 || a==8 || a==9 || a==10)
{
int i=0;
for(i=0; i<6; i++)
{
Dice dice02;
dice02.init(1+rand()%6,1+rand()%6);
if(dice02.Getpoint()==a)
{
d++;
break;
}
}
}
if(a==2 || a==3 || a==12)
{
d++;
}
}
return d;
}
int putwager(void) //输入赌注的数目
{
int wager;
cout<<"请输入这轮的赌注: "<
cin>>wager;
return wager;
}
int continuegame(void) //决定要不要继续玩游戏
{
cout<
int x = 0;
cin>>x;
while(x!=1 && x!=0)
{
cout<<"您输入的数字不符合要求"<
continue ;
}
return x;
}
static int bankBalance = 1000; //设置全局变量,也就是赌本,并初始化为1000美元,游戏中以1美元为最小单位
void talking(int x) //输出聊天语言。当赌注少于200或多余1800时才输出聊天语言。
{
if(x <= 200)
{
cout<
if(x >= 1800)
{
cout<
return ;
}
void main()
{
cout<<" 欢迎来玩掷骰子游戏,您有 1000美元 的本钱。"<
p=mathgame(12);
q=mathgame(1000);
cout<
int m = putwager();
while(m > bankBalance) //判断输入的赌注是不是符合要求
{
cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<
}
while(m <= bankBalance) //赌注符合条件后,开始游戏
{
int a = game();
if(a==1)
{
bankBalance=bankBalance+m;
}
else
{
bankBalance=bankBalance-m;
}
talking( bankBalance );
cout< cout<<"本剧结束,您的赌本为:"< if(bankBalance <= 0)
{
cout<<" Sorry,You Busted!* * * 您的赌本已经输完了,本次游戏结束 !"<
}
if(bankBalance >= 2000)
{
cout<<"You are Lucky !!! * * * 您已经赢得了1000美元以上,本次游戏结束 !"<
}
if(continuegame()==1)
{
m = putwager();
if(m > bankBalance)
{
cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<
}
continue ;
}
else
break ;
}
cout<
}本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
