在扑克游戏中表示手和识别手的组合

2024-09-30 08:19:37 发布

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

我正在编写一个简单的扑克游戏。我的代码可以在下面找到。我想知道我怎么能编码以下组合:三个一类,直,满房子,和四个一类。我不使用套装,我只对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)

我没有收到任何错误消息,因为我还没有真正开始编写这些组合。你知道吗


Tags: orandyouifrandomprintdecisioncombo
2条回答

我写了一堆扑克代码,其中一些是为我教的编程课编写的。我学到的一点是,不要不尊重@mneedham的回答,最好将一张卡片表示为0到51之间的数字。这有很多的优势,就如何处理一个5卡手寻找特定的组合。你知道吗

如果用0到51的数字表示的两张牌在等级上匹配,那么它们用13表示的数字将是相同的。所以你可以这样做来评估手的匹配等级:

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

结果:

[[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

要识别冲水,请检查所有5个卡值除以13是否给出相同的数字。要识别直线,请创建一个用13表示的每张卡值的列表(创建一个列组列表),对其排序,然后检查生成的数字是否是连续的。如果这两个测试都通过了,你就有了一个平手。 我把这些支票的编码留给你。你知道吗

任何号码的西装都很容易买到:

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

获得命名等级也是如此:

def show_rank(card):
    rank_names = { 0: 'Ace', 10: 'Jack', 11: 'Queen', 12: 'King'}
    rank = card % 13
    print(rank_names[rank] if rank in rank_names else rank)

您可能会发现将卡定义为pythonclasses很有用。这样你就可以组织卡片的细节(套装、价值)并制作一张由五个卡片对象组成的“手”。然后您可以为每只手执行简单的if语句。你知道吗

相关问题 更多 >

    热门问题