Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Card Counting

card_counting.livemd

Card Counting

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Fun FormulasHabit Tracker

Card Counting

We’re going to build some logic for a card counting application. Card counting is a strategy used in card games to determine if there are more high cards or low cards in the deck.

There are 13 numbered cards in a common deck of playing cards.

We’ll use integers 1 to 13 to represent playing cards. Cards are either low, normal, or high.

flowchart LR
subgraph low cards
  direction LR
  1 --- 2 --- 3 --- 4 --- 5 --- 6
end

subgraph Normal Cards
  direction LR
  7 --- 8 --- 9
end

subgraph High Cards
  direction LR
  10 --- 11 --- 12 --- 13
end

In order to track if there are more high cards left in the deck, or low cards left in the deck, you’ll track a running count starting at 0.

  • When dealt a low card decrease the count by 1.
  • When dealt a high card increase the count by 1.
  • When dealt a normal card do nothing.

Bind Card Counts

Bind integers low, normal, and high to track the amount to increase or decrease the current count by. You will use these variables in further exercises.

Example solution

low = -1
normal = 0
high = 1

Enter your solution below.

low = -1
normal = 0
high = 1

High Card

The initial count is 0. You have been dealt one high card. Use arithmetic operators and the bound variables above to determine the new count.

Example solution

initial_count = 0
initial_count + high

Enter your solution below.

count = 0
count + high

Low Card

The initial count is 0. You have been dealt one low card. Use arithmetic operators and the bound variables above to determine the new count.

Example solution

initial_count = 0
initial_count + low

Enter your solution below.

count + low

Multiple Cards

The initial count is 5. You have been dealt five high cards, two low cards, and two normal cards. Use arithmetic operators and the bound variables above to determine the new count.

Example solution

initial_count = 5
initial_count + high * 5 + low * 2 + normal * 2

You could omit the normal cards or allow high and low cards to balance each other, however if the increment/decrement value changes then your code would break.

Enter your solution below.

count = 5
count + 5*high + 2*low + 2*normal

Bonus: Determining Card Values

You are dealt three cards of a random value between 1 and 13. You can use Enum.random/1 to generate a random number.

random_card = Enum.random(1..13)

Generate three random cards, use the values of the three randomly generated cards to determine the current count given the initial count is 0.

Example solution

random_card1 = Enum.random(1..13)
random_card2 = Enum.random(1..13)
random_card3 = Enum.random(1..13)

cards =
  Enum.map([random_card1, random_card2, random_card3], fn random_card ->
    cond do
      random_card <= 6 -> low
      random_card >= 10 -> high
      true -> normal
    end
  end)

Enum.sum(cards)

You could omit the normal cards or allow high and low cards to balance each other, however if the increment/decrement value changes then your code would break.

Enter your solution below.

defmodule Test do
  
  def draw(x) when x > 0 do
    random_card = Enum.random(1..13)
    classify(random_card) + draw(x-1)
  end

  def draw(0), do: 0
  
  defp classify(card) do
    cond do
      card > 9 -> 1
      card < 7 -> -1
      true -> 0
    end
    
  end
    
end

Test.draw(10)
number_of_cards = 10
card_list =
  for _x <- (1..number_of_cards) do
    Enum.random(1..13)
  end

count_list = 
  Enum.map(card_list, fn card ->
    cond do
      card > 9 -> 1
      card <7 -> -1
      true -> 0
    end
  end)

Enum.sum(count_list)
count = 0

defmodule Cards do
  def count_cards(card) do
    high = 1
    low = -1
    normal = 0
    cond do
      card > 9 -> high
      card < 7 -> low
      true -> normal
    end
  end
end

card1 = Cards.count_cards(Enum.random(1..13))
card2 = Cards.count_cards(Enum.random(1..13))
card3 = Cards.count_cards(Enum.random(1..13))

count = card1 + card2 + card3

# for _x <- (1..3) do
#   Enum.random(1..13) |> Cards.count_cards()
# end



Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Card Counting exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Fun FormulasHabit Tracker