2017-11-02 2 views
2
import itertools 

ranks = '23456789TJQKA' 

deck = [r+s for r in ranks for s in 'dchs'] 

allholdemhands = [i for i in itertools.combinations(deck,2)] 


def allcombos(hand,group): 
    'give all combos of a given hand for a given game' 
    return [i for i in group if (i[0][0] == hand[0] and i[1][0] == hand[1]) 
      or (i[0][0] == hand[1] and i[1][0] == hand[0])] 

def allbettercombos(cards,group): 
    'all combos as good as or better, of that type' 
    if cards[0] == cards[1]: 
     betterpairs = [i+i for i in ranks if ranks.index(i) >= 
         ranks.index(cards[0])] 
     return [allcombos(x,group) for x in betterpairs] 
    else: 
     bettercards = [cards[0]+i for i in ranks 
         if ranks.index(i) >= ranks.index(cards[1]) 
         and ranks.index(i) < ranks.index(cards[0])] 
     return [allcombos(x,group) for x in bettercards] 

def makerange(hands, group = allholdemhands): 
    'create a range of all hands from list of types' 
    handrange = [] 
    for hand in hands: 
     if hand[-1] != '+': 
      handrange.append(allcombos(hand,group)) 
     else: 
      handrange.append(allbettercombos(hand,group)) 
    return handrange 

위의 코드는 makerange 함수가 holdem 포커 핸드 또는 범위의 일부 집합을 입력하고 가능한 모든 고유 한 카드 조합의 목록을 출력하도록 허용하는 코드입니다.파이썬 - 포커 맞춤 범위 목록 출력 구조와 관련된 문제

작동하지만 출력이 목록 길이 1로 나오고 그 길이를 나타내는 카드의 조합 수와 같은 길이의 목록이되고 싶습니다.

  • 아래 코드는 이 길이 38의 목록이되도록 코드를 변경하는 방법을 알려줄 수 있습니까? 각 손을 요소로 사용 하시겠습니까?

PS : makerange 대한 입력의 구문에 짧은 프라이머

makerange ([ 'AA', 'KJ +']) 것 출력 :

[[('Ad', 'Ac'), ('Ad', 'Ah'), ('Ad', 'As'), ('Ac', 'Ah'), ('Ac', 'As'), ('Ah', 'As')], [[('Td', 'Kd'), ('Td', 'Kc'), ('Td', 'Kh'), ('Td', 'Ks'), ('Tc', 'Kd'), ('Tc', 'Kc'), ('Tc', 'Kh'), ('Tc', 'Ks'), ('Th', 'Kd'), ('Th', 'Kc'), ('Th', 'Kh'), ('Th', 'Ks'), ('Ts', 'Kd'), ('Ts', 'Kc'), ('Ts', 'Kh'), ('Ts', 'Ks')], [('Jd', 'Kd'), ('Jd', 'Kc'), ('Jd', 'Kh'), ('Jd', 'Ks'), ('Jc', 'Kd'), ('Jc', 'Kc'), ('Jc', 'Kh'), ('Jc', 'Ks'), ('Jh', 'Kd'), ('Jh', 'Kc'), ('Jh', 'Kh'), ('Jh', 'Ks'), ('Js', 'Kd'), ('Js', 'Kc'), ('Js', 'Kh'), ('Js', 'Ks')], [('Qd', 'Kd'), ('Qd', 'Kc'), ('Qd', 'Kh'), ('Qd', 'Ks'), ('Qc', 'Kd'), ('Qc', 'Kc'), ('Qc', 'Kh'), ('Qc', 'Ks'), ('Qh', 'Kd'), ('Qh', 'Kc'), ('Qh', 'Kh'), ('Qh', 'Ks'), ('Qs', 'Kd'), ('Qs', 'Kc'), ('Qs', 'Kh'), ('Qs', 'Ks')]]] 

