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 a number from 2 to 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 are only 4 suits in a 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) for i in Card.allRanks for j in Card.allSuits]
#for i in Card.allRanks:
# for j in Card.allSuits:
# self.deck.append(Card(i,j))
def shuffle(self):
from 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=[[] for i in range(self.num_players)] #returns a list of lists
#say you have 4 players you will have [[hand1],[hand2],[hand3],[hand4]] where hand1=[5 cards]
#now you pass this to the hand variable
for i in range(self.cards):
for 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=[[] for i in range(len(*args))] #returns a list of lists
#say you have 4 players you will have [[hand1],[hand2],[hand3],[hand4]] where hand1=[5 cards]
#now you pass this to the hand variable
for i in range(self.cards):
for j in (*args):
j.append(self.deck.topCard())
class Player(object):
def __init__(self, hand=[]):
self.hand=hand
您好我有经销商和Dealer_better。它接受来自Deck()的对象,并且必须将卡牌交给n个玩家。
在课程经销商中,我将需要处理卡片的玩家数量直接作为变量传递,并生成列表列表,其中每个玩家的手牌都是列表。
我想在Deal_better中做得更好,并在初始化 Class Player 的多个实例后将玩家对象直接传递给Deal_better。有没有办法传递* player_objects类似于* args。
这样我就可以获得以下功能,
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应该给玩家一个人的手,或至少给它的对象。
我可以在播放器中编写一个add_card方法,以便在需要时附加到self.hand。
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=[[] for i in range(self.num_players)] #returns a list of lists
#say you have 4 players you will have [[hand1],[hand2],[hand3],[hand4]] where hand1=[5 cards]
#now you pass this to the hand variable
for i in range(self.cards):
for 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, *players):
self.deck=deck
self.cards=cards
self.players = players
def deal(self):
self.deck.shuffle()
for i in range(self.cards):
for 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)
返回
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)
返回20
答案 0 :(得分:1)
如何处理以下内容:
class player(object):
def __init__(self):
self.d = []
class dealer(object):
def __init__(self, *decks): #feel free to add more stuff this is just an example :)
self.decks = [d for d in decks]
def deal(self):
for d in self.decks:
d.append(1) # just an example
d.append(2)
p1 = player()
p2 = player()
p3 = player()
d = dealer(p1.d,p2.d,p3.d)
d.deal()
print p1.d # was changed by deal
(demo)