带输入fi的Python扑克牌概率

2024-10-01 11:22:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道怎样才能找到一手牌的概率,不管下一张牌是什么。我不知道如何检查下一张牌并得到概率,也不知道如何将它们组合成每种手牌的不同方法。如果你能帮我读懂手中的卡片,并找到拿到那只手的概率,我将不胜感激。在

Write a program that reads in a text file. The name will be supplied as a command-line parameter. Each line gives you a list of 4 cards in your current hand. After reading in the file, your program will print out the probability of each type of winning hand, where a winning hand is given

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、10秒 KS、3C、3S、QC 6D,10D,AD,7D

输出应如下所示:

^{pr2}$

Tags: ofinforifdefcountsyscard
1条回答
网友
1楼 · 发布于 2024-10-01 11:22:56

下面是一些应该让您开始并在Python3.5上实现的东西。它将提供框架 因为这是你需要做的。请注意,您不需要为 甲板。输入卡按给定的方式从文本文件中读取,然后进行映射 为便于处理,将其转换为一系列数字。在

你必须实现其余的,因为它是不完整的。希望这能帮上忙 祝你好运。在

# 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)

相关问题 更多 >