- handToDice(hand)
Write the (very short) function handToDice(hand) that takes a hand, which is
a 3-digit integer, and returns 3 values, each of the 3 dice in the hand.
For example:
assert(handToDice(123) == (1,2,3))
assert(handToDice(214) == (2,1,4))
assert(handToDice(422) == (4,2,2))
Hint: You might find // and % useful here, and also getKthDigit.
- diceToOrderedHand(a, b, c)
Write the function diceToOrderedHand(a, b, c) that takes 3 dice
and returns them in a hand, which is a 3-digit integer. However,
even if the dice are unordered, the resulting hand must be
ordered so that the largest die is on the left and smallest
die is on the right. For example:
assert(diceToOrderedHand(1,2,3) == 321)
assert(diceToOrderedHand(6,5,4) == 654)
assert(diceToOrderedHand(1,4,2) == 421)
assert(diceToOrderedHand(6,5,6) == 665)
assert(diceToOrderedHand(2,2,2) == 222)
Hint: You can use max(a,b,c) to find the largest of 3 values,
and min(a,b,c) to find the smallest.
- playStep2(hand, dice)
This is the most complicated part. Write the function playStep2(hand, dice)
that plays step 2 as explained above. This function takes a hand,
which is a 3-digit integer,
and it also takes dice, which is an integer containing all the future
rolls of the dice. For example, if dice is 5341, then the next roll
will be a 1, then the roll after that will be a 4, then a 3, and finally
a 5. Note that in a more realistic version of this game, instead
of hard-coding the dice in this way, we'd probably use a random-number
generator.
With that, the function plays step2 of the given hand, using the given
dice to get the next rolls as needed. At the end, the function
returns the new hand, but it has to be ordered, and the function
also returns the resulting dice (which no longer contain the
rolls that were just used).
For example:
assert(playStep2(413, 2312) == (421, 23))
Here, the hand is 413, and the future dice rolls are 2312. What happens?
Well, there are no matching dice in 413, so we keep the highest die,
which is a 4, and we replace the 1 and the 3 with new rolls. Since
new rolls come from the right (the one's digit), those are 2 and 1.
So the new hand is 421. It has to be sorted, but it already is.
Finally, the dice was 2312, but we used 2 digits, so now it's just 23.
We return the hand and the dice, so we return (421, 23).
Here are some more examples. Be sure you carefully understand them:
assert(playStep2(413, 2345) == (544, 23))
assert(playStep2(544, 23) == (443, 2))
assert(playStep2(544, 456) == (644, 45))
Hint: You may wish to use handToDice(hand) at the start
to convert the hand into the 3 individual dice.
Hint: Then, you may wish to use diceToOrderedHand(a, b, c)
at the end to convert the 3 dice back into a sorted hand.
Hint: Also, remember to use % to get the one's digit, and use //=
to get rid of the one's digit.
- score(hand)
Almost there... Now write the function score(hand) that takes
a 3-digit integer hand, and returns the score for that hand
as explained in step4 above. For example:
assert(score(432) == 4)
assert(score(532) == 5)
assert(score(443) == 10+4+4)
assert(score(633) == 10+3+3)
assert(score(333) == 20+3+3+3)
assert(score(555) == 20+5+5+5)
Hint: The structure of this function is actually quite similar
to the previous function.
- bonusPlayThreeDiceYahtzee(dice)
Ok, we've made it to the last function: bonusPlayThreeDiceYahtzee(dice),
the function that actually earns the 2.5 bonus points!
This function takes one value, the dice with all the rolls for
a game of 3-Dice Yahtzee. The function plays the game -- it does step1
and gets the first 3 dice (from the right), then it does step2 twice
(by calling playStep2, which you already wrote), and then it computes
the score (by calling score, which you already wrote). The
function should return two values -- the resulting hand and
the score for that hand. For example:
assert(bonusPlayThreeDiceYahtzee(2312413) == (432, 4))
assert(bonusPlayThreeDiceYahtzee(2315413) == (532, 5))
assert(bonusPlayThreeDiceYahtzee(2345413) == (443, 18))
assert(bonusPlayThreeDiceYahtzee(2633413) == (633, 16))
assert(bonusPlayThreeDiceYahtzee(2333413) == (333, 29))
assert(bonusPlayThreeDiceYahtzee(2333555) == (555, 35))