[Tutor] Show commands

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun Dec 29 02:52:02 2002


On Sat, 28 Dec 2002, Adam Vardy wrote:

> >> A lot of programs work in this way, bundling up a bunch of compound
> >> actions as a single name.  When we say deck.deal(), we're actually
> >> doing quite a few things:
>
> >> ###
> >> ###  See Section 16.3 of thinkCSpy
> >> ###
> >> def deal(self, hands, nCards=999):
> >>     nHands = len(hands)
> >>     for i in range(nCards):
> >>       if self.isEmpty(): break    # break if out of cards
> >>       card = self.popCard()       # take the top card
> >>       hand = hands[i % nHands]    # whose turn is next?
>
> This line I'm not sure about.
>
> I am not sure what is [hand] or what is happening to it.


Ah!  Ok, let's concentrate on that one line from before:

    deck.deal([hand], 5)

There's actually a few things happening on this line.  This might make
more sense if we break it down into three distinct chunks:

    all_hands = [hand]
    number_of_cards = 5
    deck.deal(all_hands, number_of_cards)

This sequence of three statements should have a similar effect.



The deck.deal() function takes in a list of hands, and also takes in how
many cards each hand should be dealt.  Imagine a group of people around a
poker table --- each person has a "hand".

    petes_hand = Hand()
    wendys_hand = Hand()
    geralds_hand = Hand()
    belindas_hand = Hand()

and to deal 7 cards from a deck, the dealing function needs to know which
hands to start passing cards into.  That's why the deck.deal() hand takes
in a list of hands:

    deck.deal([petes_hand,
               wendys_hand,
               geralds_hand,
               belindas_hand],
              7)

The thing that might look weird is that we're just building this list of
hands, right on the spot, simply so that deck.deal() knows about all of
the hands it needs to touch.  Even if it seems wasteful to build a list
just to collect hands together, it is still convenient: our deck.deal()
function can handle any number of people.  Even one person, which is how
we can read:

    deck.deal([hand], 5)

as

    "The deck deals 5 cards to the following hands: [hand,]"

A list that has one element is still a list.  It's a silly thing to say,
but it's actually not as obvious as it sounds: a lot of people often treat
a list of one-element as a "special case".  It's a hard habit to break.


We could technically hardcode things so that deck.deal() deals strictly
between two people, but that excludes group-oriented games like Blackjack
or Go-fish.  deck.deal() tries to capture that feeling of flipping the
cards toward each hand.  The deal()ing doesn't just dole the cards out:
it actively fans them out to each person --- er, hand.


By the way, in the above example, it turns out that since we're only
passing seven cards to a group of four hands, someone's going to have
fewer cards.  Try it out and see what happens.  Can you see whose hand is
left with fewer cards?


Anyway, hope that clears things up a little more.  *grin*  Good luck to
you.