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?
import random
class Card(object):
def __init__(self):
self.value = ''
self.display = 'X'
class Deck(object):
def __init__(self):
self.cards = self.create_cards()
self.shuffle()
self.dealt = []
self.draw_piles = []
self.deal()
def create_cards(self):
card_list = []
values = []
for i in range(1, 11):
values.append(str(i))
values.append('J')
values.append('Q')
values.append('K')
for value in values:
card = Card()
card.value = value
card_list.append(card)
card_list = card_list * 8
return card_list
def shuffle(self):
random.shuffle(self.cards)
random.shuffle(self.cards)
def deal(self):
self.dealt.append(self.cards[0:10])
self.dealt.append(self.cards[10:20])
self.dealt.append(self.cards[20:30])
self.dealt.append(self.cards[30:40])
self.dealt.append(self.cards[40:50])
self.dealt.append(self.cards[50:54])
self.draw_piles.append(self.cards[54:64])
self.draw_piles.append(self.cards[64:74])
self.draw_piles.append(self.cards[74:84])
self.draw_piles.append(self.cards[84:94])
self.draw_piles.append(self.cards[94:105])
for index, card in enumerate(self.dealt[5]):
self.dealt[5][index].display = self.dealt[5][index].value
def display_cards(self):
for row in self.dealt:
for card in row:
print '%5s ' % card.display,
print ''
mydeck = Deck()
mydeck.display_cards()
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor