无法弄清楚为什么纸牌游戏会减去纸牌

时间:2018-10-06 00:58:43

标签: python-3.x

我正在制作一个名为beggar的纸牌游戏,作为我的邻居,在那里创建一副纸牌,然后将其随机排序并平均分配给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))

2 个答案:

答案 0 :(得分:0)

我认为问题出在此代码内:

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 :(得分:0)

好吧,我对代码做了些摆弄,请告诉我这是否对您有帮助。 它不再为我烦扰,并且相应地计算了回合。

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