python - passing multiple instances of the same class through objects -
class card: allranks=(2,3,4,5,6,7,8,9,10,11,12,13,14) allsuits=('spades','hearts','diamonds','clubs') def __init__(self, rank, suit): self.rank=rank self.suit=suit.capitalize() def getrank(self): if self.rank in card.allranks: return self.rank else: print 'please enter number 2 14\n14 => ace\n11 => jack\n12 => queen\n13 => king\n' exit() def getsuit(self): if self.suit in card.allsuits: return self.suit else: print 'there 4 suits in pack!' exit() def __str__(self): translate={11:'jack',12:'queen',13:'king',14:'ace'} r = self.rank if r in range(11,15): myrank=translate[r] elif r in range(2,11): myrank=str(r) else: print "sorry wrong card" exit() return myrank+' of '+self.suit def __lt__(self,other): return (self.rank > other.getrank()) #c=card(1,'spades') class deck: def __init__(self): self.deck=[card(i,j) in card.allranks j in card.allsuits] #for in card.allranks: # j in card.allsuits: # self.deck.append(card(i,j)) def shuffle(self): random import shuffle class dealer(object): def __init__(self, deck, cards, num_players): self.deck=deck self.num_players=num_players self.cards=cards def deal(self): self.deck.shuffle() deal_list=[[] in range(self.num_players)] #returns list of lists #say have 4 players have [[hand1],[hand2],[hand3],[hand4]] hand1=[5 cards] #now pass hand variable in range(self.cards): j in range(self.num_players): deal_list[j].append(self.deck.topcard()) return deal_list class dealer_better(object): def __init__(self, deck, cards, *args): self.deck=deck self.cards=cards def deal(self): self.deck.shuffle() deal_list=[[] in range(len(*args))] #returns list of lists #say have 4 players have [[hand1],[hand2],[hand3],[hand4]] hand1=[5 cards] #now pass hand variable in range(self.cards): j in (*args): j.append(self.deck.topcard()) class player(object): def __init__(self, hand=[]): self.hand=hand
hi have classes dealer , dealer_better. accepts objects deck() , has deal cards n number of players.
in class dealer pass number of players need deal cards directly variable , generate list of lists hand of each player list.
i make better in deal_better , pass player objects directly deal_better after initializing multiple instances of class player. there way pass *player_objects similar *args.
so can following functionality, p1=player() p2=player() p3=player() p4=player() new_d=dealer_better(deck(),p1.hand,p2.hand,p3.hand,p4.hand) new_d.deal()
print p1.hand should give me player one's hand, or atleast objects in it. write add_card method in player append self.hand if need be.
pasting classes in question below clarity.
class dealer(object): def __init__(self, deck, cards, num_players): self.deck=deck self.num_players=num_players self.cards=cards def deal(self): self.deck.shuffle() deal_list=[[] in range(self.num_players)] #returns list of lists #say have 4 players have [[hand1],[hand2],[hand3],[hand4]] hand1=[5 cards] #now pass hand variable in range(self.cards): j in range(self.num_players): deal_list[j].append(self.deck.topcard()) return deal_list
versus
class dealer_better(object): def __init__(self, deck,cards, *players): self.deck=deck self.cards=cards self.players = players def deal(self): self.deck.shuffle() in range(self.cards): p in range(len(self.players)): self.players[p].addcard(self.deck.topcard()) print self.players[p].name,len(self.players[p].hand) class player(object): def __init__(self, name, hand=[]): self.hand=hand self.name=name def addcard(self,card): self.hand.append(card)
returns
p1=player('bob') p2=player('lola') p3=player('luigi') p4=player('mario') new_d=dealer_better(deck(),5,p1,p2,p3,p4) new_d.deal() print len(p1.hand)
returns 20
how along lines of:
class player(object): def __init__(self): self.d = [] class dealer(object): def __init__(self, *decks): #feel free add more stuff example :) self.decks = [d d in decks] def deal(self): d in self.decks: d.append(1) # example d.append(2) p1 = player() p2 = player() p3 = player() d = dealer(p1.d,p2.d,p3.d) d.deal() print p1.d # changed deal
(demo)
Comments
Post a Comment