想不通为什么纸牌游戏会减去卡牌



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

它不再为我打扰,并相应地计算回合数。

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 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:

最新更新