[Tutor] list of objects?

Walter Prins wprins at gmail.com
Tue Nov 15 15:22:08 CET 2011

Hi Elwin,

On 15 November 2011 13:40, Elwin Estle <chrysalis_reborn at yahoo.com> wrote:

> I am attempting to write a text based spider solitaire game.  I have  a
> pretty simple card class, and a deck class, which has a list of card
> objects, which are shuffled, then individual elements are put into
> self.dealt, which is a 'list of lists' when the cards are dealt.
> I am trying to control the visibility of the cards.  There is a small
> "for" loop in the "deal" method of the deck class, this is intended to
> toggle the visibility of four of the cards.  It does that just fine, but
> for some reason, it seems to be randomly toggling the visibility of other
> cards in the self.dealt list and I am thoroughly confused as to why that
> is.  Is it something to do with the way objects are referenced?  Is my list
> of card objects a bad way to approach this?

Off the top of my head, your problem is likely due to the line that reads:
card_list = card_list * 8

What this will do is put the *same* set of card objects in the list to
begin with, into a new card_list 8 times over.  So the *same* Jack (for
example) will appear 8 times in the resulting list.

This list, card_list, is the list that implements then later on sliced in
your deal method.  So, when you then set the cards in the dealt hand to
visible in your self.dealt[5] loop, because these same cards (card objects)
also are part of the other piles/hands, they also appear visible elsewhere
when the display method is called.

To fix: Replace the line above (e.g. card_list = card_list * 8)  with
something that explicitly constructs new cards 8 times, thereby not holding
mutliple refernces from seperate piles to the same card, then you should
have no further issues.  Maybe loop 8 times and make a function of te
preceding code etc.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111115/8a348e56/attachment.html>

More information about the Tutor mailing list