开发者

Need some help with a dice rolling algorithm

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 开发者_StackOverflow中文版there to the previous value.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