我玩了一场21点游戏,我在数牌时遇到了麻烦。有人能帮我清理一下密码吗?
class Player():
def __init__(self, name):
self.bust = False
self.hand = []
self.hand2 = []
self.name = str(name)
self.handlist = []
def hit(self):
for i in self.handlist:
response = ''
while response != 'h' or response != 's':
print(self.name)
print(i)
print("Do you want to hit or stand? (h/s)")
print(self.gettotal(i))
response = str(input())
if response == 'h':
phand = i
card = pickacard(deck)
phand.append(card)
i = phand
phand = []
grade = self.gettotal(i)
if grade > 21:
self.bust = True
print(i)
print(self.gettotal(i))
print("Player busts!")
break
else:
break
def gettotal(self, hand):
total = 0
if hand == []:
total = 0
return total
for i in range(len(hand)):
t = int(hand[i][0])
if t == 11 or t == 12 or t == 13:
total += 10
elif t != 1:
total += t
elif t == 1 and total < 22:
total += 11
if total > 21:
i = len(hand)-1
while i >= 0:
t = int(hand[i][0])
if t == 1:
total -= 10
break
i -= 1
t = 0
return total
我有这样的例子:
player1
['1','D','11','C','11','D','1','H']
你想打还是站着?(h/s)
共计= 21
player1
['1','D','10','D','11','H','1','H']
你想打还是站着?(h/s)
共计= 21
发布于 2022-01-17 02:15:49
就像这样:
def gettotal(self, hand):
total = 0
aces = False
for card in hand:
t = int(card[0])
if t > 11:
total += 10
elif t == 1:
aces = True
total += 1
else:
total += t
if aces and total <= 11:
total += 10
return total
顺便说一句,任何显示1,11,12和13而不是A,J,Q,K的纸牌游戏都是不值得玩的。既然你把卡片存储成字符串,为什么不直接存储熟悉的字母呢?它将对您的代码进行非常小的更改,并且用户体验会更好:
def gettotal(self, hand):
total = 0
aces = False
for card in hand:
t = card[0]
if t in "JQK":
total += 10
elif t == 'A':
aces = True
total += 1
else:
total += int(t)
if aces and total <= 11:
total += 10
return total
https://stackoverflow.com/questions/70735797
复制相似问题