<p>你为什么不用?我认为随着你的发展,改变你的项目会容易得多。我在这里做了一些事情-<em>没有经过充分测试,但它应该会给你一个想法:</p>
<pre><code>import random
class Card(object):
card_values = {'ace' : 13, 'king' : 12, 'queen' : 11, 'jack' : 10,
'10' : 9, '9' : 8, '8' : 7, '7' : 6, '6' : 5,
'5' : 4, '4' : 3, '3' : 2, '2' : 1}
card_suits = {'spades' : u'♤', 'diamonds' : u'♦', 'clubs': u'♧', 'hearts' : u'♥'}
def __init__(self, suit, name):
self.name = name
self.value = self.card_values[name]
if suit in self.card_suits.keys():
self.suit = suit
def __lt__(self, other):
return self.value < other.value
def __gt__(self, other):
return self.value > other.value
def __eq__(self, other):
return (self.value == other.value) and (self.suit == other.suit)
def __str__(self):
return self.__repr__()
def __repr__(self):
return u"{" + unicode(self.name, "utf-8") + u" " + self.card_suits[self.suit] + u"}"
def get_value(self):
return self.value
def get_suit(self):
return self.suit
def get_name(self):
return self.name
def same_suit(self, other):
return self.suit == other.suit
class Hand(object):
def __init__(self, card_list):
assert len(card_list) == 5
self.cards = sorted(card_list)
def determine_strength(self):
score = self.high_card()
if self.royal_flush():
print "royal_flush"
score += 1000
return score
elif self.straight_flush():
print "straight_flush"
score += 900
return score
elif self.four_of_a_kind():
print "four_of_a_kind"
score += 800
return score
elif self.full_house():
print "full_house"
score += 700
return score
elif self.flush():
print "flush"
score += 600
return score
elif self.straight():
print "straight"
score += 500
return score
elif self.three_of_kind():
print "three_of_kind"
score += 400
return score
elif self.two_pair():
print "two_pair"
score += 300
return score
elif self.one_pair():
print "one_pair"
score += 200
return score
print "high card"
return score
def flush(self):
suit = self.cards[0].get_suit()
return all([card.get_suit() == suit for card in self.cards])
def straight(self):
start = self.cards[0].get_value()
for card in self.cards[1:]:
if card.get_value() == (start + 1):
start = card.get_value()
else:
return False
return True
def straight_flush(self):
return (self.straight() and self.flush())
def royal_flush(self):
return ((self.cards[0].get_name() == '10') and self.straight_flush())
def _pair(self):
count = 0
names = [card.get_name() for card in self.cards]
for i in range(len(names)):
if (names[0:i] + names[i+1:]).count(names[i]) == 1:
count += 1
return count
def one_pair(self):
return self._pair() == 2
def two_pair(self):
if self.three_of_kind():
return False
return self._pair() == 4
def _of_a_kind(self):
count = {}
names = [card.get_name() for card in self.cards]
for name in names:
if name not in count:
count[name] = 1
else:
count[name] += 1
return max(count.values())
def three_of_kind(self):
return self._of_a_kind() == 3
def four_of_a_kind(self):
return self._of_a_kind() == 4
def full_house(self):
return (self.one_pair() and self.three_of_kind())
def high_card(self):
return max([card.get_value() for card in self.cards])
def __str__(self):
return self.__repr__()
def __repr__(self):
return u'<>'.join([repr(card) for card in self.cards]).encode('utf-8')
def run_hand(h):
H = Hand(h)
print H
print H.determine_strength()
rand_hand = []
while len(rand_hand) < 5:
c = Card(random.choice(Card.card_suits.keys()), random.choice(Card.card_values.keys()))
if c not in rand_hand:
rand_hand.append(c)
rf = [Card('hearts', 'ace'), Card('hearts', 'king'), Card('hearts', 'queen'),
Card('hearts', 'jack'), Card('hearts', '10')]
sf = [Card('spades', '9'), Card('spades', '8'), Card('spades', '7'),
Card('spades', '6'), Card('spades', '5')]
foak = [Card('spades', 'ace'), Card('hearts', 'ace'), Card('clubs', 'ace'),
Card('diamonds', 'ace'), Card('spades', '5')]
fh = [Card('spades', 'ace'), Card('hearts', 'ace'), Card('clubs', 'ace'),
Card('diamonds', 'king'), Card('spades', 'king')]
f = [Card('spades', 'ace'), Card('spades', '10'), Card('spades', '2'),
Card('spades', '5'), Card('spades', '7')]
s = [Card('spades', '6'), Card('spades', '5'), Card('clubs', '4'),
Card('diamonds', '3'), Card('hearts', '2')]
toak = [Card('spades', 'ace'), Card('diamonds', 'ace'), Card('clubs', 'ace'),
Card('diamonds', 'king'), Card('spades', '10')]
tp = [Card('spades', 'ace'), Card('diamonds', 'ace'), Card('clubs', 'king'),
Card('diamonds', 'king'), Card('spades', '10')]
op = [Card('spades', 'ace'), Card('diamonds', 'ace'), Card('clubs', '9'),
Card('diamonds', 'king'), Card('spades', '10')]
hc = [Card('spades', 'ace'), Card('diamonds', '10'), Card('clubs', '9'),
Card('diamonds', 'king'), Card('spades', '5')]
run_hand(rand_hand)
print " -Royal Flush -"
run_hand(rf)
print " -Straight Flush -"
run_hand(sf)
print " -Four of a Kind -"
run_hand(foak)
print " -Full House -"
run_hand(fh)
print " -Flush -"
run_hand(f)
print " -Straight -"
run_hand(s)
print " -Three of a Kind -"
run_hand(toak)
print " -Two Pairs -"
run_hand(tp)
print " -One Pair -"
run_hand(op)
print " -High Card -"
run_hand(hc)
</code></pre>
<p>注:这是Python2.7,如果你需要的话,我会把它转换成Python3</p>