2017-02-03 20 views
1

다음 카드가 무엇이든간에 손의 확률을 찾는 방법을 찾으려고합니다. 나는 다음 카드를 확인하고 확률을 얻는 방법을 모르며, 각 종류의 손마다 각각의 방법으로 모두 쓰려고 할 때 무엇을해야 할지를 알지도 못한다. 카드를 손에 들고 그 손을 잡을 확률을 찾아 내면 도움이 될 것입니다.파이썬에서 입력 파일이있는 포커 확률

텍스트 파일을 읽는 프로그램을 작성하십시오. 이름은 으로 명령 줄 매개 변수로 제공됩니다. 각 행은 현재 핸드 카드 에있는 4 장의 카드 목록을 제공합니다. 파일의 판독 후, 프로그램 화료 가 주어진다 손 우승 각 유형의 확률에서 인쇄한다

import sys 
#error message 
if len (sys.argv) == 1: 
    print "Error" 
    exit() 
file = sys.argv[1] 
#counts and arrays 
#count = 0 

f = open(file) 
f = f.read() 
hand = f.splitlines() 
arraynum = 0 
def deck(): 
    deck = [] 
    suit = ['H', 'S', 'D', 'C'] 
    number = ['A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K'] 
    for s in suit: 
     for n in number: 
      deck.append(n+s) 
    return deck 

def startHand(arraynum): 
    hand1 = str(hand[arraynum]).split(', ') 
    hand1.sort() 
    return hand1 

def checkHand(deck,hand1): 
    for card in hand1: 
     for Card in deck: 
      if Card == card: 
       deck.remove(card) 
    return deck 

def check1(deck, hand1): 
    count = 0 
    for Card in deck: 
     for i in hand1[0:-1]: 
      if i != Card: 
       count +=1 
    prob = count/48 
    print prob 
    print count 


t1 = deck() 
t2 = startHand(3) 
t3 = checkHand(t1,t2) 
t4 = check1(t2,t3)' 

입력 파일이다 QS, JS, KS, 10S SOM은이 여기

('Chance of Royal Flush: ', 0.020833333333333332) 
('Chance of Straight Flush: ', 0.020833333333333332) 
('Chance of Four of a Kind: ', 0.0) 
('Chance of Full House: ', 0.0) 
('Chance of Flush: ', 0.14583333333333334) 
('Chance of Straight: ', 0.125) 
('Chance of Three of a Kind: ', 0.0) 
('Chance of Two Pair: ', 0.0) 
('Chance of Pair: ', 0.25) 
('Chance of High Card: ', 0.4375) 
************************************* 

('Chance of Royal Flush: ', 0.0) 
('Chance of Straight Flush: ', 0.0) 
('Chance of Four of a Kind: ', 0.0) 
('Chance of Full House: ', 0.0) 
('Chance of Flush: ', 0.0) 
('Chance of Straight: ', 0.0) 
('Chance of Three of a Kind: ', 0.041666666666666664) 
('Chance of Two Pair: ', 0.125) 
('Chance of Pair: ', 0.8333333333333334) 
('Chance of High Card: ', 0.0) 
************************************* 

('Chance of Royal Flush: ', 0.0) 
('Chance of Straight Flush: ', 0.0) 
('Chance of Four of a Kind: ', 0.0) 
('Chance of Full House: ', 0.0) 
('Chance of Flush: ', 0.1875) 
('Chance of Straight: ', 0.0) 
('Chance of Three of a Kind: ', 0.0) 
('Chance of Two Pair: ', 0.0) 
('Chance of Pair: ', 0.25) 
('Chance of High Card: ', 0.5625) 
************************************* 
+3

