我为Yahtzee编写了代码。我的代码询问用户是否要玩Yahtzee或顺子。然后滚动直到达到Yahtzee或直线。
它运行良好,但是需要花费很长时间和很多回合,因此我希望我的代码更有效并能够重新发布。因此,如果滚动的数字是1 2 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;
}
答案 0 :(得分:0)
我帮助教人们如何编码,这是一个常见问题。
答案是...只是开始。
您大概知道从哪里开始,知道基本结构,这就是您所需要的。
不要一口气做所有的事情,只是走一小步,测试它是否有效,然后再做一个。
您将犯错,但事情将无法正常进行,这是完全正常的现象。学习编码有点像学习马拉松走路,跌跌撞撞,跌落在地上,但是只需要振作起来然后再次向前迈进即可。每次尝试时,您都可以跑得更远,进度会令人振奋。
编辑:响应Q的编辑,具体询问如何重新滚动。
因此要掷出第二个骰子,您需要执行以下代码:
die2 = rand() % 6 + 1;
要重新滚动它,请使用同一行。
最终,您将想要将代码转换为使用六个骰子的数组。这将使重复更少的代码更加动态和智能。但这是第二个版本,只需一些小步骤,先完成一些工作,然后再进行其他工作,然后对其进行调整以使其工作得更好,等等。