我正在做一個撲克模擬,當我被困在如何使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
我正在做一個撲克模擬,當我被困在如何使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
排序的手。這樣可以輕鬆檢查直線,因爲您需要依次輸入五個數字。
對於某種類型的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-的一類等等。我認爲你問大部分是你需要的基本列表方法。
這應該足以讓你開始。
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)
請顯示您到目前爲止所嘗試過的 –