c - 需要在 Yahtzee 中使计算机重新滚动
问题描述
我为 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;
}
解决方案
我帮助教人们如何编码,这是一个常见的问题。
答案是……刚开始。
您大致知道从哪里开始,您知道基本结构,这就是您所需要的。
不要试图一口气做完所有事情,只需迈出一小步,测试它是否有效,然后再迈出另一步。
你会犯错误,事情不会奏效,这是完全正常和意料之中的。学习编程有点像通过马拉松学习走路,你会绊倒,你会摔倒在脸上,但你只需要振作起来,再次向前冲。每次你尝试,你都能跑得更远一点,进步会令人振奋。
编辑:回应Q的编辑,特别询问如何重新滚动。
所以要掷第二个骰子,你有以下代码行:
die2 = rand() % 6 + 1;
要重新滚动它,请使用同一行。
最终,您将希望将代码转换为使用六个骰子的数组。这将允许使用更少重复的更动态和更智能的代码。但那是第二版,小步骤,让一些东西工作,然后让其他东西工作,然后调整它们以更好地工作等等。
推荐阅读
- latex - 如何在 Latex 中仅将一位作者与其他三位作者居中对齐
- vue.js - 如何使用 vuedraggable 处理数组数组
- unity3d - 在 Unity 中,如何检测窗口是否正在调整大小以及窗口是否已停止调整大小
- c# - viewmodel 参数转换为 json 用于 ajax 请求
- php - php-xlsxwriter:如何连续获取颜色
- java - 传递 POINT 数据类型值的 JSON 格式是什么?
- linux - 无法从 Windows 10 访问新的 Linux Samba 共享
- react-native - 更新 react-native 的不同方法
- javascript - 有没有一种简单的方法可以将对象名称转换为字符串,然后将该字符串添加到对象中名为 objName 的属性中?
- node.js - 如何从我的 REST API 中获取数据以填充材料表?