掷骰子游戏-C语言实现

作者:半点闲
博客:blog.csdn.net/cg_i
邮箱:
b_dx@sohu.com
参考书籍:《C和指针》

引子:

  大领导的公子就读于美国XXX大学计算机专业,公子多才多艺会吹口琴、玩玉箫、看小人书、占扑星相观人眉宇、风流倜傥、窃玉偷香本想过个几年弄个文凭回国吃皇粮,没曾想外国师傅不懂人情非要布置家庭作业,一时间给公子添优伤,忽想到大洋彼岸老爸有能量,领导得知吩咐喽啰速办此事为公子解忧伤,喽啰手揣上令放眼四周,角落里发现我两眼放光,可叹我苦逼一枚屁也不敢放,看看题目随手写来对不对公子你自己思量...

作业2:

B. (20 points) Simulated Dice
The casino game Table Crapsis a dice game in which a number of betting options are available depending on
the eventual roll of a pair of dice. Players may bet "for" or"against" certain outcomes in a dizzying array of
combinations with varying odds. Each round of Crapsbegins with an initial roll of the dice (the so-called
'come-out' roll). The player 'wins' on the initial roll if it totals 7 or 11, and 'Craps out'(loses) if the roll is 2, 3
or 12.

Write a simulation program to determine the probability of winning vs. losing on the first roll in the game of
Craps. Your program must include a void function named diceRollthat will use the rand()function to
provide 2 pseudo-random integer values in the range [1..6] representing a single roll of a pair of (independent)
dice.

Your mainprogram must do the following:
              •  Prompt the user, input an integer seed value and seed the pseudo-random number generator with that 
                 value
              •  Simulate 10,000 dice rolls using the diceRollfunction and determine the number of 'wins' (7, 11) 
                 and 'losses' (2, 3, 12)
              •  Display the number of 'wins' and 'losses', and also the resulting probabilities on the terminal display 
                 (note the theoretical probability for 'win' on the first roll is .2222 and for a 'loss' on the first roll is .1111)

Example:
            Enter an integer seed value: 1
            Number of wins: 2230
            Probability of winning: 0.223
            Number of losses: 1098
            Probability of losing: 0.1098
            Enter an integer seed value: 102
            Number of wins: 2258
            Probability of winning: 0.2258
            Number of losses: 1072
            Probability of losing: 0.1072

大意是写一个模拟掷骰子的游戏,每次掷两个骰子每个骰子是一个正方体有6面上面标有1、2、3、4、5、6个圆点,当骰子停止时将每个骰子朝上的点数相加,如果所得的和为2、3或12那么游戏者输掉;和为7、11赢;并模拟1000次掷骼子的结果。

代码(作业要求用C++实现,老子没心情给就给个C的):

#include <STDIO.H>
#include <STDLIB.H>
#define  ROLLS 1000


void main()
{
	int i = 0;
	int numOfDice1;
	int numOfDice2;
	int sum;
	int status = 0;
	int numOfWin = 0;
	int numOfLose = 0;
	unsigned value = 0;

	printf("Enter an integer seed value:");
	scanf("%u",&value);
	srand(value);

	for(i = 0; i <= ROLLS; i++){
		numOfDice1 = 1 + rand() % 6;
		numOfDice2 = 1 + rand() % 6;
		sum = numOfDice1 + numOfDice2;
		
		switch(sum){
			case 7:
			case 11:
				status = 1;
				numOfWin++;
				break;
			case 2:
			case 3:
			case 12:
				status = 0;
				numOfLose++;
				break;
		}
	}
	printf("Number of winns:%d\n",numOfWin);
	printf("Probability of winning:%.3f\n",(float)numOfWin / ROLLS);
	printf("Number of losses:%d\n",numOfLose);
	printf("Probability of losing:%.3f\n",(float)numOfLose  / ROLLS);
}

结尾:

  事由是郁闷了点。但从学习的角度上,我希望各位大虾能给我上述代码批评及建议,毕竟我E文太差不知道题目是否理解的对,也望各位给出建议。我也好从中学到些东西,知识本身是可贵的,小弟在此拜谢了!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值