如何将牌传给另一个玩家等等。假设 P1 抽到 4 个梅花,我如何传给 P2
How to pass cards to another player and so on. Say P1 draws 4 of clubs, how do I pass to P2
我假设我想把它放在我开始的播放器 class 下,但无论我做了什么,我都不知道该怎么做。我更愿意被指出正确的方向,而不是只给我代码。
import random
class Card(object):
def __init__(self, suit, val):
self.suit = suit
self.value = val
def show(self):
print("{} of {}".format(self.value, self.suit))
class Deck(object):
def __init__(self):
self.cards = []
self.build()
#suits of the cards
def build(self):
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.cards.append(Card(s, v))
def show(self):
for c in self.cards:
c.show()
#shuffle the deck
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
r = random.randint(0, i)
self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
def draw(self):
return self.cards.pop()
class Player(object):
def __init__(self):
self.hand = []
#draw cards
def draw(self, deck):
self.hand.append(deck.draw())
return self
#show the cards in the players hand
def showHand(self):
for card in self.hand:
card.show()
#Pass card to next player
def passCard(self):
self.hand.pop()
deck = Deck()
deck.shuffle()
bob = Player()
bob.draw(deck).draw(deck).draw(deck).draw(deck)
bob.showHand()
我在播放器中有两种方法class:
# In Python, prefer snake_case over camelCase
def give_card(self, index):
return self.hand.pop(index)
def take_card(self, card):
self.hand.append(card)
然后任何时候你想把一张牌从一个玩家传给另一个玩家,你会做这样的事情:
player_2.take_card(player_1.give_card(0))
我假设我想把它放在我开始的播放器 class 下,但无论我做了什么,我都不知道该怎么做。我更愿意被指出正确的方向,而不是只给我代码。
import random
class Card(object):
def __init__(self, suit, val):
self.suit = suit
self.value = val
def show(self):
print("{} of {}".format(self.value, self.suit))
class Deck(object):
def __init__(self):
self.cards = []
self.build()
#suits of the cards
def build(self):
for s in ["Spades", "Clubs", "Diamonds", "Hearts"]:
for v in range(1, 14):
self.cards.append(Card(s, v))
def show(self):
for c in self.cards:
c.show()
#shuffle the deck
def shuffle(self):
for i in range(len(self.cards)-1, 0, -1):
r = random.randint(0, i)
self.cards[i], self.cards[r] = self.cards[r], self.cards[i]
def draw(self):
return self.cards.pop()
class Player(object):
def __init__(self):
self.hand = []
#draw cards
def draw(self, deck):
self.hand.append(deck.draw())
return self
#show the cards in the players hand
def showHand(self):
for card in self.hand:
card.show()
#Pass card to next player
def passCard(self):
self.hand.pop()
deck = Deck()
deck.shuffle()
bob = Player()
bob.draw(deck).draw(deck).draw(deck).draw(deck)
bob.showHand()
我在播放器中有两种方法class:
# In Python, prefer snake_case over camelCase
def give_card(self, index):
return self.hand.pop(index)
def take_card(self, card):
self.hand.append(card)
然后任何时候你想把一张牌从一个玩家传给另一个玩家,你会做这样的事情:
player_2.take_card(player_1.give_card(0))