简体   繁体   English

试图做一个二十一点游戏

[英]Trying to make a blackjack game kind of

Summarize the problem总结问题

I am trying to make a simple blackjack program that picks two numbers for the dealer 1-11 and two numbers for the player 1-11 also.我正在尝试制作一个简单的二十一点程序,该程序为庄家 1-11 选择两个数字,也为玩家 1-11 选择两个数字。 I want to make a loop that checks if the player is hitting or staying based on what they input, hit or stay.我想制作一个循环,根据玩家输入、击中或停留的内容来检查玩家是否击中或停留。 If they hit I want it to add a card to the player deck and give them the option to hit or stay again.如果他们击中,我希望它在玩家牌组中添加一张牌,并让他们选择再次击中或留下。

If they stay I want the dealer to check a set of rules and those rules are:如果他们留下来,我希望经销商检查一组规则,这些规则是:

1: does the dealer have less than 17 and does it have less than the player 1a: if it does have less than 17 and less than the player it will draw a new number 1-11 and add it to the dealer hand 1b: it will then run through the loop of checking if it has the following conditions 1:庄家的点数是否小于 17 且小于玩家的点数 1a:如果小于 17 且小于玩家的点数,将抽取一个新号码 1-11 并将其添加到庄家手牌中 1b:它然后将运行检查它是否具有以下条件的循环

2: does the dealer have 17-21 and does it have more than the player 2a: if it has any number 17-21 and it has more than the player then it will print a win message 2:庄家是否有 17-21 且是否多于闲家 2a:如果有任何数字 17-21 且多于闲家,则打印获胜信息

3: does the dealer have over 21 3a: if it has over 21 then it will print a house busted message 3:庄家是否有超过 21 3a:如果有超过 21 则打印房屋破产信息

4: does the dealer have more than the player 4a: if the dealer has more than the player (but less than 21 because of the previous check) then it will print a house wins message 4:庄家是否多于玩家 4a:如果庄家多于玩家(但由于之前的检查而小于 21),那么它会打印一个庄家获胜消息

So that is what I am trying to accomplish with the stay command and so far i haven't found any issues with it.所以这就是我试图用留命令完成的事情,到目前为止我还没有发现任何问题。

My hit command however will not register, even if I don't type stay.. I'm trying to debug it by having it print a message and end the loop when i type hit but it still runs through the stay commands regardless, I think it could be an issue with my input converting to a string.但是,即使我不键入stay,我的命中命令也不会注册。我试图通过让它打印一条消息并在我键入命中时结束循环来调试它,但无论如何它仍然会通过停留命令运行,我认为这可能是我的输入转换为字符串的问题。

Don't be too harsh on me lol I only taught myself how to code last week and started with python.不要对我太苛刻,哈哈,我上周只自学了如何编码,并从 python 开始。

Here is my code where I am having issues:这是我遇到问题的代码:

''' '''

                    else:

                        hitstay = True

                        while hitstay:
                            action = str(input("hit or stay? "))
                            dealer = sum(dealer_cards)
                            player = sum(player_cards)
                            if action == 'stay' or 'Stay':
                                if dealer < 17 and dealer < player:
                                    dealer_cards.append(random.randint(1, 11))
                                    dealer = sum(dealer_cards)
                                    print(f"Dealer pulls {dealer_cards[-1]}\n"
                                          f"Dealer now has {sum(dealer_cards)}")
                                    if 17 <= dealer <= 21 and dealer > player:
                                        print(f"House won! Dealer cards: {dealer_cards}, {sum(dealer_cards)}\n"
                                              f"Player cards: {player_cards}, {sum(player_cards)}")
                                        playing = False
                                        hitstay = False
                                    elif dealer > 21:
                                        print(f'House busted! Dealer cards: {dealer_cards}')
                                        playing = False
                                        hitstay = False
                                    elif dealer > player:
                                        print(f'House wins, Dealer cards {dealer_cards}, {sum(dealer_cards)}\n'
                                              f'Player cards: {player_cards}, {sum(player_cards)}')
                                        playing = False
                                        hitstay = False
                            if action == 'hit' or 'Hit':
                                print('command hit')
                                playing = False
                                hitstay = False

''' '''

Instead of:代替:

if action == 'stay' or 'Stay':

do:做:

if action == 'stay' or action == 'Stay':

or:或者:

if action in ('stay', 'Stay'):

or best of all:或者最重要的是:

if action.lower() == 'stay':

The first version doesn't work because it's interpreted as:第一个版本不起作用,因为它被解释为:

if (action == 'stay') or 'Stay':

which is the same as:这与以下内容相同:

if (action == 'stay') or True:

which is the same as:这与以下内容相同:

if True:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM