繁体   English   中英

C:胡扯/骰子游戏

[英]C: Craps / Dice Game

该代码的目的:模拟CRAPS的100场比赛,并记录第一轮损失,第一轮胜利,第二轮损失加分和第二轮胜利加分的数量。

你们中那些不熟悉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);
}

您可以轻松地将两个事件的发生浓缩

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

进入aa函数:

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

您将结果以int结尾的解决方案看起来很合理。 如果我理解正确的问题,似乎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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM