dimanche 8 mars 2015

On Haskell, what is the linguistic way to represent a card effect for a TCG game?

I have a simple one-player Card Game:



data Player = Player {
_hand :: [Card],
_deck :: [Card],
_board :: [Card]}
$(makeLenses ''Player)


Some cards have an effect. For example, "Erk" is a card with the following effect:



Flip a coin. If heads, shuffle your deck.


I've implemented it as such:



flipCoin :: (RandomGen g) => Rand g Coin
flipCoin = getRandom

shuffleDeck :: (MonadRandom m, Functor m) => Player -> m Player
shuffleDeck = deck shuffleM

erk :: (RandomGen g) => Player -> Rand g Player
erk player = do
coin <- flipCoin
case coin of
Head -> deck shuffleM player
Tail -> return player


While this certainly does the job, I find an issue on the forced coupling to the Random library. I'd prefer a way to describe the logic of my game entirely independent from it. Something like that:



erk :: CardAction
erk = do
coin <- flipCoin
case coin of
Head -> shuffleDeck
Tail -> doNothing


I could, later on, have a runGame function that does the connection.



runGame :: (RandomGen g) => g -> CardAction -> Player -> Player


I'm not sure that would help. What is the correct, linguistic way to deal with this pattern?


Aucun commentaire:

Enregistrer un commentaire