在扑克游戏中表示手牌并识别手牌组合

时间:2019-06-06 15:46:35

标签: python combinations

我正在尝试编写一个简单的扑克游戏。我的代码可以在下面找到。我想知道如何编写以下组合:三种,笔直的,满屋子的,和四种。我没有使用西装,对Python也只有一个基本的了解。任何建议表示赞赏。谢谢。

我尝试了几种不同的组合,但是我还没有接近编码。

import random

print('The game ends if you fold')
print('11 = Jack')
print('12 = Queen')
print('13 = King')
print('14 = Ace')

#Choose a random card
cardOne = random.randint(1, 14)
cardTwo = random.randint(1, 14)

#Print the users hand
print('YOUR CARDS')
print(cardOne)
print(cardTwo)

oppCardOne = random.randint(1, 14)
oppCardTwo = random.randint(1, 14)

print("OPPONENT'S CARDS")
print(oppCardOne)
print(oppCardTwo)

def fold():
  print('Lol, ok u lose champ')
  exit()

swampOne = random.randint(1,14)
print('First Swamp Card:', swampOne)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

swampTwo = random.randint(1,14)
print('Second Swamp Card:', swampTwo)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

swampThree = random.randint(1,14)
print('Third Swamp Card:', swampThree)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()

fourthStreet = random.randint(1, 14)
print('fourth Street:', fourthStreet)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print("Damn, you've got some buzungas")

if decision == 2:
  fold()


river = random.randint(1, 14)
print('River:', river)
print('What would you like to do?')
print('1: Keep Playing')
print('2: Fold')

decision = int(input())

if decision == 1:
  print('Good Luck')

if decision == 2:
  fold()


#User combos



#Highest compile
if cardOne > oppCardOne or oppCardTwo:
  combo = 1
if cardTwo > oppCardOne or oppCardTwo:
  combo = 1
#Pair
if cardOne or cardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  combo = 2
#Two pairs
if cardOne and cardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  combo = 3
if cardOne == swampOne and swampTwo or swampOne and swampThree or swampOne and fourthStreet or swampOne and river:
  combo = 3
if cardOne == (swampTwo and swampOne) or (swampTwo and swampThree) or (swampTwo and fourthStreet) or (swampTwo and river):
  combo = 3
if cardOne == (swampThree and swampOne) or (swampThree and swampTwo) or (swampThree and fourthStreet) or (swampThree and river):
  combo = 3
if cardOne == (fourthStreet and swampOne) or (fourthStreet and swampTwo) or (fourthStreet and swampThree) or (fourthStreet and river):
  combo = 3
if cardOne == (river and swampOne) or (river and swampTwo) or (river and swampThree) or (river and fourthStreet):
  combo = 3
#Two pars card two
if cardTwo == swampOne and swampTwo or swampOne and swampThree or swampOne and fourthStreet or swampOne and river:
  combo = 3
if cardTwo == (swampTwo and swampOne) or (swampTwo and swampThree) or (swampTwo and fourthStreet) or (swampTwo and river):
  combo = 3
if cardTwo == (swampThree and swampOne) or (swampThree and swampTwo) or (swampThree and fourthStreet) or (swampThree and river):
  combo = 3
if cardTwo == (fourthStreet and swampOne) or (fourthStreet and swampTwo) or (fourthStreet and swampThree) or (fourthStreet and river):
  combo = 3
if cardTwo == (river and swampOne) or (river and swampTwo) or (river and swampThree) or (river and fourthStreet):
  combo = 3
#Hand pairs
if cardOne == cardTwo:
  combo = 3
#Three of a kind









#Opponent Combos
if oppCardOne > cardOne or cardTwo:
  oppCombo = 1
if oppCardTwo > cardOne or cardTwo:
  oppCombo = 1
if oppCardOne or oppCardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  oppCombo = 2
if oppCardOne and oppCardTwo == swampOne or swampTwo or swampThree or fourthStreet or river:
  oppCombo = 3






#Determine who wins
if combo > oppCombo:
  print('YOU WIN YA SCHMUCK')
  exit()
elif oppCombo > combo:
  print('HA, YOU LOSE')
  exit()
else:
  print('TIE')
  exit()




print(combo)

我还没有收到任何错误消息,因为我还没有真正开始编写这些组合的代码。

2 个答案:

答案 0 :(得分:0)

将卡定义为python classes可能会很有用。这样,您可以组织卡片的详细信息(花色,值),并用一只手组成五个卡片对象。然后,您可以为每手创建简单的if语句。

答案 1 :(得分:0)

我写了一堆扑克手的代码,其中一些是我教的编程课的代码。我了解到的一件事是,不要无视@mneedham的回答,最好将一张卡表示为介于0到51之间的数字。这在处理5张卡的手牌外观方面有很多优势对于特定组合。

如果用从0到51的数字表示的两张牌在等级上匹配,则用13修改的数字相同。因此,您可以执行以下操作来评估涉及排名匹配的手牌:

222222  1074556     2
222222  1001297     blank

结果:

def evaluate_hand(cards):
    # build a list of lists for collecting cards of the same rank
    ranks = []
    for i in range(13):
        ranks.append([])

    # put each card in the appropriate rank list
    for i in cards:
        ranks[i % 13].append(i)

    # Sort our rank list descending by sublist length
    ranks = sorted(ranks, key=len, reverse=True)

    # Show what we end up with
    print(ranks)

    # Now show what we've got
    if len(ranks[0]) == 4:
        print("Four of a kind")
    elif len(ranks[0]) == 3 and len(ranks[1]) == 2:
        print("Full house")
    elif len(ranks[0]) == 3:
        print("Three of a kind")
    elif len(ranks[0]) == 2 and len(ranks[1]) == 2:
        print("Two pair")
    elif len(ranks[0]) == 2:
        print("Pair")
    else:
        print("Nada")

evaluate_hand([31, 4, 23, 17, 30])
evaluate_hand([36, 4, 23, 17, 30])
evaluate_hand([36, 4, 23, 19, 30])
evaluate_hand([4, 5, 6, 7, 8])

要识别同花顺,请检查是否所有5张卡值除以13都给出相同的数字。要识别一个顺子,请为每个用13修改的卡值创建一个列表(创建等级列表),对其进行排序,然后检查所得的数字是否连续。如果这两个测试均通过,则表示同花顺。 我将这些支票的编码留给您。

轻松获取任意数量的西装:

[[4, 17, 30], [31], [23], [], [], [], [], [], [], [], [], [], []]
Three of a kind
[[4, 17, 30], [36, 23], [], [], [], [], [], [], [], [], [], [], []]
Full house
[[4, 30], [36, 23], [19], [], [], [], [], [], [], [], [], [], []]
Two pair
[[4], [5], [6], [7], [8], [], [], [], [], [], [], [], []]
Nada

获得命名排名也是这样:

def show_suit(card):
    print(['Diamond', 'Heart', 'Spade', 'Club'][int(card / 13)])