我做我的C语言编程课程学习任务,我被要求做到以下几点:C程序设计“骰子”游戏
Task 2. Craps.
In the game of Craps, a "Pass Line" bet proceeds as follows. Using two six-sided dice, the first roll of the dice in a craps round is called the "Come out Roll." The bet immediately wins when the come out roll is 7 or 11, and loses when the come out roll is 2, 3, or 12. If 4, 5, 6, 8, 9, or 10 is rolled on the come out roll, that number becomes "the point." The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If the player rolls a 7 first, then the player loses.
Write a program that plays the game of Craps using the rules stated above so that it simulates a game without human input. Instead of asking for a wager, the program should just calculate if the player would win or lose. Create a function that simulates rolling the two dice and returns the sum. Add a loop so that the program plays 10,000 games.
Add counters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, compute the probability of winning, i.e. Wins/(Wins + Losses) and output this value. Over the long run, who is going to win the most games of Craps, you or the house?
这是我到目前为止写:
# include <stdio.h>
# include <stdlib.h>
# include <time.h>
// Craps Program
// Written by Kane Charles
// Lab 2 - Task 2
// 7 or 11 indicates instant win
// 2, 3 or 12 indicates instant los
// 4, 5, 6, 8, 9, 10 on first roll becomes "the point"
// keep rolling dice until either 7 or "the point is rolled"
// if "the point" is rolled the player wins
// if 7 is rolled then the player loses
int wins = 0, losses = 0;
int r, i;
int N = 1, M = 12;
int randomgenerator();
main(void){
/* initialize random seed: */
srand (time(NULL));
/* generate random number 10,000 times: */
for(i=0; i < 10000 ; i++){
int r = randomgenerator();
if (r == 7 || r == 11) {
wins++;
}
else if (r == 2 || r == 3 || r == 12) {
losses++;
}
else if (r == 4 || r == 5 || r == 6 || r == 8 || r == 9 || r == 10) {
int point = r;
int temproll;
int *tr = &temproll;
do
{
*tr = randomgenerator();
}while (temproll != 7 || temproll != point);
if (temproll == 7) {
losses++;
}
else if (temproll == point) {
wins++;
}
}
}
printf("Wins\n");
printf("%d",&wins);
printf("\nLosses\n");
printf("%d",&losses);
}
int randomgenerator(){
r = M + rand()/(RAND_MAX/(N - M + 1) + 1);
return r;
}
该程序将永远结束:尽管它是为了显示(满分10000)程序是如何多次荣获以及如何人
Wins:
0.000000
Losses:
0.000000
它失去了它的几倍。有人能帮我解决这个问题吗?我怀疑我可能需要使用指针而不是wins++
和losses++
,以便分数保持全局。
请记住,我是新来很多这个,所以任何帮助将不胜感激。
滚动两个六面骰子不会在2和12之间产生均匀的加权概率。您将不匹配的值传递给您的打印语句,这就是为什么最终得到第e'0'打印输出 - 打开警报级别! –
请不要编辑你的问题。它使人们的答案荒谬。您可以随时*添加*新信息,但请不要替换之前的内容。 –
@CarlNorum对不起,我甚至都没有考虑过这会如何看待这个帖子。再次对不起 –