在Python中实现21点

2024-05-14 20:13:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为python编写一个21点代码,我希望有人能告诉我如何实现它:

  1. 识别某人输入的内容,即“击中”或“站立”,并做出相应的反应。
  2. 计算出玩家的得分是多少,它是否是一张王牌和一个杰克在一起,并自动获胜。

好吧,这就是我到目前为止得到的。

"This imports the random object into Python, it allows it to generate random numbers."
import random
print("Hello and welcome to Sam's Black Jack!")
input("Press <ENTER> to begin.")
card1name = 1
card2name = 1
card3name = 1
card4name = 1
card5name = 1

"This defines the values of the character cards."
Ace = 1
Jack = 10
Queen = 10
King = 10

decision = 0

"This generates the cards that are in your hand and the dealer's hand to begin with.
card1 = int(random.randrange(12) + 1)
card2 = int(random.randrange(12) + 1)
card3 = int(random.randrange(12) + 1)
card4 = int(random.randrange(12) + 1)
card5 = int(random.randrange(12) + 1)

total1 = card1 + card2

"This makes the value of the Ace equal 11 if the total of your cards is under 21"
if total1 <= 21:
    Ace = 11

"This defines what the cards are"
if card1 == 11:
    card1 = 10
    card1name = "Jack"
if card1 == 12:
    card1 = 10
    card1name = "Queen"
if card1 == 13:
    card1 = 10
    card1name = "King"
if card1 == 1:
    card1 = Ace
    card1name = "Ace"

elif card1:
    card1name = card1

if card2 == 11:
    card2 = 10
    card2name = "Jack"
if card2 == 12:
    card2 = 10
    card2name = "Queen"
if card2 == 13:
    card2 = 10
    card2name = "King"
if card2 == 1:
    card2 = Ace
    card2name = "Ace"

elif card2:
    card2name = card2

if card3 == 11:
    card3 = 10
    card3name = "Jack"
if card3 == 12:
    card3 = 10
    card3name = "Queen"
if card3 == 13:
    card3 = 10
    card3name= "King"
if card3 == 1:
    card3 = Ace
    card3name = "Ace"

elif card3:
    card3name = card3

if card4 == 11:
    card4 = 10
    card4name = "Jack"
if card4 == 12:
    card4 = 10
    card4name = "Queen"
if card4 == 13:
    card4 = 10
    card4name = "King"
if card4 == 1:
    card4 = Ace
    card4name = "Ace"

elif card4:
    card4name = card4

if card5 == 11:
    card5 = 10
    card5name = "Jack"
if card5 == 12:
    card5 = 10
    card5name = "Queen"
if card5 == 13:
    card5 = 10
    card5name = "King"
if card5 == 1:
    card5 = Ace
    card5name = "Ace"

elif card5:
    card5name = card5
"This creates the totals of your hand"
total2 = card1 + card2
total3 = card1 + card2 + card3

print("You hand is ", card1name," and", card2name)
print("The total of your hand is", total2)
decision = input("Do you want to HIT or STAND?").lower()

"This is the decision for Hit or Stand"
if 'hit' or 'HIT' or 'Hit' in decision:
    decision = 1
    print("You have selected HIT")
    print("Your hand is ", card1name,",",card2name," and", card3name)
    print("The total of your hand is", total3)

if 'STAND' or 'stand' or 'Stand' in decision:
    print("You have selected STAND")

"Dealer's Hand"
dealer = card4 + card5
print()
print("The dealer's hand is", card4name," and", card5name)

if decision == 1 and dealer < total3:
    print("Congratulations, you beat the dealer!")

if decision == 1 and dealer > total3:
    print("Too bad, the dealer beat you!")

好吧,别客气,我把它修好了:D

我刚把目标改成是或否

if total2 < 21:
    decision = input("Do you want to hit? (Yes or No)")

    "This is the decision for Hit or Stand"
    if  decision == 'Yes':
        print("You have selected HIT")
        print("Your hand is ", card1name,",",card2name," and", card3name)
        print("The total of your hand is", total3)

    if decision == 'No':
            print("You have selected STAND")

Tags: theifisthisprinthandacedecision
3条回答

这可以让你开始:

http://docs.python.org/library/random.html

http://docs.python.org/library/strings.html

http://docs.python.org/library/stdtypes.html

http://docs.python.org/reference/index.html

我看到你加了一些代码,很好。

想想你的程序中需要存在的部分。你需要一些“卡片”的表示——卡片有重要的特征,比如它们的价值,它们的套装等等。给定一张卡片,你应该能够知道它的价值是什么,是杰克还是王牌,还是红桃2号。阅读Python中的“类”开始学习。

你还将拥有一手牌——你的庄家当前持有的牌,以及你的玩家当前持有的牌。“手”是卡片的集合,您(程序员)可以向其中添加新卡片(当卡片被处理时)。您可能希望使用包含这些数组的“列表”、“数组”或“类”来实现这一点。一只手也有一个值,通常是卡片值的总和,但是正如你所知,ace是特殊的(可以是1或11),所以你需要用一些“if语句”来正确地处理这个情况。

你还将拥有一副牌;一副牌是一个特殊的集合——它在开始时正好有52张牌,而且没有一张牌是重复的(当然,你可以使用几个牌组来玩,但这是一个复杂的问题,你可以在以后解决)。你如何填充这样的甲板?你的程序需要从牌堆中“处理”牌,所以你需要一种方法来跟踪哪些牌已经发给了玩家。

那是很多东西。试着用简单的句子写下程序需要做的所有逻辑,而不用担心Python。这叫做“伪代码”。这不是一个真正的程序,它只是一个计划,你到底要做什么——这是有用的方式地图是有用的。如果你要去一个你去过100次的地方,你不需要地图,但是如果你要开车去一个你从未去过的城镇,你要先规划好你的路线,然后再开车。。

用您的伪代码更新您的问题,以及您已经(或将要)尝试将伪代码转换为Python的任何尝试。

在你的代码中,你写道:

Ace = 1 or 11

恐怕这不符合你的想法。启动python解释器,然后在其中键入1 or 11。我得到的是:

>>> 1 or 11
1

这意味着当您键入:Ace = 1 or 11时,python首先计算1 or 11位,然后将Ace设置为该位。换句话说,您的代码相当于:

Ace = 1

我建议你忘记一个Ace的两个可能值;只保留它为1。简化规则,做些有用的事情,然后你就可以考虑做一个完整的21点游戏了。

我同意SquareCog的评论——一些关于你尝试过什么,什么不起作用,什么让你困惑的附加信息会有帮助。

一些可能有用的信息,但是:

关于1-10之间的数字生成,ace,king,queen和jack:给每张卡分配一个数字索引可能会有帮助。2-10是显而易见的,你可以为杰克、皇后、国王和王牌创造自己的价值。有一点要特别注意,如果你也在生成ace,那么就没有1。一旦您分配了号码,random module就可以提供帮助。

有一些标准的比较方法可以用来识别“击中”和“站立”之间的区别。尤其是==operator

因为你正在生成手,所以应该很容易检查它们是否是特定的组合。至于计算分数,一个好的起点是使用加法。

编辑:根据您随后的评论,您可能会从"Python for non-programmer" guides中受益。

相关问题 更多 >

    热门问题