Skip to content

Instantly share code, notes, and snippets.

@scottfrazer
Created August 11, 2022 18:50
Show Gist options
  • Save scottfrazer/48fb3868f077ee40eba6d94839a58460 to your computer and use it in GitHub Desktop.
Save scottfrazer/48fb3868f077ee40eba6d94839a58460 to your computer and use it in GitHub Desktop.

Using the Deck Of Cards API (https://deckofcardsapi.com/), write a program that does the following:

  1. Create a new shuffled deck
  2. Draw cards one at a time from the deck
  3. When a card is drawn, print it out in any format that includes number and suit (e.g. "JACK of SPADES" or "7H")
  4. If any three consecutive cards add up to a value of 21 using blackjack rules (see below), exit the program
  5. If all cards are drawn from the deck and no three consecutive cards add up to a value of 21, print "no more cards"

The "blackjack value" of a card can be defined as follows:

  • For numbered cards 2-10 (inclusive): the value is the number on the card
  • For JACK, QUEEN, KING: the value is 10
  • For ACE: the value is 1 or 11

Part 2:

How would you refactor the code to detect any sequence of cards adding to 21? For example, if I drew the following cards in order: ACE, ACE, ACE, ACE, 2, 2, 2, 2, 9, then the program would exit because all of the aces would be interpreted as having a value of 1 and thus 1+1+1+1+2+2+2+2+9=21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment