While planning a games night with friends, the group decides they will revisit their childhood favorites. Your German friend proposes Mensch ärgere Dich nicht (Man, Don't Get Angry), a classic in many European countries (similar to the English Ludo or the North American Parcheesi).
To prepare for the game, you decide to implement the dice rolling function in Elixir.
In Mensch ärgere Dich nicht, players roll one 6-sided die to determine their next move. If they are lucky and roll 6, they can roll and move again.
Implement the d6/0
function that will randomly choose one integer between 1 and 6 on each call.
MenschAergereDichNicht.d6()
# => 1
MenschAergereDichNicht.d6()
# => 5
Implement the roll/0
function. It should return a stream of dice rolls. The dice rolls should follow the rules of the game. If I roll a six, I can roll again. Otherwise, stop rolling. Use the d6/0
function defined in the previous step.
MenschAergereDichNicht.roll() |> Enum.to_list()
# => [5]
MenschAergereDichNicht.roll() |> Enum.to_list()
# => [6, 3]
To make it easier to test your code, allow using a rigged die.
Extend the roll/0
function. It should accept an optional argument - a function. If the argument is passed, use it to do the die roll. Otherwise, fall back to MenschAergereDichNicht.d6/0
.
MenschAergereDichNicht.roll(fn -> 3 end) |> Enum.to_list()
# => [3]