Skip to content

Instantly share code, notes, and snippets.

@audunstrand
Last active March 6, 2017 21:32
Show Gist options
  • Save audunstrand/86aaa7b8987096e7f672a266e11aafa9 to your computer and use it in GitHub Desktop.
Save audunstrand/86aaa7b8987096e7f672a266e11aafa9 to your computer and use it in GitHub Desktop.

Can you beat the dealer at 21?

Model the game

  • create a single deck of playing cards
  • two players (called Sam and the Dealer) who will play against each other
  • each player is given two cards from the top of a shuffled deck of cards

Rules to implement

  • determine score of a hand[1]
  • check if either player has blackjack (21) with their initial hand and wins the game
  • if neither player has blackjack then Sam can start drawing cards from the top of the deck
  • Sam should stop drawing cards from the deck if their total reaches 17 or higher
  • Sam has lost the game if their total is higher than 21
  • when Sam has stopped drawing cards the Dealer can start drawing cards from the top of the deck
  • the Dealer should stop drawing cards when their total is higher than Sam.
  • the Dealer has lost the game if their total is higher than 21
  • determine which player wins the game
  • Sam wins when both players starts with 21
  • Dealer wins when both players starts with 22

[1] Numbered cards are their point value. Jack, Queen and King count as 10 and Ace counts as 11.

Read Deck of Cards

The game should read a deck of cards from a http endpoint. This endpoint is available at nav.no/blackjack It should also be possble to play the game with a random deck of cards. This should be indicated with the commandline argument --randon

The input will be in the format (52 cards):

SA, H4, C7, SJ,…, S5, H9, D10

Suits:
C: Clubs
D: Diamonds
H: Hearts
S: Spades
Values:
2: 2
3: 3
….
10: 10
J: Jack
Q: Queen
K: King
A: Ace

Output

When the game is over, the game should print the name of the winner to standard out, as well as all cards for both players, using this format:

[sam|dealer]
sam: card1, card2,..., cardN
dealer: card1, card2,..., cardN

Testing

The solution should include tests.

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