【问题标题】:Can not figure out why card game subtracts cards wonky无法弄清楚为什么纸牌游戏会减去纸牌
【发布时间】:2021-10-31 11:20:06
【问题描述】:

我正在制作一款名为 beggar my neighbor 的纸牌游戏,其中创建了一副纸牌,然后随机排序并平均分配给 2 名玩家。然后,每个玩家抽牌并将其放在桌子上,直到打出罚牌(面牌)。每张面牌的债务价值为 1-4,其他玩家必须在牌桌上打出该数量的牌。但是,其他玩家可以自己抽一张罚牌,重新开始偿还债务。如果一个玩家打出欠债,而另一个玩家打出所有牌而不是任何欠债,则打出欠债的玩家将拿走桌子上的所有牌。获胜者是拥有所有牌组的玩家。

我的问题是,当游戏自行运行时(控制台中的 play()),堆栈(每个玩家拥有的牌数)不会减少 1,而是减少一些任意数量。我该如何解决这个问题?

编辑:

if(G['debt']>0):
            # Paying a debt.
            print("Turn {}: Player {} is paying a debt.".format(turn, current(G)))
            # May want to show debt cards being played with displayCard().
            # Careful to handle any penalty cards paid as part of a debt!
            if len(G['stacks'][G['next']]) > G['debt']:
                #if 
                for i in range(G['debt']):
                    # Print what card is being played
                    print("Turn {}: Player {} Played {}".format(turn, current(G), (displayCard(G['stacks'][G['next']][0]))))
                    # Test if card being played is a penalty card
                    if G['stacks'][G['next']][0][0]  == 1:
                        #G['stacks'][G['next']].pop(0)
                        G['debt']=4
                        i=0
                    elif G['stacks'][G['next']][0][0] == 13:
                        #G['stacks'][G['next']].pop(0)
                        G['debt']=3
                        i=0
                    elif G['stacks'][G['next']][0][0]  == 12:
                        #G['stacks'][G['next']].pop(0)
                        G['debt']=2
                        i=0
                    elif G['stacks'][G['next']][0][0]  == 11:
                        #G['stacks'][G['next']].pop(0)
                        G['debt']=1
                        i=0
                # Add the card to the table
                G['table'].append(G['stacks'][G['next']][0])
                # Remove the card from the player's stack
                G['stacks'][G['next']].pop()
            else:
                G['debt'] = 0

原代码:

from random import randint

def createDeck(N=13, S=('spades', 'hearts', 'clubs', 'diamonds')):
    return([(v, s) for v in range(1,N+1) for s in S])

def displayCard(c):
    suits = {'spades':'\u2660', 'hearts':'\u2661', 'diamonds':'\u2662', 'clubs':'\u2663'}
    return(''.join( [ str(c[0]), suits[c[1]] ] ))

def simpleShuffle(D):
    for i in range(len(D)):
        r=randint(i,len(D)-1)
        D[i],D[r]=D[r],D[i]
    return(D)

def newGame(N=13, S=('spades', 'hearts', 'clubs', 'diamonds')):
    d = simpleShuffle(createDeck(N,S))
    return {'table':[], 'next':0, 'debt':0, 'stacks':[d[:len(d)//2],d[len(d)//2:]]}

def describeGame(G):
    return('Player:'+str(G['next'])+' Stacks:['+str(len(G['stacks'][0]))+', '+str(len(G['stacks'][1]))+'] Table:'+str(len(G['table']))+' Debt:'+str(G['debt']))

def current(G):
    return(G['next'])

def opponent(G):
    if G['next']==0:
        return(1)
    else:
        return(0)

def advancePlayer(G):
    G['next']=opponent(G)
    return(G)

def play(G=newGame()):
    turn = 0

    while(G['stacks'][0]!=0 and G['stacks'][1]!=0): 
        # Show the state of play.
        print("Turn {}: {}".format(turn, describeGame(G)))

        # Make a move. First, check to see if a debt is due. If so,
        # pay it.
        if(G['debt']>0):
            # Paying a debt.
            print("Turn {}: Player {} is paying a debt.".format(turn, current(G)))

            if len(G['stacks'][G['next']]) >= G['debt']:
                for i in range(G['debt']):
                    # Print what card is being played
                    print("Turn {}: Player {} Played {}".format(turn, current(G), (displayCard(G['stacks'][G['next']][0]))))
                    # Test if card being played is a penalty card
                    if G['stacks'][G['next']].pop(0) == 1:
                        G['debt']=4
                        i=0
                    elif G['stacks'][G['next']].pop(0) == 13:
                        G['debt']=3
                        i=0
                    elif G['stacks'][G['next']].pop(0) == 12:
                        G['debt']=2
                        i=0
                    elif G['stacks'][G['next']].pop(0) == 11:
                        G['debt']=1
                        i=0
                # Add the card to the table
                G['table'].append(G['stacks'][G['next']][0])
                # Remove the card from the player's stack
                G['stacks'][G['next']].pop(0)
                # Increment turn
                turn = turn + 1

        else:
            print("Turn {}: Player {} Played {}".format(turn, current(G), (displayCard(G['stacks'][G['next']][0]))))
            #print(displayCard(G['stacks'][G['next']][0]))

            # Check if c is a penalty card.
            if(G['stacks'][G['next']][0][0]==1 or G['stacks'][G['next']][0][0]==11 or G['stacks'][G['next']][0][0]==12 or G['stacks'][G['next']][0][0]==13):
                  # Set up a new debt for the other player and advance
                  # immediately to next turn.
                  if (G['stacks'][G['next']][0][0])== 1:
                      G['debt']=4 
                  elif (G['stacks'][G['next']][0][0])== 13:
                      G['debt']=3 
                  elif (G['stacks'][G['next']][0][0])== 12:
                      G['debt']=2 
                  else:
                      G['debt']=1 

            # Not a penalty card; add it to the table.
            G['table'].append(G['stacks'][G['next']][0])
            # Remove the card 
            G['stacks'][G['next']].pop(0)

        # Advance to next player.
        advancePlayer(G)
        # Increment turn counter.
        turn = turn + 1

    # Exit loop: indicate winner.`enter code here`
    print("Player {} wins in {} turns.".format(opponent(G), turn))

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    它停止了对我的窃听,并且相应地计算了回合数。

    def play(G=newGame()):
    turn = 0
    
    while(len(G['stacks'][0])!=0 and len(G['stacks'][1])!=0):
        # Show the state of play.
        print("Turn {}: {}".format(turn, describeGame(G)))
    
        # Make a move. First, check to see if a debt is due. If so,
        # pay it.
        if(G['debt']>0):
            # Paying a debt.
            print("Turn {}: Player {} is paying a debt.".format(turn, current(G)))
            if len(G['stacks'][G['next']]) >= G['debt']:
                for i in range(G['debt']):
                    # Print what card is being played
                    print("Turn {}: Player {} Played {} for their debt.".format(turn, current(G), (displayCard(G['stacks'][G['next']][0]))))
                    nextcard = G['stacks'][G['next']][0][0]
                    # Test if card being played is a penalty card
                    if nextcard == 1:
                        G['debt']= 4
                    elif nextcard == 13:
                        G['debt']= 3
                    elif nextcard == 12:
                        G['debt']= 2
                    elif nextcard == 11:
                        G['debt']= 1
                    #G['stacks'][G['next']].pop(0)
                    # Add the card to the table
                    G['table'].append(G['stacks'][G['next']][0])
                    # Remove the card from the player's stack
                    G['stacks'][G['next']].pop(0)
                    # Increment turn
                    turn = turn + 1
                # in each iteration the turn is increased
                # however, outside of this loop the turn is increased once again
                # take this into account and remove one turn
                turn -= 1
            else:
                # player has less cards than they have to pay
                # plays all his cards
                print("Turn {}: Player {} has not enough cards to pay their debt.".format(turn, current(G)))
                G['debt'] = 0
                continue
        else:
            print("Turn {}: Player {} Played {}".format(turn, current(G), (displayCard(G['stacks'][G['next']][0]))))
            #print(displayCard(G['stacks'][G['next']][0]))
    
            # Check if c is a penalty card.
            if(G['stacks'][G['next']][0][0]==1 or G['stacks'][G['next']][0][0]==11 or G['stacks'][G['next']][0][0]==12 or G['stacks'][G['next']][0][0]==13):
                  # Set up a new debt for the other player and advance
                  # immediately to next turn.
                  if (G['stacks'][G['next']][0][0])== 1:
                      G['debt']=4
                  elif (G['stacks'][G['next']][0][0])== 13:
                      G['debt']=3
                  elif (G['stacks'][G['next']][0][0])== 12:
                      G['debt']=2
                  else:
                      G['debt']=1
    
            # Not a penalty card; add it to the table.
            G['table'].append(G['stacks'][G['next']][0])
            # Remove the card
            G['stacks'][G['next']].pop(0)
    
        # Advance to next player.
        advancePlayer(G)
        # Increment turn counter.
        turn = turn + 1
    # Exit loop: indicate winner.`enter code here`
    print("Player {} wins in {} turns.".format(opponent(G), turn))
    

    我做的第一件事是将 while 条件更改为 len() 而不仅仅是堆栈。 我还介绍了一个变量,nextcard:

    nextcard = G['stacks'][G['next']][0][0]
    

    我实现了你提到的 else 条件(将债务设置为 0)。

    【讨论】:

    • 非常感谢您的帮助,我能够在 if 语句之前添加 if len(G['stacks'][G['next']]) == 0: break To将卡片作为特殊例外添加到表中,以防止不时出现的索引不足。
    【解决方案2】:

    我认为问题出在这段代码中:

    if G['stacks'][G['next']].pop(0) == 1:
        G['debt']=4
        i=0
    elif G['stacks'][G['next']].pop(0) == 13:
        G['debt']=3
        i=0
    elif G['stacks'][G['next']].pop(0) == 12:
        G['debt']=2
        i=0
    elif G['stacks'][G['next']].pop(0) == 11:
        G['debt']=1
        i=0
    

    当一张卡片被弹出时,它会从堆栈中移除。每次执行“检查”时都会这样做。因此,任意数字来自检查返回 true 时。如果是例如。 13,它会移除 2 张卡片,对于值 12,它将移除 3 张卡片,等等。

    你应该检查它,而不是弹出卡片,就像你以后做的那样:

    if G['stacks'][G['next']][0][0])== 1:
    

    【讨论】:

    • 当我这样做时,堆栈确实减少了 1。但是,同一张牌被播放 x 数量(债务数量)次。
    • 我编辑了我的答案:检查结果为真后,您必须弹出卡片!抱歉,我没有想到这一点。
    • 首先,当我运行程序时它并没有结束,其次,它只从玩家堆栈中移除 1 张牌,并没有从牌堆中移除最上面的 4 张牌,非常感谢你的时间!
    • 嘿,我正在玩一些代码,我发现了一个你没有描述的案例(如果发生这种情况,游戏不会停止的可能原因):如果玩家少了怎么办卡比他必须支付的债务: if len(G['stacks'][G['next']]) >= G['debt']: ....
    • 我添加了一个 else 以在卡片
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-04
    • 2022-06-10
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多