CPSC120
Fundamentals of Computer Science

Activity 30

Methods

Card Methods

Add the following methods to the Card class:

  • __repr__(self: Card) -> str - Return a string representation of a card. The parameter self is a Card object with the attributes rank and suit, both ints. This method is nearly identical to the card_to_str function you previously wrote. You just need to convert it to a method.

  • points(self: Card) -> int - Return the point value of a card. The parameter self is Card object with the attributes rank and suit, both ints. This method should determine a card's point value according to the following:

Rank Str Rank Int Point Value
A 1 11
2 2 2
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
J 11 10
Q 12 10
K 13 10

Test Cases

import test

class Card:
 # Put your code here

def main() -> None:
    a_card: Card

    a_card = Card(1, 2)
    test.equal(str(a_card), "A♥")
    test.equal(a_card.points(), 11)

    a_card = Card(2, 1)
    test.equal(str(a_card), "2♠")
    test.equal(a_card.points(), 2)

    a_card = Card(9, 3)
    test.equal(str(a_card), "9♦")
    test.equal(a_card.points(), 9)

    a_card = Card(10, 4)
    test.equal(str(a_card), "10♣")
    test.equal(a_card.points(), 10)

    a_card = Card(11, 1)
    test.equal(str(a_card), "J♠")
    test.equal(a_card.points(), 10)

    a_card = Card(12, 2)
    test.equal(str(a_card), "Q♥")
    test.equal(a_card.points(), 10)

    a_card = Card(13, 3)
    test.equal(str(a_card), "K♦")
    test.equal(a_card.points(), 11)

     # Put more test cases here
    return None

main()

Hint

  • Convert the card_to_str function to a method by 1. putting it indenting it underneath the Card class header and 2. changing all a_card variables to self.

  • The points method is similar to the __repr__ method. It should use an if-elif chain to set the value of the return variable. If self.rank is 1, it should set the return variable to 11. If self.rank is 13, 12, or 11, it should set the return variable to 10. If self.rank is 2 through 10, it should set the return variable to self.rank.

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 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. Deal two cards to the player's hand and print the player's hand.

  2. Repeatedly prompt the user if they want to hit. If the player does, deal a card to the player’s hand and print the player's hand.

  3. When the player is done hitting, print the total points of the player's hand.

Example

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

  • The player's hand should be represented as a list of Card objects. It should initially be empty. To deal a card to the player's hand, create a random card, and add it to the hand list.

  • To create a random Card, generate random integers for the rank (1 to 4) and suit (1 to 13) of a card using the random.randrange function and call the Card function.

  • Print the player's hand by printing the list of Card objects. Because the Card class has the function __repr__, it will automatically convert each of the Card objects to a string to print the list.

  • 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.

  • After the loop, print the point value of the player’s hand. Use a for loop to iterate over the Card objects in the hand list. Use the Card class's points method to accumulate each card’s point value.

Challenge 1

Because the cards are randomly generated, the player can be dealt the same card twice. Fix this by creating a deck, a list of Card objects with all 52 different cards. Shuffle the list at the beginning of the game and deal cards by removing a card from the end of the deck list and adding it to the player’s hand list.

Challenge 2

This solitary version of Blackjack is kind of boring. Add a dealer to the mix. The game is still very similar. In 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 Blackjack.

After the player hits until they are content, it is then the dealer's 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 dealer's hand, display the total points for both the player and the dealer, and print who won.