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 aCard
object with the attributes rank and suit, both ints. This method is nearly identical to thecard_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 isCard
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
= Card(1, 2)
a_card str(a_card), "A♥")
test.equal(11)
test.equal(a_card.points(),
= Card(2, 1)
a_card str(a_card), "2♠")
test.equal(2)
test.equal(a_card.points(),
= Card(9, 3)
a_card str(a_card), "9♦")
test.equal(9)
test.equal(a_card.points(),
= Card(10, 4)
a_card str(a_card), "10♣")
test.equal(10)
test.equal(a_card.points(),
= Card(11, 1)
a_card str(a_card), "J♠")
test.equal(10)
test.equal(a_card.points(),
= Card(12, 2)
a_card str(a_card), "Q♥")
test.equal(10)
test.equal(a_card.points(),
= Card(13, 3)
a_card str(a_card), "K♦")
test.equal(11)
test.equal(a_card.points(),
# 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 theCard
class header and 2. changing alla_card
variables toself
.The
points
method is similar to the__repr__
method. It should use anif-elif
chain to set the value of the return variable. Ifself.rank
is 1, it should set the return variable to 11. Ifself.rank
is 13, 12, or 11, it should set the return variable to 10. Ifself.rank
is 2 through 10, it should set the return variable toself.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:
Deal two cards to the player's hand and print the player's hand.
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.
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 theCard
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 theCard
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 theCard
objects in the hand list. Use theCard
class'spoints
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.