首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >不知道为什么纸牌游戏会使牌变差

不知道为什么纸牌游戏会使牌变差
EN

Stack Overflow用户
提问于 2018-10-06 00:58:42
回答 2查看 57关注 0票数 1

我正在制作一个名为“乞讨我的邻居”的纸牌游戏,在游戏中创建一副扑克牌,然后随机排序,然后平均分成2名玩家。然后,每个玩家抽签并把它放在桌子上,直到有一张惩罚牌(面牌)。每张牌面牌的债务值为1-4,而另一个牌手必须在牌桌上打这个数目的牌。然而,另一个玩家可以自己抽一张罚单,重新开始还债。如果一个玩家玩一个债务,而另一个玩家玩所有的牌而不是任何的债务,那么那些玩债务的玩家就会把所有的牌都拿走。胜利者是拥有所有牌牌的球员。

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

编辑:

代码语言:javascript
运行
复制
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

原始代码:

代码语言:javascript
运行
复制
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))
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-10-06 02:57:02

它停止了对我的窃听,并相应地对子弹进行计数。

代码语言:javascript
运行
复制
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:

代码语言:javascript
运行
复制
nextcard = G['stacks'][G['next']][0][0]

我实现了您提到的其他条件(将债务设置为0)。

票数 0
EN

Stack Overflow用户

发布于 2018-10-06 01:12:03

我认为问题就在这个代码中:

代码语言:javascript
运行
复制
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

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

你不应该打开卡片,你应该检查它,就像你以后做的那样:

代码语言:javascript
运行
复制
if G['stacks'][G['next']][0][0])== 1:
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52674825

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档