{-# LANGUAGE LambdaCase #-}

{-|

Module      : Yahtzee
Description : Code from "Good Design an Type Safety in Yahtzee"
Copyright   : © Frank Jung, 2019
License     : GPL-3

Yahtzee test suite from blog
<http://h2.jaguarpaw.co.uk/posts/good-design-and-type-safety-in-yahtzee Good Design and Type Safety in Yahtzee>

-}

module Yahtzee (DiceChoice (..), DiceVals, allRolls) where

-- | Dice values from 1 to 6.
type DiceVals = [Word]

-- | Keep the dice, or re-roll?
data DiceChoice = Keep Word | Reroll

-- | Generate all rolls from a given state.
allRolls :: [DiceChoice] -> [DiceVals]
allRolls :: [DiceChoice] -> [DiceVals]
allRolls = (DiceChoice -> DiceVals) -> [DiceChoice] -> [DiceVals]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((DiceChoice -> DiceVals) -> [DiceChoice] -> [DiceVals])
-> (DiceChoice -> DiceVals) -> [DiceChoice] -> [DiceVals]
forall a b. (a -> b) -> a -> b
$ \case
  DiceChoice
Reroll -> [Word
1..Word
6]
  Keep Word
v -> [Word
v]