【问题标题】:C: Craps / Dice GameC:掷骰子/骰子游戏
【发布时间】:2012-02-29 15:51:50
【问题描述】:

此代码的目的:模拟 100 场 CRAPS 游戏,并记录第一轮输、第一轮获胜、第二轮输 PLUS 点和第二轮获胜 PLUS 点数。

那些不熟悉 CRAPS 规则的人;你基本上掷了两个骰子,如果结果不是总数为 2、3 或 12,你可以再次掷骰(你在该回合中掷出的数字被保留并添加到你的分数中)。如果您掷出 7 或 11,您将自动获胜。

这是我目前所处的位置:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;
printf("This program will simulate the game of craps for 100 times.\n");

for (i=0; i<100; i++) {
    d1 = rand()%6+1;
    d2 = rand()%6+1;
    sumd = d1 + d2;

    if (sumd==7 || sumd==11) {
        printf("You rolled a 7 or an 11, you win.\n");
        winf++;
    }
    if (sumd==2 || sumd==3 || sumd==12) {
        printf("You rolled a 12, a 3, or a 2, you lose.\n");
        lostf++;
    }
    if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
        while (1) {
            d1 = rand()%6+1;
            d2 = rand()%6+1;
            sumd2 = d1 + d2;

            if (sumd2==sumd){ 
                printf("You rolled your points, you win.\n");
                winp++;
            break;}
            if (sumd==7){ 
                printf("You rolled a 7, you lose.\n");
                lostp++;
            break;}
        }
    }
}

printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. ", winf, lostf, winp, lostp);
}

我对你的要求是,你给我一些选项,让我可以选择如何保留这些点以在最后打印??

另外,我觉得我的代码可以写得更好,更少冗余,建议?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

int main ()
{
int i,d1,d2,sumd,sumd2;
double winf = 0, lostf = 0, winp = 0, lostp = 0;

printf("This program will simulate the game of craps for 100 times. Press any key to continue.\n");
//getchar();

for (i=0; i<100; i++) {
    d1 = rand()%6+1;
    d2 = rand()%6+1;
    sumd = d1 + d2;

switch(sumd){
    case 7:
    case 11:
        printf("You rolled %d, you win.\n", sumd);
        winf++;
        break;
    case 2:
    case 3:
    case 12:
        printf("You rolled %d, you lose.\n", sumd);
        lostf++;
        break;
    default:
        while (1) {
            d1 = rand()%6+1;
            d2 = rand()%6+1;
            sumd2 = d1 + d2;

            if (sumd2==sumd){ 
                printf("You rolled your points(%d), you win.\n",sumd);
                winp++;
            break;}
            if (sumd2==7){ 
                printf("You rolled a 7, you lose.\n");
                lostp++;
            break;}
        }
}

}
printf("First roll wins: %lf, First roll loses: %lf, Second roll wins: %lf, Second roll loses: %lf. \n", winf, lostf, winp, lostp);
}

【问题讨论】:

  • 提示:不要使用printf("You rolled a 7 or an 11, you win.\n"); 使用printf("You rolled a %d, you win.\n",sumd);
  • while(1) if (sumd==7) 永远不会是真的。那是错字吗?你的意思是if(sumd2 == 7)
  • 是的,它必须是if (sumd2==7)
  • 注意:即使rand() 本身是统一的,rand() % 6 很可能不是并且骰子会被加载。原因是,RAND_MAX+1 不是 6 的倍数。
  • 例如,您可以在 rand() 返回值时继续调用它>= (RAND_MAX+1U)/6*6。一旦值变小,您可以使用它执行%6 并继续。请注意,1U 中的U 会强制将RAND_MAXsigned int 转换为unsigned int,并避免在RAND_MAX=INT_MAX 的情况下发生加法溢出(相当典型的情况)。

标签: c simulation dice


【解决方案1】:

您将结果放入整数中以在最后打印的解决方案看起来很合理。如果我正确理解了这个问题,似乎 winp 和 lostp 应该添加 sumd2 而不是仅仅增加。还是它已经正常工作了,但我误读了这个问题?

您可能还想查看switch 声明:

switch(sumd){
    case 7:
    case 11:
        //existing code goes here
        break;

    case 2:
    case 3:
    case 12:
        //more existing code
        break;

    default:
        //code for games that don't end on the first turn
        break;
}

【讨论】:

    【解决方案2】:

    你可以很容易地浓缩这两种情况

    d1 = rand()%6+1;
    d2 = rand()%6+1;
    sumd2 = d1 + d2;
    

    变成一个函数:

    int rolldice(){
        int d1,d2;
        d1 = rand()%6+1;
        d2 = rand()%6+1;
        return d1 + d2;
    }
    

    或以单行形式:

    int rolldice(){
        return (rand()%6)+(rand()%6)+2;
    }
    

    那你会写

    sumd = rolldice();
    

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 2011-01-19
      相关资源
      最近更新 更多