以下是问题: 对于扑克游戏,我希望能够有效地比较多个变量。 这是代码的主体,必须从具有最高值的一个(ace = 1,king = 13)开始输入五张卡:
print("*********")
print("P O K E R")
print("*********")
print("Type in your cards starting from the highest value: ")
print()
#values going from 1 (ace) to 13 (king) and suits: 1 (spades), 2 (hearts), 3 (diamonds) and 4 (clubs)
value1 = int(input("1. Value: "))
suit1 = int(input("1. Suit: "))
value2 = int(input("2. Value: "))
suit2 = int(input("2. Suit: "))
value3 = int(input("3. Value: "))
suit3 = int(input("3. Suit: "))
value4 = int(input("4. Value: "))
suit4 = int(input("4. Suit: "))
value5 = int(input("5. Value: "))
suit5 = int(input("5. Suit: "))
我正在使用if
语句评估扑克牌并确定他们的ranking。对我来说,这似乎是最简单的方法。评估的一个例子是:
#Evaluating the hand and comparing the cards
c = [value1, value2, value3, value4, value5]
#four of a kind
if c[0] == c[1] == c[2] == c[3] != c[4] or c[0] == c[1] == c[2] == c[4] != c[3] or c[0] == c[1] == c[3] == c[4] != c[2] or c[0] == c[2] == c[3] == c[4] != c[1] or c[1] == c[2] == c[3] == c[4] != c[0]:
print("You have Four Of A Kind!")
#three of a kind
#two pairs
#one pair
#highest card
此代码检查5种组合中的1种是否为真。这是低效且耗时的。而且由于我想进一步编码“三种”,“两对”和“一对”的选项,组合变得更大。有没有办法缩短if
声明?
答案 0 :(得分:1)
您可能需要考虑构建所有扑克组合的完整列表,而不是创建大量if/elif
分支,并使用它来将绘制的手与此预建列表进行比较。 5张扑克手牌组合的数量约为260万。这包括130万个高卡组合,您可以从预先建立的列表中排除这些组合,并在没有其他任何匹配时使用“后退”检查。
答案 1 :(得分:1)
为什么不使用OOP?我认为随着您的进展,您可以更轻松地更改项目。我已经在这里做了一些事情 - 没有经过充分测试,但它应该给你一个想法:
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)
注意:这是python2.7,如果你需要,我会尝试将它转换为python3
答案 2 :(得分:0)
我的方法是首先按尺寸(4,7,9,12,13)订购卡片,然后编写checkPairs(),checkFlush()等函数并将其应用到它。
订购汽车会对您有所帮助,因为您可以检查一下,如果您有两张相同价值的卡片......它们将是连续的
function checkPairs(){
card = hand[0];
pairs=1;
for (i =1; i<5; i++){
if(card == hand[1]){
pairs++;
}else{
pairs=1;
}
return pairs;
}
或冲洗
card = hand[0];
return_value ="flush";
if(card = 10)
return_value ="royal_flush";
for (i =1; i<5; i++){
if((hand[1] - card) !=1){
return false;
}
return return_value;