6、掷骰子游戏(续5)
来源:互联网 发布:纳森网络 编辑:程序博客网 时间:2024/05/18 03:33
掷骰子游戏:
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 <iostream.h>
#include <stdlib.h>
#include <time.h>
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<<"您第 "<<count<<" 次掷骰子的结果为:"<<endl
<<getdice1()<<" + "
<<getdice2()<<" = "
<<Getpoint()<<endl;
return ;
}
};
int Dice::count=0;
void Result(int a) //输出胜负结果
{
if(a==1)
cout<<" Player Wins !"<<endl;
else
cout<<" Player Loses !"<<endl;
return ;
}
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<x; 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<<"请输入这轮的赌注: "<<endl
<<" wager = ";
cin>>wager;
return wager;
}
int continuegame(void) //决定要不要继续玩游戏
{
cout<<endl<<"是否要继续游戏?"<<endl
<<endl
<<" 是 请输入数字 1 ,不是 请输入数字 0 。"<<endl
<<endl
<<"请选择 :";
int x = 0;
cin>>x;
while(x!=1 && x!=0)
{
cout<<"您输入的数字不符合要求"<<endl;
x = continuegame();
continue ;
}
return x;
}
static int bankBalance = 1000; //设置全局变量,也就是赌本,并初始化为1000美元,游戏中以1美元为最小单位
void talking(int x) //输出聊天语言。当赌注少于200或多余1800时才输出聊天语言。
{
if(x <= 200)
{
cout<<endl<<" Oh, you’re going for broke, huh? "<<endl;
}
if(x >= 1800)
{
cout<<endl<<" You’re up big. Now’s the time to cash in your chips ! "<<endl;
}
return ;
}
void main()
{
cout<<" 欢迎来玩掷骰子游戏,您有 1000美元 的本钱。"<<endl
<<"每次的赌注是 整数 。"<<endl<<endl;
int p,q;
p=mathgame(12);
q=mathgame(1000);
cout<<endl<<" 本游戏的输赢机会为 : "<<endl<<endl;
cout<<endl<<" 玩 12次 的输赢机会是 : "<<p<<endl;
cout<<endl<<" 玩12次以后到1000次 的赢机会为 : "<<q-p<<endl;
cout<<endl<<" 本游戏 玩1000次 的输赢机会为 : "<<q<<endl;
cout<<endl<<"****** 祝 你 游 戏 愉 快 !******"<<endl<<endl;
int m = putwager();
while(m > bankBalance) //判断输入的赌注是不是符合要求
{
cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<<endl;
m = putwager();
}
while(m <= bankBalance) //赌注符合条件后,开始游戏
{
int a = game();
if(a==1)
{
bankBalance=bankBalance+m;
}
else
{
bankBalance=bankBalance-m;
}
talking( bankBalance );
cout<<endl;
cout<<"本剧结束,您的赌本为:"<<bankBalance<<endl;
if(bankBalance <= 0)
{
cout<<" Sorry,You Busted!* * * 您的赌本已经输完了,本次游戏结束 !"<<endl;
break ;
}
if(bankBalance >= 2000)
{
cout<<"You are Lucky !!! * * * 您已经赢得了1000美元以上,本次游戏结束 !"<<endl;
break ;
}
if(continuegame()==1)
{
m = putwager();
if(m > bankBalance)
{
cout<<"您输入的赌注超过了你您的赌本,请重新输入 !"<<endl;
putwager();
}
continue ;
}
else
break ;
}
cout<<endl<<" 游 戏 结 束 ,欢 迎 再 来 !"<<endl;
return ;
}
- 6、掷骰子游戏(续5)
- 玩游戏(掷骰子)
- 笔记(7)掷骰子游戏--CPrimer
- 掷骰子游戏
- 掷骰子游戏
- 掷骰子游戏
- C++掷骰子游戏
- Java 常见小问题源代码 掷骰子 游戏 三个骰子,约定三个骰子的点数1-6
- 实验作业:掷骰子游戏
- 5.掷骰子游戏(随即取值)
- 骰子游戏
- 一个简单的掷骰子游戏
- 【HTML5/CSS3学习笔记】掷骰子游戏
- 【程序12】模拟掷骰子游戏
- 甲乙两人做游戏,掷三个骰子,比点数
- 掷骰子游戏 华为机试
- 掷骰子游戏-C语言实现
- java掷骰子游戏联机版
- 虽然年轻,但要敢于设计
- 八大排序算法
- ABAP开发-Class: CL_GUI_FRONTEND_SERVICES的功能
- 同一个IIS上怎么发布多个ASP.NET MVC网站
- ORA-16014和ORA-00312 无法归档日志解决方案
- 6、掷骰子游戏(续5)
- 【转】ArrayList深拷贝的一种实现方法
- Windows Server2003 配置aspnetdb数据库
- 软件手工测试和自动化测试的比较
- Pre-Test for Manual Tester( fits for both junior tester and senior tester)
- sql2005 没有注册类别0x80040154
- 超级终端的使用
- 09.10.23
- windows装上svn后,机器速度慢的解决办法