如何在纸牌游戏中交替使用计算机回合和玩家回合?

问题描述

这是我使用 Pygame 的第一个项目,我已经从其他人的代码中汲取了很多灵感,同时尝试将其调整到我自己的目的。

游戏规则:

我正在编写一款玩家对计算机的纸牌游戏。它与 Solitaire 相似,因为有 4 个游戏堆可以从 Ace-Queen 中取出一系列卡片(King 是通配符,可以是 7 以外的任何东西)。 每一轮从玩家或电脑抽牌开始,使手牌为 5。 玩家和电脑有一副 26 张牌(游戏使用 2 副标准牌组),目标是打出 4 个游戏堆之一的顶牌,并在另一个玩家之前完成这副牌。 要结束一个回合,必须将手上的一张牌扔到四个骨堆之一上,然后下一个玩家的回合开始。

我的问题:

电脑成功抽手制作5张牌,然后随机选择一张牌扔到骨堆上。问题是计算机将继续抽牌并将它们打到骨堆上,直到主牌堆为空,此时会引发错误(IndexError:从空列表中弹出)。 根据我的理解,问题是计算机的 draw_hand() 函数在主循环运行时继续循环。 我的问题是如何实现一个代码来识别卡片何时被扔到骨堆上,然后切换到下一个玩家。

我的代码

在甲板类:

def draw_card(self):
    return self.cards.pop()

在计算机类中:

    def draw_hand(self,deck):
        while len(self.hand) < self.max_hand:
            self.hand.append(deck.draw_card())
        return self

在主文件中:

def computer_turn():
    computer.turn = True
    player.turn = False
    time.sleep(1)

def computer_end_turn():
    pile_choice = randint(1,5)
    card_choice = randint(1,(len(computer.hand) - 1))
    throw_card = computer.hand.pop(card_choice)
    if pile_choice == 1:
        comp_bp1.cards.append(throw_card)
    elif pile_choice == 2:
        comp_bp2.cards.append(throw_card)
    elif pile_choice == 3:
        comp_bp3.cards.append(throw_card)
    elif pile_choice == 4:
        comp_bp4.cards.append(throw_card)

def player_turn():
    global app_running
    global moving
    computer.turn = False
    player.turn = True
    main_deck_rect = Rect(main_deck.rect)
    for event in pygame.event.get():
        if event.type == QUIT:
            app_running = False
        if event.type == MOUSEBUTTONDOWN:
            if main_deck_rect.collidepoint(event.pos):
                player.draw_hand(game_deck)
    pygame.display.update()

主循环:

while app_running:

    first_player()
    if computer.first_turn:
        if drawing == 0:
            computer.draw_hand(game_deck)
            computer_turn()
        else:
            computer_end_turn()
        drawing += 1
        drawing = drawing % 2

    else:
        player_turn()

我尝试为计算机在抽牌和玩牌时实现不同的状态,但这仍然不起作用。我有一种感觉,问题出在 draw_hand() 方法上,即使在从手牌中打出一张牌后,该方法仍会继续运行,我不确定如何阻止这种情况发生。 任何帮助将不胜感激。

编辑: 这是我编写的用于确定哪个玩家先走的代码

def first_player():
    if comp_top_card.name == 'king' or player_top_card.name == 'king':
        if comp_top_card.name == 'king':
            computer.first_turn = True
            player.first_turn = False
            # print('computer goes first')
        elif player_top_card.name == 'king':
            computer.first_turn = False
            player.first_turn = True
            # print('player goes first')
        elif comp_top_card.name and player_top_card.name == 'king':
            comp_choice = randint(0,52)
            play_choice = randint(0,52)
            if comp_choice > play_choice:
                computer.first_turn = True
                player.first_turn = False
                # print('computer goes first')
            else:
                computer.first_turn = False
                player.first_turn = True
                # print('player goes first')
    else:
        if comp_top_card.value > player_top_card.value:
            computer.first_turn = True
            player.first_turn = False
            # print('computer goes first')
        elif comp_top_card.value < player_top_card.value:
            computer.first_turn = False
            player.first_turn = True
            # print('player goes first')
        else:
            comp_choice = randint(0,52)
            if comp_choice > play_choice:
                computer.first_turn = True
                player.first_turn = False
                # print('computer goes first')
            else:
                computer.first_turn = False
                player.first_turn = True
                # print('player goes first')

(我敢肯定这段代码很乱,而且有一种更有效的做事方式……但这对我有用哈哈哈哈)

解决方法

我尝试了对代码的各种修改,通过将 first_player() 函数移出主循环,我让它按照我想要的方式工作,因为该函数只能在游戏开始时调用一次。 所以现在我的代码看起来像这样:

first_player()

while app_running:
    if computer.turn:
        if drawing == 0:
            computer.draw_hand(game_deck)
        else:
            computer_turn()
            check_for_turn_end()
        drawing += 1
        drawing = drawing % 2

    else:
        player_turn()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...