2

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

任务 2. 废话。

在掷骰子游戏中,“及格线”赌注如下进行。使用两个六面骰子,掷骰子的第一轮骰子称为“Come out Roll”。出局数为 7 或 11 时下注立即赢,出局数为 2、3 或 12 时下注。如果出局数为 4、5、6、8、9 或 10,则这个数字变成了“重点”。玩家继续掷骰子,直到掷出 7 点或点数。如果点数先滚动,则玩家赢得赌注。如果玩家先掷出 7,则玩家输。

编写一个使用上述规则玩掷骰子游戏的程序,以便在没有人工输入的情况下模拟游戏。程序不应要求下注,而应只计算玩家是否会赢或输。创建一个模拟掷两个骰子并返回总和的函数。添加一个循环,使程序可以玩 10,000 场比赛。

添加计数器来计算玩家赢了多少次,以及玩家输了多少次。在10,000 场比赛结束时,计算获胜的概率,即Wins / (Wins + Losses) 并输出该值。从长远来看,谁会赢得最多的掷骰子游戏,你还是房子?

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

# 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++andlosses++以便全局保存分数。

请记住,我对这方面很陌生,因此将不胜感激任何帮助。

4

2 回答 2

5

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

if (r = 7 || 11)

您使用的是=赋值而不是==相等。下一个问题是您似乎对如何||工作有误解。看起来您想检查是否r711实际上是:

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 );

这意味着 while temprollis not7和 it is not r

最后打印出的输赢也需要修正:

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

lf格式是 fordouble并且您使用的是地址而wins不是值,它应该是:

printf("%d",wins);
printf("%d",losses);
于 2013-04-13T02:17:20.680 回答
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...
 }
于 2013-04-13T02:19:07.347 回答