이것은 모든 6 나타낸다 AA 카드를 만드는 방법과 높은 카드 K를 가지고있는 32 핸드 모두와 두 번째로 높은 J 카드를 만드는 방법.

+0

그럼 당신은 'makerange (['AA ','KJ + '])'와 같은 함수를 호출 할 것이라고 말하고 있습니까? –

+0

맞습니다. 정확히 맞습니다. – mariomendoza

+0

사소한 점이지만'ranks'의 값이'deck' 정의에 사용 되더라도'ranks = '23456789TJQKA'' * after *'deck'을 정의하는 것은 이상합니다. – Iguananaut

답변

0

가장 좋은 방법인지는 모르겠지만 질문에 대한 답변입니다. 과 같이 makerange

def allcombos(hand,group): 
    'give all combos of a given hand for a given game' 
    for i in group: 
     if (i[0][0] == hand[0] and i[1][0] == hand[1]) or (i[0][0] == hand[1] and i[1][0] == hand[0]): 
      yield i 

def allbettercombos(cards,group): 
    'all combos as good as or better, of that type' 
    if cards[0] == cards[1]: 
     betterpairs = [i+i for i in ranks if ranks.index(i) >= ranks.index(cards[0])] 
     for x in betterpairs: 
      for hand in allcombos(x,group): 
       yield hand 
    else: 
     bettercards = [cards[0]+i for i in ranks if ranks.index(i) >= ranks.index(cards[1]) and ranks.index(i) < ranks.index(cards[0])] 
     for x in bettercards: 
      for hand in allcombos(x,group): 
       yield hand 

와 적응 :

이 같은 generators으로 기능을 다시 작성

당신이 print makerange(['AA','KJ+'])를 호출 할 때

이제
def makerange(hands, group = allholdemhands): 
    'create a range of all hands from list of types' 
    handrange = [] 
    for hand in hands: 
     if hand[-1] != '+': 
      for hand in allcombos(hand,group): 
       handrange.append(hand) 
     else: 
      for hand in allbettercombos(hand,group): 
       handrange.append(hand) 
    return handrange 

는, 출력 (38)의 길이의 목록입니다 :

[('Ad', 'Ac'), ('Ad', 'Ah'), ('Ad', 'As'), ('Ac', 'Ah'), ('Ac', 'As'), ('Ah', 'As'), ('Jd', 'Kd'), ('Jd', 'Kc'), ('Jd', 'Kh'), ('Jd', 'Ks'), ('Jc', 'Kd'), ('Jc', 'Kc'), ('Jc', 'Kh'), ('Jc', 'Ks'), ('Jh', 'Kd'), ('Jh', 'Kc'), ('Jh', 'Kh'), ('Jh', 'Ks'), ('Js', 'Kd'), ('Js', 'Kc'), ('Js', 'Kh'), ('Js', 'Ks'), ('Qd', 'Kd'), ('Qd', 'Kc'), ('Qd', 'Kh'), ('Qd', 'Ks'), ('Qc', 'Kd'), ('Qc', 'Kc'), ('Qc', 'Kh'), ('Qc', 'Ks'), ('Qh', 'Kd'), ('Qh', 'Kc'), ('Qh', 'Kh'), ('Qh', 'Ks'), ('Qs', 'Kd'), ('Qs', 'Kc'), ('Qs', 'Kh'), ('Qs', 'Ks')] 

자세한 정보 here. 이제 코드가 리팩토링 될 수도 있지만 원하는대로 작동해야합니다.

+0

정말 고마워요! – mariomendoza

+0

@mariomendoza 환영합니다! 문제가 해결되면 대답으로 받아 들여주세요. [도움말 센터] (https://stackoverflow.com/help/someone-answers)에서 "귀하의 질문이나 답변에 '감사합니다'라고 말하지 마십시오. [...] '고맙습니다.'라고 투표하고 그 사람의 대답을 수락하거나 [...] 받아 드리고 싶습니다. " – Mrioddo