Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Card is meant to represent a single card. The main function is to store the rank and value of the card and compare them using

Card is meant to represent a single card. The main function is to store the rank and value of the card and compare them using the following symbols

  • " < "
  • " == "
  • " > "
  • " >= "
  • " <= "
  • " != "

There are multiple ways to compare the cards, we will use this order:

  • Suits: Clubs (lowest), followed by Diamonds, Hearts, and Spades (highest)
  • Ranks: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King (from smallest to highest).

We will not mark off magic numbers for storing values in a list/dictionary (Hint: use a list/dictionary to assign values).

For example: King of Clubs is smaller (<) than 2 of Spades. First you compare Suits, and, if the same, you compare Ranks.

class Card(): ''' This class represents a single card Also compares two cards >>> c1 = card(4, "Clubs") >>> print(c1) 4 of Clubs >>> c2 = card(3, "Hearts") >>> print(c2) 3 of Hearts >>> c1 < c2 True >>> c1 > c2 False >>> c1 == c2 False ''' 

The tests above show only three comparison operators: "<", ">" and "==". There should be appropriate methods for all of them.

Finished creating Card class, move on to a Deck class.

Deck will contain

  • __init__ - a constructor that will initialize a new Deck object. This will contain
  • An instance variable called deck, which will be a deck of cards as a list (initially full).
  • Deck should be initialized in sorted order by suit and card value. The suit order is Clubs, Diamonds, Hearts, and then Spades. Aces are low and can be treated as the number 1.
  • An instance variable called dealt_cards, which will be a list of cards that have been dealt out
  • shuffle() - a method that will be used to add dealt_cards back into deck and then shuffle the deck
  • This should use the numpy function random choice exactly once. Documentation here. Think about the proper parameters here: what is a? what is the size and what is the value for replace?
  • Note: in order to grade your assignment, we should make the random generator "predictable", so you all will get the same answer. In order to do that the special method, seed(), is used. Here is a link for more detailed explanation: seeding. This will make it so that each time you call random, you will get the same output.
  • Note 2: Make sure to convert the numpy array back into a list after shuffling it with choice.
  • deal_cards() - a method that will take in n and will deal out n cards from the top of the deck.
  • The top n cards should be removed from deck and added to dealt_cards
  • You will return the list of cards that were dealt.

REFER TO STARTER CODE DOCTESTS FOR OUTPUTS. Be sure to match everything.

Constructor's doctests:

>>> cards = deck() >>> len(cards.deck) 52 >>> len(cards.dealt_cards) 0 >>> print(cards) # PLEASE REFER TO DOCTESTS  

Make sure to match output.

A few doctests for example:

 >>> cards = deck() >>> np.random.seed(5) >>> cards.shuffle() >>> print(cards.deck[:5]) [9 of Hearts, 4 of Hearts, 7 of Spades, 7 of Diamonds, 6 of Hearts] >>> cards = deck() >>> hand = cards.deal_cards(5) >>> np.random.seed(5) >>> cards.shuffle() >>> cards.deck[:5] [A of Spades, 9 of Hearts, Q of Spades, Q of Diamonds, J of Hearts] 

Make a simplified version of Blackjack with the following rules.

  1. There will be a player and a dealer. Both will be dealt cards from the same deck.
  2. In each round, the player and the dealer will be dealt two cards.
  3. Neither players can go over 21, the one with the higher value wins.
  4. If the values for both are equal then compare the second cards using card comparison as done in the previous homework.
  5. Unlike the real version of Blackjack where all face cards are 10, you will be using ranking values for cards like (J is 11, Q is 12, etc.)
  6. Also unlike the real version of Blackjack, Ace will only be treated as 1.

Blackjack class should have the following methods.

  • Constructor for the class, which will
  • have a deck and shuffle it exactly once
  • keep track of the scores of the player and the dealer
  • Function play_rounds that will take in an n number of rounds
  • n will default to 1 if there is no input
  • You should check if there are enough cards in the deck to play the number of rounds
  • If not, PRINT "NOT ENOUGH CARDS TO PLAY"
  • It will play n rounds of the game and PRINT what each card each player is dealt and the winner of the round:
  • Player's cards: 9 of Hearts and 4 of Hearts
  • Dealer's cards: 7 of Spades and 7 of Diamonds
  • Dealer won the round
  • The player and dealer's value is determined by the sum of the rank of their cards
  • If the dealer or the player goes above 21, then print
  • Dealer/Player Bust!
  • If the dealer and player have the same value,
  • compare the player's second card with the dealer's second card
  • The one with the higher card wins the round.
  • Otherwise check, who won the round and print
  • Player/Dealer won the round
  • If a player won the round with a score of 21, then print
  • Blackjack! or Dealer Blackjack!
  • After n rounds of the game, it should print out the score: player to dealer
  • The score is now 0 to 1
  • Function declare_winner which will print out the score and the string depending on who is winning the game
  • Player Wins!
  • Dealer Wins!
  • It's a Tie!

FOLLOW THE DOCTESTS CAREFULLY AND CLEARLY, MAKE SURE THAT ALL OUTPUTS MATCH DOCTESTS.

Sample Run of the Game

 """ >>> np.random.seed(5) >>> game = BlackJack() >>> game.play_rounds(2) Player's cards: 9 of Hearts and 4 of Hearts Dealer's cards: 7 of Spades and 7 of Diamonds Dealer won the round Player's cards: 6 of Hearts and 9 of Diamonds Dealer's cards: 7 of Clubs and 8 of Hearts Dealer won the round The score is now 0 to 2 >>> game.declare_winner() The score is 0 to 2 Dealer Wins! >>> np.random.seed(8) >>> game2 = BlackJack() >>> game2.play_rounds(2) Player's cards: Q of Spades and 10 of Hearts Dealer's cards: 2 of Clubs and K of Spades Player Bust! Dealer won the round Player's cards: A of Clubs and 10 of Diamonds Dealer's cards: K of Diamonds and 6 of Hearts Dealer won the round The score is now 0 to 2 >>> np.random.seed(15) >>> game3 = BlackJack() >>> game3.play_rounds(2) Player's cards: 8 of Hearts and 4 of Diamonds Dealer's cards: 9 of Hearts and J of Spades Dealer won the round Player's cards: Q of Hearts and 9 of Spades Dealer's cards: A of Spades and K of Hearts Blackjack! Player won the round The score is now 1 to 1 >>> game3.declare_winner() The score is 1 to 1 It's a Tie! """ 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions