首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在21点游戏中数牌的问题

在21点游戏中数牌的问题
EN

Stack Overflow用户
提问于 2022-01-17 02:07:15
回答 1查看 57关注 0票数 -2

我玩了一场21点游戏,我在数牌时遇到了麻烦。有人能帮我清理一下密码吗?

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-17 02:15:49

就像这样:

代码语言:javascript
运行
复制
    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的纸牌游戏都是不值得玩的。既然你把卡片存储成字符串,为什么不直接存储熟悉的字母呢?它将对您的代码进行非常小的更改,并且用户体验会更好:

代码语言:javascript
运行
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70735797

复制
相关文章

相似问题

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