gpt4 book ai didi

c - 需要在 Yahtzee 中重新启动计算机

转载 作者:行者123 更新时间:2023-11-30 20:30:58 25 4
gpt4 key购买 nike

我为 Yahtzee 编写了代码。我的代码询问用户是否想玩 Yahtzee 还是顺子。然后它会滚动,直到获得快艇或直道。

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

我刚刚开始学习如何用 C++ 编程,所以我对编程主题的了解有限。我知道如何使用循环、switch、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;

要重新滚动它,请使用同一条线。

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

关于c - 需要在 Yahtzee 中重新启动计算机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52808624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com