CPSC120 Lab 12

Creating Classes

  1. Card

    Create a class called Card in a file called card.py that represents a playing card. The class does not need to represent joker cards. The class should contain the following methods:

    1. An __init__ method that takes two integers, one that represents the suit and one that represents the rank of a card.

    2. A __str__ method that returns a string that represents the card.

    3. A method called value that returns an int that represents the value of the card. The Ace has the value 11. The face cards, Jack, Queen, and King, have the value 10. All other cards have the value of their rank.

    Create a program that imports the card module to test the class and its methods.

  2. Deck

    Create a class called Deck in a file called deck.py that represents a deck of playing cards. The class should contain the following methods:

    1. An __init__ method that has no parameters and fills the deck with one of every card, in a random order. Each card should be an object of the Card class.

    2. A __str__ method that returns a string that represents the deck.

    3. A method called remove_top that removes and returns the card on the top of the deck.

    Create a program that imports the deck module to test the class and its methods.

  3. Hand

    Create a class called Hand in a file called hand.py that represents a hand of playing cards. The class should contain the following methods:

    1. An __init__ method that has no parameters and initializes a hand with no cards.

    2. A method called add_card that takes a Card object and adds it to the hand.

    3. A __str__ method that returns a string that represents the hand.

    4. A method called total_value that returns the total value of all of the cards in the hand.

    Create a program that imports the hand module to test the class and its methods.

  4. Black Jack

    Create a program in a file called black_jack.py that plays a single hand of black jack. The program should:

    1. create a deck of cards and two hands, one for the dealer and one for the player.

    2. deal two cards from the deck into the hand of the dealer and two cards into the hand of the player.

    3. repeatedly print the user’s hand and prompt the user to hit or stay. If the user enters “hit”, a card should be dealt from the deck to the players hand and the new hand printed. If the player enters anything but “hit”, the loop should stop.

    4. repeatedly print the dealer’s hand and deal a card from the deck to the dealer’s hand until the dealer’s hand has a total value of 17 or more.

    5. print who won. The winner is whoever gets closest to 21 without going over. If both the player and the dealer go over 21 or if they have the same total value then it is a tie.

Submission

Submit a zip file of your code on the course Inquire site that uses your last names as the file name.