Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 需要在Yahtzee进行计算机重新滚动_C - Fatal编程技术网

C 需要在Yahtzee进行计算机重新滚动

C 需要在Yahtzee进行计算机重新滚动,c,C,我给亚特泽编了个密码。我的代码询问用户他们是想玩Yahtzee牌还是直牌。然后滚动,直到达到Yahtzee或直线 它工作得很好,但是它需要很长的时间和太多的循环,所以我希望我的代码更高效,并且能够自己重新运行。因此,如果滚动的数字是1 2 3 2,我希望计算机重新滚动1和3,直到它们变成2 我刚刚开始学习如何用C++编程,所以我对编程的知识有限。我知道如何使用循环、开关、if和else语句。任何建议或简单的解决方案都会有所帮助 我对如何让电脑重新滚动有一些想法,但它们似乎不起作用。如果您能够找出

我给亚特泽编了个密码。我的代码询问用户他们是想玩Yahtzee牌还是直牌。然后滚动,直到达到Yahtzee或直线

它工作得很好,但是它需要很长的时间和太多的循环,所以我希望我的代码更高效,并且能够自己重新运行。因此,如果滚动的数字是1 2 3 2,我希望计算机重新滚动1和3,直到它们变成2

我刚刚开始学习如何用C++编程,所以我对编程的知识有限。我知道如何使用循环、开关、if和else语句。任何建议或简单的解决方案都会有所帮助

我对如何让电脑重新滚动有一些想法,但它们似乎不起作用。如果您能够找出如何使计算机重新运行,请向我提供对代码的修改,并指导我如何执行

int die3 = 0;
int die4 = 0;
int die5 = 0;
int roundCounter = 0;
bool yahtzeeNotFound = true;
bool yahtzeeGame = true;
char game[4] = "";

//ask user if they want to play for Yahtzee or Straight
printf("Lets play Yahtzee!\n Do you want to play for a Yahtzee or a Straight?  (Y/S)");
scanf("%s", game);

//if user wants to play Yahtzee, program rolls for same numbers from all the dice.
if (tolower(game[0]) == 'y')
{
    yahtzeeGame = true;

    //this is included so that new numbers are rolled after each round.
    srand(time(0));

    //if the condition of an unfound Yahtzee is met, the program proceeds to enter the loop.
    while (yahtzeeNotFound)
    {
        roundCounter++;

        //the dice are all rolled at same time.
        die1 = rand() % 6 + 1;
        die2 = rand() % 6 + 1;
        die3 = rand() % 6 + 1;
        die4 = rand() % 6 + 1;
        die5 = rand() % 6 + 1;

        //the numbers rolled are printed for user to see.
        //the counter counts each round, so user knows how many rounds it take for a Yahtzee.
        printf("ROUND # %d\n\n", roundCounter);
        printf("   Die 1 = %d\n", die1);
        printf("   Die 2 = %d\n", die2);
        printf("   Die 3 = %d\n", die3);
        printf("   Die 4 = %d\n", die4);
        printf("   Die 5 = %d\n\n\n\n", die5);

        //when all the dice have rolled the same number, a Yahtzee is achieved.
        if ((die1 == die2) && (die1 == die3) && (die1 == die4) && (die1 == die5))

        {
            printf(" Congratulations! You finally reached Yahtzee after %d rounds!\n", roundCounter);
            //when the Yahtzee is achieved, the program exits the loop.
            break;
        }
    }
}

else
{
//if the user does not play for a Yahtzee, they must play for a straight.
yahtzeeGame = false;

//this ensures that new number are rolled after each round.
srand(time(0));

//the program enters the loop.
while (yahtzeeNotFound)
{
    //the counter is declared.
    roundCounter++;

    //the 5 dice are rolled at the same time.
    die1 = rand() % 6 + 1;
    die2 = rand() % 6 + 1;
    die3 = rand() % 6 + 1;
    die4 = rand() % 6 + 1;
    die5 = rand() % 6 + 1;

    //the numbers are printed out with the round number for the user to see.
    printf("ROUND # %d\n\n", roundCounter);
    printf("   Die 1 = %d\n", die1);
    printf("   Die 2 = %d\n", die2);
    printf("   Die 3 = %d\n", die3);
    printf("   Die 4 = %d\n", die4);
    printf("   Die 5 = %d\n\n\n\n", die5);

    //if the numbers rolled have unique values, the user has a Straight.
    if ((die1 != die2) && (die1 != die3) && (die1 != die4) && (die1 != die5) && (die2 != die3)
            && (die2 != die4) && (die2 != die5) && (die3 != die4) && (die3 != die5) && (die4 != die5))
    {
        // The user is told how many rounds it took for them to get a Straight.
        printf(" Congratulations! You finally reached a Straight after %d rounds!\n", roundCounter);

        //when the user has a Straight, the program exits the loop.
        break;
    }
}
}
return 0;
}

我帮助教人们如何编码,这是一个常见的问题

答案是。。。刚刚开始

你大概知道从哪里开始,你知道基本结构,这就是你所需要的

不要试图一次完成所有工作,只需迈出一小步,测试它是否有效,然后再迈出一小步

你会犯错误,事情不会成功,这是完全正常的,也是意料之中的。学习编码有点像通过跑马拉松来学习走路,你会绊倒,你会摔倒在地,但你只需要振作起来,再次向前。每次你尝试,你都能跑得更远一点,进步是令人振奋的

编辑:响应Q的编辑,特别是询问如何重新滚动

因此,要掷第二个骰子,您有以下代码行:

die2 = rand() % 6 + 1;
要重新滚动,请使用同一行


最终,您将希望将代码转换为使用数组来表示六个骰子。这将允许使用更少重复的更动态、更智能的代码。但这是第二版,小步骤,让某些东西工作,然后让其他东西工作,然后调整它们以更好地工作等等。

Thx您能给我一些指导吗。我从谷歌上获得了大部分的C++知识,所以我不知道我在做什么。我曾考虑使用switch语句询问用户要重新掷哪个骰子,但这使我的代码过于复杂。如果您想要更结构化的方法,可以尝试一些教程。我不会太担心代码过于复杂,也不会有错误的处理方法。使用switch语句,让它工作起来,然后寻找代码中重复的元素并重新构造以改进它。我不知道如何使用数组。我在网上找到了这个作业,所以我决定试一试。有没有一种方法可以利用我已经拥有的东西让电脑重新运行以提高效率?我不想欺骗我的学习过程或这个网站的目的,但我只想要一个解决方案。如果有人能编辑我的代码使其工作,这将对我有很大帮助。我已经在这上面坐了几天了,我没有任何进展。如果我能看到其他人如何处理和解决同一个问题,对我来说会更容易。你可能会在类似主题的问题中找到一些想法,例如。在使用十进制时,这是一个相当复杂的问题,但在使用十六进制时,这是一个相对简单的问题。这可能是参加十六进制数学速成班的时候了;在整个程序中属于一个不重复的位置,理想情况下位于//靠近主程序的开头。设定伪随机数生成器的种子仅需要一次,除非您有意通过将种子重置为已知值来重复序列,但您没有这样做。因此,我应该只在开始时包含srandtime0,并从if和else语句中删除它?