【发布时间】: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