2013-04-13 65 views
2

我做我的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++,以便分数保持全局。

请记住,我是新来很多这个,所以任何帮助将不胜感激。

+1

滚动两个六面骰子不会在2和12之间产生均匀的加权概率。您将不匹配的值传递给您的打印语句,这就是为什么最终得到第e'0'打印输出 - 打开警报级别! –

+0

请不要编辑你的问题。它使人们的答案荒谬。您可以随时*添加*新信息,但请不要替换之前的内容。 –

+0

@CarlNorum对不起,我甚至都没有考虑过这会如何看待这个帖子。再次对不起 –

回答

5

我看到在您的main陈述if例如一个主要问题:

if (r = 7 || 11) 

您使用=分配,而不是==平等。接下来的问题是你似乎对||的工作原理有误解。它看起来像你想检查r或者是711这实际上是:

if((r == 7) || (r == 11)) 

您还你do循环在这里创建本地temproll

int temproll; 
do 
{ 
     int temproll = randomgenerator(); 
     ^^^^^^^^^^^^ 
}while (temproll != 7 || point); 

这意味着价值您在do循环中生成的循环丢失,并且随后的if语句将检查未定义的值。至于我可以告诉你可能是指的while是这样的:

while (temproll != 7 && temproll != r); 

这意味着虽然temproll7,它不是r。需要

而且在最后的战绩打印出来加以固定:

printf("%lf",&wins); 
printf("%lf",&losses); 

lf格式是double和你正在服用的wins,而不是价值的地址,它应该是:

printf("%d",wins); 
printf("%d",losses); 
+0

+1。而'||'不能像那样工作。 –

+0

@CarlNorum是的,我发布之前,我的意思是 –

+0

这不是如何布尔||也可以。 r == 2 || r == 3 ... –

2

你有一大堆的逻辑错误在你的代码

if (r == 7 || r ==11) { 
    //^^^^should be logical equal not assignment and r== cannot be skipped for 11 
    wins++; 
} 
else if (r = 2 || 3 || 12) { 
    //similar error as above 
    losses++; 
} 
else if (r = 4 || 5 || 6 || 8 || 9 || 10) { 
    //similar error as above 
    int point = r; 
    int temproll; 
    do 
    { 
     int temproll = randomgenerator(); 

    }while (temproll != 7 || point); //^^same error here 

    if (temproll = 7) { 
     //^^should be temproll ==7 
     losses++; 
    } 
    else if (temproll = point) { 
     //similar error 
     wins++; 
    } 

您的更新后,有一个问题就在这里:

int temproll; 
    //remove line below 
    //int *tr = &temproll; 
    do 
    { 
     //*tr = randomgenerator(); 
     //replace this line with: 
     temproll = randomgenerator(); 

    }while (temproll != 7 || temproll != point); 

的胜利和损失打印报表有错误:

printf("%d",&wins); 
//^^^^^^^^^^^remove & 
printf("\nLosses\n"); 
    printf("%d",&losses); 
    ///^^^^^^^^^same thing, remove & 

您当前的代码将永远不会结束,因为你在这里有死循环:

else if (r == 4 || r == 5 || r == 6 || r == 8 || r == 9 || r == 10) { 
    int point = r; //so r cannot be 7, otherwise, will not enter this if block 
    int temproll; 
    int *tr = &temproll; 
    do 
    { 
     *tr = randomgenerator(); 

    }while (temproll != 7 || temproll != point); 
    //you will need temproll ==7 and temproll ==point to exit this loop 
    //this means point ==7. however, pointer can never be 7. dead loop, keep running... 
} 
+0

@WhozCraig是的,他们是逻辑错误,所以我更新了。谢谢! – taocp

+0

谢谢你,我会马上更新代码! –

+0

@KaneCharles不客气 – taocp