需要一些有关掷骰子算法的帮助

发布于 2024-10-11 18:58:21 字数 363 浏览 3 评论 0原文

我将如何对以下方法的算法进行伪编码:

  • 滚动一种骰子 - 4、6、8、10 或 12 面

  • 最多可滚动 10 个此类骰子

  • 如果超过一半的骰子是 1,则向它们打印一条消息,表明它们已损坏并结束程序

  • 如果任何骰子与掷出的骰子类型相同,则从该组中取最高值。以及重新掷骰子,使其等于所掷骰子类型的值。

    ^^^^IE - 假设您有 3 个六面骰子,您滚动它们,得到 4、2 和 6。您取 6 的值,因为它是最高的。然后你重新掷骰子,结果是六。如果你得到了 6,则将这 6 添加到之前的 6 中并重新滚动。如果不是,您只需将最高的骰子添加到先前的值即可。

How would I go about pseudocoding the algorithm for a method that:

  • Rolls a type of Die - 4 , 6 , 8 , 10 , or 12 sided

  • Can roll up to ten of this type of Die

  • If more than half of the die are 1's, print them a message that they bust and end the program

  • If any of the die are equal to the type of die rolled, take the highest value from the group. As well as re-roll the die that equaled the value of the type of die rolled.

    ^^^^I.E. - Let's say you have 3 six sided die, you roll them and you get a 4, 2 and 6. You take the value of 6, since it is the highest. Then you re-roll the die that was a six. If you get a six you add that six to the previous six AND re-roll. If not you just add the highest die there to the previous value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一笔一画续写前缘 2024-10-18 18:58:21

我认为你的问题指出了你觉得这很困难的原因。你试图在一个地方解决太多问题,这会让人不知所措。您不想创建一个单一的方法来做到这一点。您将想要创建多个。首先将问题分解为其组成部分。

注意:我并不是以面向对象的方式来处理这个问题,以使答案更容易解析。我鼓励您更详细地考虑设计。

要求 1:滚动一种骰子 - 4、6、8、10 或 12 面

好的 - 所以我们需要一些方法类似于:

int Roll(int Sides);

基本上 Roll 只是返回 1 和边(含)之间的随机值。

要求 2:最多可以滚动 10 个此类骰子

这可能是一个 for 循环。

要求 3:如果超过一半的骰子是 1,则向它们打印一条消息,表明它们已失败并结束程序

此要求意味着您将每次调用 Roll 的结果存储在集合中 - 例如、List 或 int[](整数数组)。

接下来,它表示您正在迭代该集合并计算“1”的卷数。如果计数大于总掷数的一半,则结束程序。计数很容易(for循环或foreach可能是你最好的选择),并且你知道制作了多少卷(既通过集合中的项目数量,也因为在制作卷时你的for循环上有一个计数器.. 要求4:如果有任何一个骰子与

所掷骰子的类型相同,则从该组中取出等于该类型值的骰子。 再次强调

- 您需要迭代结果集并执行操作请求,我不会尝试通过将此规则与前一个规则相结合来“优化”您的解决方案 - 它只会使解决方案变得复杂。真正的好处。

I think your question points to the reason you are finding this difficult. You are trying to solve too much in one place and that becomes overwhelming. You don't want to create a single method to do that. You will want to create several. Start by decomposing the problem into it's constituent parts.

Note: I'm not approaching this in an OO fashion to make the answer a bit easier to parse. I would encourage you to think about the design in more detail.

Requirement 1: Rolls a type of Die - 4 , 6 , 8 , 10 , or 12 sided

Ok - so we need some method similar to:

int Roll(int sides);

Basically Roll just returns a random value between 1 and sides (inclusive).

Requirement 2: Can roll up to ten of this type of Die

This would likely be a for loop.

Requirement 3: If more than half of the die are 1's, print them a message that they bust and end the program

This requirement implies that you are storing the results of each call to Roll in a collection - e.g., a List or an int[] (array of integers).

Next it says that you are iterating over that collection and counting the number of rolls that are "1". If count is greater than half the total number of rolls than you end the program. Counting is easy (for loop or foreach would probably be your best bet) and you know how many rolls were made (both by the number of items in the collection and because you had a counter on your for loop when the rolls were made ... so divide and compare.

Requirement 4: If any of the die are equal to the type of die rolled, take the highest value from the group. As well as re-roll the die that equaled the value of the type of die rolled.

Again - you need to iterate over the result set and perform the operation request. I would not attempt to "optimize" your solution by combining this rule with the previous rule - it will just convolute the solution for no real benefit.

捂风挽笑 2024-10-18 18:58:21

您的算法必须:

  1. 掷骰子
  2. 检查结果以查看一半是否为 1
  3. 检查并可能重新掷高数字
  4. 跟踪已接受的数字以及掷骰子的运行总数

然后实际执行的操作并不多给你的。

Your algorithm would have to:

  1. Roll the dice
  2. Check the results to see if half are 1s
  3. Check and possibly re-roll high numbers
  4. Keep track of the accepted numbers and running totals from the rolled dice

There is not much more to it then to actually do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文