不确定为什么我会收到 IndexError-poker 游戏代码

发布于 2025-01-14 11:46:44 字数 2498 浏览 1 评论 0原文

我对 python 很陌生,我正在尝试创建一个扑克游戏框架,它正在工作,但我尝试添加一个函数来让你知道其他玩家有更好的手牌,当它运行时它会抛出索引错误,我不知道是什么原因造成的。以下代码块是出现错误的函数。我将在下面的 github 存储库中链接完整的代码,以便您可以运行它,名为 Three_kind_check()、two_pair_check()、pair_check() 和 high_card_check() 的部分是出现错误的地方。抱歉,这非常混乱且不清楚。任何帮助将不胜感激

https://github.com /Doug160516/Poker-Game/blob/main/limit%20hold%20em.ipynb

这是有问题的函数

def hand_prob(player):

"""
Gives a probability value to another player having a better hand and improving your hand on subsequent streets

args:
    player:A Player object type representing the player or an ai
Returns:
    Current_win_prob:An integer representing the probability of the player hand being better than the opponents hand
Raises:
    AttributeError:If player is not a Player object type
"""

test_hole_cards=[]
test_community_cards=[]
for cards in player.hole_cards: #append players hole cards to a seperate list to prevent them being accidentally altered
    test_hole_cards.append(cards)
    
for cards in player.card_hand:
    for tests in test_hole_cards:
        if cards.rank==tests.rank and cards.suit==tests.suit: #appending non hole cards to player.card_hand
            pass
        else:
            test_community_cards.append(cards)
    

test_deck=Deck() #test deck to check all card combos
test_deck2=Deck() #second test deck because .remove() is giving error
test_deck2.all_cards.clear()
for cards in test_deck.all_cards: #removing player hole cards and community cards from deck so they cant be repeated
    for tests in player.card_hand:
        if cards.rank==tests.rank and cards.suit==tests.suit:
            pass
        else:
            test_deck2.all_cards.append(cards)
current_combo_num=0
current_better_combos=0
for combos in combinations(test_deck2.all_cards,2): #checking all possible card combos of opponent
    current_combo_num+=1
    card_com_a,card_com_b=combos
    alt_test_player=Player("alt",1)
    for cards in test_community_cards:
        alt_test_player.deal(cards)
    alt_test_player.deal(card_com_a)
    alt_test_player.deal(card_com_b)
    hand_value_check(player)
    hand_value_check(alt_test_player)
    if alt_test_player.hand_score>player.hand_score:
        current_better_combos+=1
        print("better")
    else:
        print("worse")
current_win_prob=((current_better_combos/current_combo_num)*100)



return current_win_prob

I'm quite new to python, i'm trying to create a poker game framework and it was working but I've tried to add a function to give you the likelihood the other player has a better hand and when it runs it throwing up an index error and I have no idea what's causing it. The following code block is the function where the error is coming up. I'll link the full code in a github repository below so you can run it, the sections called three_kind_check(),two_pair_check(),pair_check() and high_card_check() have been where the error has cropped up. Sorry this is quite cluttered and unclear. Any help would be appreciated

https://github.com/Doug160516/Poker-Game/blob/main/limit%20hold%20em.ipynb

This is the offending function

def hand_prob(player):

"""
Gives a probability value to another player having a better hand and improving your hand on subsequent streets

args:
    player:A Player object type representing the player or an ai
Returns:
    Current_win_prob:An integer representing the probability of the player hand being better than the opponents hand
Raises:
    AttributeError:If player is not a Player object type
"""

test_hole_cards=[]
test_community_cards=[]
for cards in player.hole_cards: #append players hole cards to a seperate list to prevent them being accidentally altered
    test_hole_cards.append(cards)
    
for cards in player.card_hand:
    for tests in test_hole_cards:
        if cards.rank==tests.rank and cards.suit==tests.suit: #appending non hole cards to player.card_hand
            pass
        else:
            test_community_cards.append(cards)
    

test_deck=Deck() #test deck to check all card combos
test_deck2=Deck() #second test deck because .remove() is giving error
test_deck2.all_cards.clear()
for cards in test_deck.all_cards: #removing player hole cards and community cards from deck so they cant be repeated
    for tests in player.card_hand:
        if cards.rank==tests.rank and cards.suit==tests.suit:
            pass
        else:
            test_deck2.all_cards.append(cards)
current_combo_num=0
current_better_combos=0
for combos in combinations(test_deck2.all_cards,2): #checking all possible card combos of opponent
    current_combo_num+=1
    card_com_a,card_com_b=combos
    alt_test_player=Player("alt",1)
    for cards in test_community_cards:
        alt_test_player.deal(cards)
    alt_test_player.deal(card_com_a)
    alt_test_player.deal(card_com_b)
    hand_value_check(player)
    hand_value_check(alt_test_player)
    if alt_test_player.hand_score>player.hand_score:
        current_better_combos+=1
        print("better")
    else:
        print("worse")
current_win_prob=((current_better_combos/current_combo_num)*100)



return current_win_prob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文