gpt4 book ai didi

python - 在 Python 中实现二十一点

转载 作者:太空宇宙 更新时间:2023-11-03 12:13:35 24 4
gpt4 key购买 nike

我正在为 python 编写 blackjack 代码,我希望有人能够告诉我如何制作它:

  1. 识别某人输入的内容,即“击中”或“停止”并做出相应 react 。
  2. 计算玩家的分数是多少,是否同时是A和J,自动获胜。

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

"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

我只是将 Hit and Stand 更改为 Yes 或 No

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")

最佳答案

这可以让你开始:

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

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

考虑您的程序中需要存在的部分。您将需要一些“纸牌”的表示形式——纸牌具有重要特征,例如它们的值(value)、花色等。给定一张纸牌,您应该能够说出它的值(value)是多少,无论是 J 还是 A 还是 2的心。阅读 Python 中的“类”以开始使用。

您还将有一手牌——庄家当前持有的牌和闲家当前持有的牌。 “手”是卡片的集合,您(程序员)可以向其中添加新卡片(发卡片时)。您可能希望使用包含这些数组的“列表”或“数组”或“类”来做到这一点。一手牌也有一个值,通常是牌值的总和,但如您所知,A 很特殊(它们可以是 1 或 11),因此您需要使用一些“if 语句”正确处理这种情况。

你也会有一个套牌;一副牌是一个特殊的集合——它在开始时正好有 52 张牌,并且没有一张牌是重复的(当然,你可以使用几副牌来玩,但这是一个你可以稍后解决的复杂问题)。你如何填充这样的甲板?您的程序将希望从一副牌中“发牌”——因此您需要一种方法来跟踪哪些牌已发给玩家。

那是很多东西。试着用简单的句子写下你的程序需要做什么的所有逻辑,而不用担心 Python。这称为“伪代码”。它不是一个真正的程序,它只是一个关于你将要做什么的计划——它的用处就像一张 map 一样有用。如果你要去一个你去过 100 次的地方,你不需要 map ,但如果你开车去某个你从未去过的城镇,你需要先计划好你的路线,然后再去在方向盘后面..

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

关于python - 在 Python 中实现二十一点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/551840/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com