DEV Community

Discussion on: Daily Challenge #198 - 21 Blackjack

Collapse
 
aminnairi profile image
Amin • Edited

Elm

From what I understood about the game, the cards are revealed at each draw. I may have implemented a wrong version though.

module BlackJack exposing (score)

import List exposing (foldl)
import String exposing (toInt)
import Maybe exposing (withDefault)


cardToScore : String -> Int -> Int
cardToScore card accumulatedScore =
  case card of
    "A" ->
      if accumulatedScore + 11 > 21 then
        accumulatedScore + 1

      else
        accumulatedScore + 11

    otherCard ->
        accumulatedScore + ( otherCard |> toInt |> withDefault 10 )


score : List String -> Int
score cards =
  foldl cardToScore 0 cards

Tests

module BlackJackTest exposing (suite)

import Expect exposing (Expectation, equal)
import Test exposing (Test, describe, test)
import BlackJack exposing (score)


suite : Test
suite =
  describe "BlackJack.elm"
    [ describe "score"
      [ test "It should return 11" <| \_ ->
          equal 11 <| score [ "A" ]
      , test "It should return 25" <| \_ ->
          equal 25 <| score [ "5", "4", "3", "2", "A", "K" ]
      , test "It should return 21" <| \_ ->
          equal 21 <| score [ "A", "J" ]
      , test "It should return 22" <| \_ ->
          equal 22 <| score [ "A", "10", "A" ]
      , test "It should return 15" <| \_ ->
          equal 15 <| score [ "5", "3", "7" ]
      ]
    ]