2015-10-15 38 views
0

我正在做一个扑克模拟,当我被困在如何使python检查数组的对,直道,三个种类等等。我已经制作了我的代码,因此每个卡片阵列都会为每张卡片生成第二个阵列的商店值。 例如如何检查一个阵列有卡的升序或两张相同的卡或三张相同的卡

a=[1,3,4,2,5,8,1] b=[2,2,4,7,10] c=[5,6,5,5,5] 

我将如何检查是否有至少5点连续的数字(直)如果B具有至少2个数字彼此相等(一对),并且如果C具有4

+0

请显示您到目前为止所尝试过的 –

回答

1

排序的手。这样可以轻松检查直线,因为您需要依次输入五个数字。

对于某种类型的N,请遍历列表并查看每件物品的数量。例如:

for pos in range(len(hand)): 
    card_count = hand.count(hand[pos]) 
    if card_count >= 2: 
     print "Hand has", card_count, hand[pos], "'s" 

我没有送人这里所有的细节 - 这将打印两次,每次对,3次,3-的一类等等。我认为你问大部分是你需要的基本列表方法。

2

这应该足以让你开始。

def check_hand(_hand_): 
    last_c = '' 
    straight = 0 
    # This is illustrative, you can use this to return 
    # the greatest hand one could have 
    for card_value, number_of_occurences in _hand_.iteritems(): 
     if number_of_occurences == 2: 
      print("We have a 2 of a kind") 
     elif number_of_occurences == 3: 
      print("We have a 3 of a kind") 
     elif number_of_occurences == 4: 
      print("We have a 4 of a kind") 

     if last_c == '': 
      last_c = card_value 
     else: 
      if card_value - last_c == 1: 
       straight += 1 
      last_c = card_value 

    if straight >= 4: 
     print("we have a straight") 

a = [1, 3, 4, 2, 5, 8, 1] 
b = [2, 2, 4, 7, 10] 
c = [5, 6, 5, 5, 5] 

# nifty way of assigning a dictionary with how many 
# occurrences of a number in a list, and sorting it 
check = { 
    'a': dict((i, a.count(i)) for i in sorted(a)), 
    'b': dict((i, b.count(i)) for i in sorted(b)), 
    'c': dict((i, c.count(i)) for i in sorted(c)), 
} 

for which, hand in check.iteritems(): 
    print "Hand: " + which 
    check_hand(hand) 
相关问题