에 오신 것을 환영합니다. 도움말 설명서의 게시 지침을 읽고 따르십시오. [주제] (http://stackoverflow.com/help/on-topic) 및 [묻는 방법] (http://stackoverflow.com/help/how-to-ask) 여기를 참조하십시오. StackOverflow는 코딩 또는 튜토리얼 서비스가 아닙니다. – Prune

답변

0

: 같이 KS는,도 3C는, 3S는 QC 6D, 10D, AD, 7D

출력은 같아야 시작해야하고 파이썬 3.5에서 구현됩니다. 그것은 당신이해야 할 일을 위해 프레임 워크 을 제공 할 것입니다. 갑판 으로 걱정할 필요가 없습니다. 주어진하지만 쉽게 처리를위한 일련의 숫자로 를 매핑되는대로 입력 카드는 텍스트 파일에서 읽습니다.

당신은 불완전로 나머지를 구현해야합니다. 도움이 되었으면 좋겠다. 행운을 빕니다. 에 StackOverflow에

# Probability calculation examples are given here: https://en.wikipedia.org/wiki/Poker_probability 

import sys 
from math import factorial 

if len (sys.argv) == 1: 
    print("Error") 
    exit() 
file = sys.argv[1] 

cnv={'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8, 
    '9':9,'10':10,'J':11,'Q':12,'K':13,'A':14} 

f = open(file) 
input = f.read() 
inCards = [x.strip() for x in input.split(',')] 
cards=[{card[-1:] : cnv[card[0:-1]]} for card in inCards] 
print("All cards from input list=",cards,"\n\n") 

# Removes num cards from input list and returns them 
def getCards(cards,num): 
    hand=cards[0:num] 
    del cards[0:num] 
    return hand 

# Finds the suit with the most cards 
def maxSuit(suitSeq): 
    mv=0 
    for k,vList in suitSeq.items(): 
     if len(vList) > mv: 
     mv=len(vList) 
     mk=k 
    return mk,mv 

def seqGapsSize(suitList): 
    cumGapSizes=0 
    for i in range(1,len(suitList)): 
     cumGapSizes+=suitList[i]-suitList[i-1]-1 
    return cumGapSizes 

# Return a dict with just non-empty suits 
def nonEmptySuits(suitSeq): 
    suits={} 
    for k,vList in suitSeq.items(): 
     if len(vList) > 0: 
     suits[k]=vList 
    return suits 

# Returns a dictionary with keys of card values (ie 2,3..J,K) 
# across all suits and the # of times they occur in the hand. 
def sameCardDict(suitSeq): 
    d={} 
    for k,vList in suitSeq.items(): 
     for v in vList: 
     if v in d: 
      d[v]+=1 
     else: 
      d[v]=1 
    return d 

def getAllGaps(suits): 
    gaps=0 
    for k,vList in suits.items(): 
     gaps+=seqGapsSize(vList) 
    return gaps 

def royalFlushProb(suitSeq,numCards): 
    k,vnum=maxSuit(suitSeq) 
    if vnum!=numCards or seqGapsSize(suitSeq[k])>(5-numCards): return 0.0 
    if not set(suitSeq[k]) < set([10,11,12,13,14]): return 0.0 
    return 1.0/nChoosek(52-numCards,5-numCards) 

def straightFlushProb(suitSeq,numCards): 
    k,vnum=maxSuit(suitSeq) 
    if vnum!=numCards or seqGapsSize(suitSeq[k])>(5-numCards): 
     return 0.0 
    return 1.0/nChoosek(52-numCards,5-numCards) 

def fourOfAKindProb(suitSeq,numCards): 
    dict=sameCardDict(suitSeq) 
    maxSameNum=max(dict,key=dict.get) 
    if dict[maxSameNum]<numCards-1: return 0.0 
    return 1.0/(nChoosek(13,1)+nChoosek(12,1)) 

def fullHouseProb(suitSeq,numCards): 
    suits=nonEmptySuits(suitSeq) 
    # Need at least 2 suits for 4 cards 
    if len(suits.keys())<(5-numCards)+1: return 0.0 
    # Same card in all suits means there should be no gaps 
    if (getAllGaps(suits)!=0): return 0.0 
    return 1.0/(nChoosek(13,1)*nChoosek(2,1)+nChoosek(12,1)*nChoosek(3,1)) 

def findSeq(hand): 
    suitSeq={'C':[],'H':[],'D':[],'S':[]} 
    for c in hand: 
     for k,v in c.items(): 
      suitSeq[k].append(v) 
     suitSeq[k].sort()  
    return suitSeq 

def nChoosek(n,k): 
    return factorial(n)/(factorial(k)*factorial(n-k)) 

def computeAndPrintProbs(suitSeq,numCards): 
    print('Chance of Royal Flush: ', royalFlushProb(suitSeq,numCards)) 
    print('Chance of Straight Flush: ', straightFlushProb(suitSeq,numCards)) 
    print('Chance of Four of a Kind: ', fourOfAKindProb(suitSeq,numCards)) 
    print('Chance of Full House: ', fullHouseProb(suitSeq,numCards)) 
    print('Chance of Flush: ',) 
    print('Chance of Straight: ',) 
    print('Chance of Three of a Kind: ',) 
    print('Chance of Two Pair: ',) 
    print('Chance of Pair: ',) 
    print('Chance of High Card: ',) 
    print('*'*30,"\n"*2) 

numCards=4 
for i in range(1,4): 
    hand=getCards(cards,numCards) 
    print("Input hand on this round: ",hand) 
    suitSeq=findSeq(hand) 
    print("Hand re-organized for processing: ",suitSeq,"\n") 
    computeAndPrintProbs(suitSeq,numCards)