【发布时间】:2013-09-22 14:57:05
【问题描述】:
不久前,我用 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。计数方法将接受哪些参数的问题?还是与我的代码的某个方面有关?
感谢您的帮助!
【问题讨论】: