程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 擲骰子游戲-C語言實現

擲骰子游戲-C語言實現

編輯:關於C語言

作者:半點閒
博客:blog.csdn.net/cg_i
郵箱:[email protected]
參考書籍:《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 
#include 
#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文太差不知道題目是否理解的對,也望各位給出建議。我也好從中學到些東西,知識本身是可貴的,小弟在此拜謝了!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved