Homework 4

advertisement

Homework 4

Due June 17, 2015

Submissions are due by 11:59PM on the specified due date. Submissions may be made on the Blackboard course site under the Assignments tab. Late submissions will be accepted up to two days late with a 10% penalty for each day.

Make sure your name and FSUID are in a comment at the top of the file.

In this assignment, you may only use the sys , strings , and random packages (although you do not necessarily need any of these).

1 deck of cards.py (100 points)

Your task is to create a Python module called deck of cards.py. The deck of cards module must define three classes: Card (which represents a single playing card), Hand (which represents a collection of Card objects in a hand), and Deck (which represents a deck of Card objects). Note that Hand is a subclass of Deck.

Required methods for the Card Class:

• init (): must accept as arguments the integer representation of suit and rank.

• str ()

• value(): returns the face value of the card (as it is in Blackjack).

The Card class should allow the user to specify the suit as an integer 0-3 where 0 is Clubs, 1 is Diamonds, 2 is Hearts, and 3 is Spades. Rank should be specified using integers 1-13 where

1 is an Ace, 2 is 2, ..., 11 is Jack, 12 is Queen, and 13 is King. The string representation of the class, returned by the str () method is shown below in the example output. For the value, we will be using Blackjack values (we’ll be using these classes later on!) so every card’s value is its face value – the King, Queen, and Jack all have a value of 10. Let Aces have the low value of 1.

Required methods for the Deck Class:

• init (): initializes a sorted standard 52 card deck.

• add card(): accepts a Card object as an argument and adds it to the deck.

• remove card(): accepts a Card object as an argument and removes it from the deck.

• pop card(): accepts an optional index as an argument. Removes and returns a card from the deck at the index specified. By default, removes and returns last card.

• shuffle(): shuffles the order of cards in the deck (hint: see random.shuffle).

• sort(): sort the deck of cards.

• move cards(): accepts a Hand and number as an argument. Deals the given number of cards into the Hand object.

1

Note that a “sorted” deck is sorted by both rank numerically and suit in alphabetical order.

That is, the first card in a sorted deck is the Ace of Clubs and the last card is the King of

Spades. You do not have to maintain order when adding or removing cards from a deck – you can simply add a card as the last card of the deck. The move cards() method must deal from the last card.

Required Methods for the Hand Class:

• init (): initializes empty hand.

• str ()

• size(): returns the size of the hand in number of cards.

Note that the Hand class is a subclass of the Deck class. The string representation of the hand class is shown in the sample output below.

>>> from deck_of_cards import *

>>> c = Card(1, 8)

>>> c

<card.Card instance at 0x7f40000ad710>

>>> print c

8 of Diamonds

>>> c.value()

8

>>> c2 = Card(3, 12)

>>> print c2

Queen of Spades

>>> c2.value()

10

>>> d = Deck()

>>> d.pop_card()

<card.Card instance at 0x7f40000b7680>

>>> c = d.pop_card()

>>> print c

Queen of Spades

>>> c = d.pop_card()

>>> print c

Jack of Spades

>>> c = d.pop_card()

>>> print c

10 of Spades

>>> d.shuffle()

>>> c = d.pop_card()

>>> print c

Queen of Clubs

>>> c = d.pop_card()

>>> print c

2

2 of Hearts

>>> d.add_card(c)

>>> d.remove_card(c)

>>> d.sort()

>>> c = d.pop_card()

>>> print c

9 of Spades

>>> c = d.pop_card()

>>> print c

8 of Spades

>>> h = Hand()

>>> print h

>>> d.move_cards(h, 3)

>>> print h

7 of Spades 6 of Spades 5 of Spades

>>> h.size()

3

3

Download