CPSC120A
Fundamentals of Computer Science

Lab 22

Mutability

Blackjack

If you don't know the card game blackjack, it's simple. Each player is dealt two cards. Face cards are worth 10 points, aces are worth 11 points, and all other cards are worth face value. You can hit (ask for another card), or you can stand (keep the cards you currently have). You can continue this process as long as you want. Once all players have stood, the player with the most points without going over 21 is the winner.

Details

Create a Python program that plays a single hand of blackjack. The program should do the following:

  1. Create deck and shuffle a deck of cards. The deck is a list of strings where each string represents a card. For example 'A♠', '4♣', 'K♥', and '10♦' are strings that represent the cards Ace of Spades, Four of Clubs, King of Hearts, and Ten of Diamonds.
  2. Deal two cards from the deck to the player's hand, which is also a list of strings, and print the player's hand.
  3. Repeatedly prompt the user if they want to hit. If the player does, deal a card from the deck to the players hand and print the player's hand.
  4. When the player is done hitting, print the total points of the player's hand. The cards 2 through 10 are worth their rank, jacks, kings, and queens are worth 10 point, and aces are worth 11 points.

Example

Blackjack
=========  
Your Hand: ['10♦', 'K♠']
Do you want to hit (y/n)? y
Your Hand: ['10♦', 'K♠', '2♥']
Do you want to hit (y/n)? n
You have 22 points.
    

Hint

  • Create the deck by initializing a list containing 52 strings representing all of the cards. Shuffle the using the deck by using the random module's shuffle function.
  • The player's hand should be represented as a list of strings, just like the deck. It should be initially empty. To deal a card to the player's hand, remove a card from the end of the deck by using the pop method and add the card to the end of the player's hand by using the append method.
  • Add a while loop that runs while the user has entered 'y'. Inside the loop, deal one more card to the user's hand, print the hand, and prompt the user to hit again.
  • Create a function hand_points(hand) that returns the sum of all of the cards in the the list hand. The function should iterate over each string in the list and use a string slice to copy the rank of each card string. It should convert each rank string into an integer and use an accumulator to compute the sum of all of the ranks. Test this function before using it in the play_blackjack_hand function.

Challenge

This solitary version of Blackjack is kind of boring. Add a dealer to the mix. The game is still very similar. At the beginning, deal cards to both the player and the dealer. Display one of the dealer's cards to the player, as is typical with a game of blackjack.

After the player hits until they are content, it is then the dealers turn. On the dealer's turn, repeatedly add cards to the dealer's hand until the total is 17 points or more.

After the dealer's turn, show the entirety of the dealers hand, display the total points for both the player and the dealer, and print who won.

Blackjack
=========
Dealer's Hand: 2♠ ##
~~~~~Your Turn~~~~~
Your Hand: ['10♦', 'K♠']
Do you want to hit (y/n)? y
Your Hand: ['10♦', 'K♠', '2♥']
Do you want to hit (y/n)? n
~~~Dealer's Turn~~~
Dealer hits.
Dealer's Hand: 2♠ 10♣ 8♥
Dealer stays.
You have 22 points.
Dealer has 20 points.
Dealer wins.