3

Is there a better way to pass state between steps.

For example every time a card is taken form the deck we have to create a new Deck and return it so that the next step can use it etc

Is there a better way to do this using Cats for example?

trait BlackjackSteps {
    def gamerTakesTwoCards(deck: Deck): (Gamer, Deck)
    def dealerTakesTwoCards(deck: Deck): (Dealer, Deck)
    def isBlackjack(gamer: Gamer, dealer: Dealer): Option[Player]
    def gamerToDrawCards(gamer: Gamer, deck: Deck): Gamer
    def dealerToDrawCards(dealer: Dealer, deck: Deck, gamer: Gamer): Dealer
    def determineWinner(gamer: Gamer, dealer: Dealer): Player
  }

The Game:

  • a gamer and dealer play
  • they both draw two cards
  • if no 21 winner
  • players keep drawing cards until 17
  • highest player not over 21 points wins

EDIT -----

Thanks for the responses. I just want to see what people think of this approach?

How about this?

trait CardPicker {
    def pick(numberCards:Int): List[Card]
  }

  abstract class BlackjackSteps(cardPicker: CardPicker) {
    def gamerTakesTwoCards(gamer: Gamer): Gamer = {
      gamer.copy(cards = cardPicker.pick(2))
    }
    def dealerTakesTwoCards(dealer: Dealer): Dealer = {
      dealer.copy(cards = cardPicker.pick(2))
    }
    def isBlackjack(gamer: Gamer, dealer: Dealer): Option[Player] = {
     if(gamer.isBlackjack) Some(gamer)
     else if(dealer.isBlackjack) Some(dealer)
     else None
    }
    def gamerToDrawCards(gamer: Gamer): Gamer = {
      def drawCards(gamerPickingCards: Gamer): Gamer = gamer match {
        case _ if(gamerPickingCards.points < drawThreshold) =>
          drawCards(Gamer(gamerPickingCards.name, gamerPickingCards.cards ++ cardPicker.pick(1)))
        case _ => gamerPickingCards
      }
      drawCards(gamer)
    }
    def dealerToDrawCards(dealer: Dealer, gamer: Gamer): Dealer = {
      def drawCards(dealerPickingCards: Dealer): Dealer = dealer match {
        case _ if(dealerPickingCards.points < gamer.points) =>
          drawCards(Dealer(dealerPickingCards.name, dealerPickingCards.cards ++ cardPicker.pick(1)))
        case _ => dealerPickingCards
      }
      drawCards(dealer)
    }
    def determineWinner(gamer: Gamer, dealer: Dealer): Player = {
      if(gamer.points == dealer.points || gamer.points > dealer.points) gamer
      else dealer
    }
  }

Here CardPicker has side effects that we can delay until the end of the world.

Is this too crazy?

2
  • I like this question because it helps raise a though experiment for me: what situations does functional programming actually hinder software development? And I can see game development being an area where functional programming is difficult to adhere to strictly.
    – Ellesedil
    Jun 11, 2019 at 18:30
  • 1
    @Ellesedil: There's a Mario Bros. clone in Haskell that was written as part of a thesis, and I also remember seeing a blog series on Purely Functional Retrogames. I think there is a Space Invader clone in Idris (which was jokingly used to prove that Idris is Tetris-complete, since Tetris obviously reduces to Space Invaders). For example, you can model an event loop as co-recursion over co-data, or using Functional Reactive Programming. You can model global game state using the State Monad (or maybe figure out a design that doesn't need global game state). Jun 11, 2019 at 19:36

1 Answer 1

6

This sounds like a great usecase for the State monad. Here's something that could get you started:

trait BlackjackSteps {
  def gamerTakesTwoCards: State[Deck, Gamer]
  def dealerTakesTwoCards: State[Deck, Dealer]
  def isBlackjack(gamer: Gamer, dealer: Dealer): State[Deck, Option[Player]]
  def gamerToDrawCards(gamer: Gamer): State[Deck, Gamer]
  def dealerToDrawCards(dealer: Dealer, gamer: Gamer): State[Deck, Dealer]
  def determineWinner(gamer: Gamer, dealer: Dealer): State[Deck, Player]
}

def gamerTakesTwoCards: State[Deck, Gamer] = 
  State { oldDeck => 
    val gamer = f(oldDeck)
    val newDeck = g(oldDeck)
    (newDeck, gamer)
  } 

You can check out the official Cats documentation to find out more: https://typelevel.org/cats/datatypes/state.html

5
  • where do the functions f and g come form?
    – jakstack
    Jun 11, 2019 at 20:38
  • Well, you'll have to fill in the blanks there :) State just wraps a function from an old state to a new state along with a result. Jun 11, 2019 at 20:48
  • thanks, also the function signature returns : State[Deck, Gamer] but a tuple is returned (newDeck, gamer) from the implementation, is that correct?
    – jakstack
    Jun 11, 2019 at 20:52
  • Not quite, we do return a State[Deck, Gamer] , the State.apply constructor just uses a function from the oldDeck to a tuple of the new state and the result. :) Jun 11, 2019 at 20:54
  • I edited the code a bit, maybe it makes it more clear now? Jun 11, 2019 at 20:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.