C编程'掷骰子'游戏

时间:2013-04-13 02:14:39

标签: c random numbers generator

我正在为我的C编程课程做一个学习任务,我被要求做以下事情:

  

任务2.掷骰子。

     

在掷骰子游戏中,“通行证”下注如下进行。使用两个六面骰子,   掷骰子的第一卷掷骰被称为“滚出来”。投注   当出来的滚动是7或11时立即获胜,当滚出时滚动失败   如果在出来的卷上滚动4,5,6,8,9或10,则该数字变为“点”。玩家继续滚动骰子直到7或点滚动。如果该点首先滚动,则玩家赢得下注。如果玩家首先掷出7,那么玩家   输。

     

使用上述规则编写一个播放Craps游戏的程序   模拟没有人为输入的游戏。该计划不是要求下注   应该只计算玩家是赢还是输。创建一个模拟的函数   滚动两个骰子并返回总和。添加循环以使程序播放10,000   游戏。

     

添加计数器,计算玩家获胜的次数,以及次数   球员输了。在10,000场比赛结束时,计算获胜的概率,即   胜利/(胜利+损失)并输出此值。从长远来看,谁将赢得胜利   Craps的大多数游戏,你还是房子?

这是我到目前为止所写的内容:

# 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;
}

程序总是以:

结束
Wins:
0.000000
Losses:
0.000000

它应该显示(超过10000)该程序获胜的次数和丢失的次数。有人能帮我解决这个问题吗?我怀疑我可能需要使用指针而不是wins++losses++,以便分数保持全局。

请记住,我是很多新手,所以任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:5)

我在if中的main语句中看到了一个主要问题,例如:

if (r = 7 || 11)

您正在使用=分配而不是==平等。接下来的问题是你似乎对||的工作原理有误解。您好像想要检查r7还是11实际上是:

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

您还在temproll循环中创建了一个本地do

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

这意味着一旦离开,您在do循环中生成的值就会丢失,后续的if语句将检查未定义的值。据我所知,你可能意味着while是这样的:

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

这意味着temproll不是7且不是r

最后打印输出的输赢还需要修复:

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

lf格式适用于double,您使用wins的地址而不是值,它应该是:

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

答案 1 :(得分: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...
 }