このコードの目的: CRAPS の 100 ゲームをシミュレートし、第 1 ラウンドの敗北数、第 1 ラウンドの勝利数、第 2 ラウンドの敗北数 PLUS ポイント、および第 2 ラウンドの勝利数 PLUS ポイントを記録します。
CRAPS のルールに慣れていない方。基本的には 2 つのサイコロを振り、結果が合計 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);
}