(骰子游戏)Craps game的算法问题.

blueink_200451 2007-04-14 03:13:48
下面有两个问题: 赌博游戏 Craps game 胜率的评价与比较.
(什么是赌博游戏 Craps game :一个简单的赌博游戏,游戏规则如下:玩家掷两个骰子,点数为1到6,如果第一次点数和为7或11,则玩家胜,如果点数和为2、3或12,则玩家输,如果和为其它点数,则记录第一次的点数和,然后继续掷骰,直至点数和等于第一次掷出的点数和,则玩家胜,如果在这之前掷出了点数和为7,则玩家输。)


1.第一种方法:c++模拟的方法.(这个我可能知道一点答案,但是很想知道各位前辈的思路)

2.第二种方法:数学概率计算的方法.(这个就是原话,我也不是很明白到底是个什么意思)(c++怎么实现数学概率计算的方法??)(这个题重点看看吧)

各路兄台都来看一下吧,我想要准确的结果,先谢谢大家了,知道多少就赐教多少吧.后辈不胜感激.
...全文
896 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2007-04-29
  • 打赏
  • 举报
回复
http://topic.csdn.net/t/20050201/16/3768945.html

和这个帖子主题内容一样,楼主自己看看 ?
jixingzhong 2007-04-29
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int rollDice(void);

int bankBalance=1000;

main(void)
{
int gameStatus,sum,myPoint,investment;
char choice;
printf("Please Enter your choice(Y/N)?");
scanf("%c",&choice);

while (choice !='N' && choice !='n')
{

printf("\nPlease enter your investment:");
scanf("%d",&investment);

if (bankBalance < investment)
{
do{
printf("\n%s\n%s",
"Your investment greater than bankBalance!",
"Please enter your investment:");
scanf("%d",&investment);
}while(bankBalance < investment);
}
srand(time(NULL));
sum=rollDice(); //第一次投掷
switch (sum)
{
case 7: case 11: //第一次投掷就赢了
gameStatus=1;
break;
case 2: case 3: case 12: //第一次投掷就输了
gameStatus=2;
break;
default:
gameStatus=0;
myPoint=sum;
printf("Point is %d\n",myPoint);
break;
}

while (gameStatus==0) //继续投掷
{
sum=rollDice();

if (sum==myPoint)
gameStatus=1; //因为掷出了自己的点数,所以赢了
else
if (sum==7) //因为掷出了7,所以输了
gameStatus=2;
}

if (gameStatus==1)
{
printf("Player wins\n");
bankBalance=bankBalance+investment;
}
else
{
printf("Player loses\n");
bankBalance=bankBalance-investment;
}
if (bankBalance <=0 )
{

printf("Your bankBalance is bankBalance=%d\n",bankBalance);
break;
}

printf("bankBalance=%d\n",bankBalance);
printf("Please Enter your choice(Y/N)?");
getchar();
scanf("%c",&choice);
}

return 0;
}

int rollDice(void)
{
int die1,die2,workSum;

die1=1+(rand()%6);
die2=1+(rand()%6);
workSum=die1+die2;
printf("Player rolled %d+%d=%d\n",die1,die2,workSum);

return workSum;
}
jixingzhong 2007-04-29
  • 打赏
  • 举报
回复
下载连接:
http://84ren.com/read.php?tid=74
leiyu2008 2007-04-28
  • 打赏
  • 举报
回复
这是两种方法都是有了.你再仔细看一下吧.有什么问题再发上来看看.我也是有个这样的例子.我们一起来分享.
leiyu2008 2007-04-28
  • 打赏
  • 举报
回复
1)
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
int rollDice(void);

main(void)
{
int gamestatus,sum,mypoint; //定义 赢输, 点数和,点数
char choice;

printf("game start(Y/N)?");
scanf("%c",&choice);
while(choice !='N' && choice !='n')
{
srand(time(NULL));
sum=rollDice(); //第一次投掷
switch (sum)
{
case 7:
case 11: //第一次投掷就赢了
gamestatus=1;
break;
case 2:
case 3:
case 12: //第一次投掷就输了
gamestatus=2;
break;
default:
gamestatus=0;
mypoint=sum;
printf("play first point is %d\n",mypoint);
break;
}

while (gamestatus==0) //继续投掷
{
sum=rollDice();

if (sum==mypoint)
gamestatus=1; //因为掷出了自己的点数,所以赢了
else
if (sum==7) //因为掷出了7,所以输了
gamestatus=2;
}

if (gamestatus==1)
{
printf("Player win\n");

}
else
{
printf("player lose\n");
}
printf("Please countin(Y/N)?");
getchar();
scanf("%c",&choice);
}

return 0;
}

int rollDice(void)
{
int Renmian1,Renmian2,RenSum;

Renmian1=1+(rand()%6);
Renmian2=1+(rand()%6);

RenSum=Renmian1+Renmian2;

printf("Player point is %d+%d=%d\n",Renmian1,Renmian2,RenSum);

return
RenSum;
}





2)
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void simulate(int times,int *win,int *lose)
{
int i;
int num_rand1,num_rand2;
int sum,sum_tmp;




srand(time(NULL));
for(i=0;i<times;i++)
{
num_rand1=rand()%6+1;
num_rand2=rand()%6+1;
sum=num_rand1+num_rand2;

if(sum==7 || sum==11)
(*win)++;
else if(sum==2 || sum==3 || sum==12)
(*lose)++;
else
{
while(1)
{
num_rand1=rand()%6+1;
num_rand2=rand()%6+1;
sum_tmp=num_rand1+num_rand2;
if(sum==sum_tmp)
{
(*win)++;
break;
}
else if(sum_tmp==7)
{
(*lose)++;
break;
}
}
}
}
return ;
}
int main()
{
double rate_win,rate_lose;
int times_win,times_lose;
int times;

times=10000;
times_win=0;
times_lose=0;
simulate(times,×_win,×_lose);

rate_win=(double)times_win/times;
rate_lose=(double)times_lose/times;

printf("simulate result:\n");
printf(" times:%d\n times win:%d\n times lose:%d\n",times,times_win,times_lose);
printf(" rate win:%f\n rate lose:%f\n",rate_win,rate_lose);


getchar();
return 0;
}


dirtysalt 2007-04-14
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void simulate(int times,int *win,int *lose)
{
int i;
int num_rand1,num_rand2;
int sum,sum_tmp;

srand(time(NULL));
for(i=0;i<times;i++)
{
num_rand1=rand()%6+1;
num_rand2=rand()%6+1;
sum=num_rand1+num_rand2;

if(sum==7 || sum==11)
(*win)++;
else if(sum==2 || sum==3 || sum==12)
(*lose)++;
else
{
while(1)
{
num_rand1=rand()%6+1;
num_rand2=rand()%6+1;
sum_tmp=num_rand1+num_rand2;
if(sum==sum_tmp)
{
(*win)++;
break;
}
else if(sum_tmp==7)
{
(*lose)++;
break;
}
}
}
}
return ;
}
int main()
{
double rate_win,rate_lose;
int times_win,times_lose;
int times;

times=0x007fffff;
times_win=0;
times_lose=0;
simulate(times,×_win,×_lose);

rate_win=(double)times_win/times;
rate_lose=(double)times_lose/times;

printf("simulate result:\n");
printf("times:%d time win:%d times lose:%d\n",times,times_win,times_lose);
printf("rate win:%f rate lose:%f\n",rate_win,rate_lose);

getchar();
return 0;
}
似乎输的概率比赢的概率稍微高一些,如果赌场每天几百亿流动量的话,还有的赚

fandouC 2007-04-14
  • 打赏
  • 举报
回复
过程比较简单,但我不懂这个题怎么来实现点数随机分配.
al0n9 2007-04-14
  • 打赏
  • 举报
回复
1。基本上就是一个循环,也没什么了。
2。就是让你列出计算概率的数学公式,而不用写程序去计算。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