C:掷骰子游戏

C:掷骰子游戏,c,simulation,dice,C,Simulation,Dice,此代码的用途:模拟100场骰子游戏,记录第一轮输、第一轮赢、第二轮输加分、第二轮赢加分的# 那些不熟悉垃圾规则的人;你基本上掷两个骰子,如果结果不是2,3,或12,你可以再次掷骰(你掷的那个回合的次数会被保留并加到你的点数上)。如果你掷7或11,你会自动获胜 这就是我目前的处境: #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> int mai

此代码的用途:模拟100场骰子游戏,记录第一轮输、第一轮赢、第二轮输加分、第二轮赢加分的#

那些不熟悉垃圾规则的人;你基本上掷两个骰子,如果结果不是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);
}
#包括
#包括
#包括
#包括
int main()
{
int i、d1、d2、sumd、sumd2;
双winf=0,lostf=0,winp=0,lostp=0;
printf(“此程序将模拟100次骰子游戏。\n”);

对于(i=0;i您将结果放在整数中以便在末尾打印的解决方案看起来是合理的。如果我正确理解了这个问题,winp和lostp似乎应该添加sumd2,而不是仅仅增加。或者它已经工作正常了,而我误读了这个问题

您可能还需要查看以下语句:

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

您将结果放在整数中以便在末尾打印的解决方案看起来是合理的。如果我正确理解了这个问题,那么winp和lostp似乎应该添加sumd2而不是仅仅递增。或者它已经工作正常了,而我误解了这个问题

您可能还需要查看以下语句:

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

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

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

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

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

提示:不要使用
printf(“你掷了一个7或11,你赢了。\n”);
使用
printf(“你掷了一个%d,你赢了。\n”,sumd);
中,而(1)
如果(sumd==7)
将永远不会是真的。这是打字错误吗?你的意思是
如果(sumd2==7)
?是的,如果(sumd2==7)
注意:即使
本身是统一的,
rand()%6
很可能不是,骰子被加载。原因是,
rand\u MAX+1
不是6的倍数。例如,您可以在它返回值>=
(rand\u MAX+1U)时继续调用
rand()
/6*6
。一旦值变小,您可以使用它执行
%6
,然后继续。请注意
1U
中的
U
强制将
RAND\U MAX
转换为
unsigned int
,并避免在
RAND\U MAX
=
int\U MAX
时出现加法溢出(一个相当典型的情况).提示:不要使用
printf(“你掷了一个7或11,你赢了。\n”);
使用
printf(“你掷了一个%d,你赢了。\n”,sumd);
中,而(1)
如果(sumd==7)
将永远不会是真的。这是打字错误吗?你的意思是
if(sumd2==7)
?是的,它需要是
如果(sumd2==7)
注意:即使
本身是统一的,
rand()%6
很可能不是,骰子被加载。原因是,
rand\u MAX+1
不是6的倍数。例如,您可以在它返回值>=
(rand\u MAX+1U)时继续调用
rand()
/6*6
。一旦值变小,您可以使用它执行
%6
,然后继续。请注意
1U
中的
U
强制将
RAND\U MAX
转换为
unsigned int
,并避免在
RAND\U MAX
=
int\U MAX
时出现加法溢出(一个相当典型的情况)。