不久前,我用 Javascript 构建了一个简单的扑克游戏,并认为我会在 Python 中从头开始。这是到目前为止的代码(您可以跳到问题的最后):
#imports
import random
#basics
values = range(2,15)
suits = ['Clubs','Spades','Diamonds','Hearts']
#card object
class Card:
def __init__(self,suit,value,name):
self.suit = suit
self.value = value
self.name = name
if self.value < 11:
self.name = str(self.value) + ' of'
if self.value == 11:
self.name = 'Jack of'
if self.value == 12:
self.name = 'Queen of'
if self.value == 13:
self.name = 'King of'
if self.value == 14:
self.name = 'Ace of'
#deck
deck = []
#load and shuffle deck
for s in suits:
for v in values:
deck.append(Card(s,v,'o'))
random.shuffle(deck)
#load hands
your_hand = random.sample(deck,7)
for card in your_hand:
deck.remove(card)
#determine hands
def find_matches(hand):
class Match:
def __init__(self,value,amount):
self.value = value
self.amount = amount
matches = [Match(card.value,hand.count(card.value)) for card in hand]
for x in matches:
print x.value,x.amount
find_matches(your_hand)
是的,我意识到它并不完美(建议总是受到赞赏!)。我的问题是制作可靠的匹配查找功能。我尝试了几种不同的方法,但是hand.count(card.value)
对于每个元素,这种方法都显示为 0。计数方法将接受哪些参数的问题?还是与我的代码的一个方面有关?
谢谢你的帮助!