python-3.6 - 二十一点,蟒蛇
问题描述
import random
import time
class card():
def __init__(self):
cardNum = ["1","2", "3", "4", "5", "6", "7", "8", "9", "10", "10", "10"]
randNum = random.randint(0, len(cardNum) - 1)
cardSuit = ["Hearts", "Clubs", "Diamonds", "Spades"]
randSuit = random.randint(0, len(cardSuit) - 1)
self.rank = cardNum[randNum]
self.suit = cardSuit[randSuit]
total1 = []
total2 = []
total1.append(self.rank)
def printIntro():
print("Blackjack (twenty-one) is a casino game played with cards.")
print("the goal of game is to draw cards that total as close to 21
points, as possibale")
print("without going over( whose hand > 21 will bust). All face
cards count as 10 points,")
print("aces count as 1 or 11, and all other cards count their
numeric value.")
print("\nFirstly, your turn:")
def displayCard(self):
return (self.rank+ " of "+ self.suit)
def totalScore(self, ans="Y"):
total1 = []
total2 = []
if ans == "Y":
totalscore1 = total1.append(self.rank)
return totalscore1
ans = "N"
if ans == "N":
totalscore2 = total2.append(self.rank)
return totalscore2
def aceOption(self, cards1, cards2):
if "Ace" in cards1:
print("You have an ACE!")
opt = int(input("Would you like ACE to be 1 or 11?: "))
if opt == "1":
aceVal(1)
elif opt == "11":
aceVal(11)
elif "Ace" in cards2:
print("You have an ACE!")
opt = int(input("Would you like ACE to be 1 or 11?: "))
if opt == "1":
aceVal(1)
elif opt == "11":
aceVal(11)
def nextOption(self):
opt = input("Would you like to (T)WIST, or (S)TICK?")
if opt == "T":
#make twist function
elif opt == "S":
#make stick function
#myCard = card()
class player():
def __init__(self, name="bob"):
self.cards = []
self.name = name
self.cards2 = []
def dealer1go(self):
for i in range(0, 1):
newcard = card()
self.cards2.append(newcard.displayCard())
def player1go(self):
for i in range(0, 1):
newcard = card()
self.cards.append(newcard.displayCard())
class totalAmount():
def __init__(self):
pass
player1 = player("Matt")
dealer = player("Kieron")
deck = card()
intro =card.printIntro()
print(" ")
print(intro)
print("\nPlayer1 is dealt two cards..")
print("-------------------------------\n")
time.sleep(1)
print("\nDealer has two cards..")
print("-------------------------------\n")
time.sleep(1)
for i in range(1, 3):
player1.player1go()
dealer.dealer1go()
print("Player1 cards: ", player1.cards)
#card.totalScore("Y")
deck.aceOption(player1.cards, dealer.cards2)
print("Dealers cards: ", dealer.cards2)
#card.totalScore("N")
deck.aceOption(player1.cards, dealer.cards2)
deck.nextOption()
card()
totalScore(self) 函数和 aceOption(self) 函数不起作用,我一直对如何链接它们感到困惑。目前正在学习如何使用类和(自我)。
目前的输出只是随机发给玩家 1 和庄家的 2 张牌,然后放在一个数组中。我需要给用户一个选项来选择一个 ace,如果他们有一个,是 11 或 1。
我需要卡值的总分,如果总分>21,那么他们就是 BUST,游戏就结束了。
解决方案
由于您是 A-Level 学生,因此我们在回答您的问题时必须考虑到这一点。当您在第 7 行写下“randNum = random.randint(0, len(cardNum) - 1)”时,您将卡片选择限制为 12 张卡片,而不是每套花色 13 张卡片。这将为您留下 48 张卡,例如 Lee Daniel Crocker 提到但实际上并未解释。如果这是您想要做的,您还可以让用户选择更改 ace,因为它是您的代码,但您可以将其保留到最后以简化操作。
推荐阅读
- datepicker - Vuetify open datepicker nr 2 on datepicker nr 1's value
- python-3.x - how to make invisible existing field Quotation Date from sales in odoo 13
- javascript - How to group multiple series in Highcharts
- wso2 - WSO2 API 分析 (3.1.0-m4) 仪表板不工作
- ios - 考虑到需要在某些类中导入框架,如何在 iOS 项目的特定目标中使用框架?
- video - Vimeo 嵌入(非 iFrame)没有全屏选项。需要解决方法
- highcharts - 多个十字准线
- python - 我的 pipenv 不再在我的任何项目文件夹中创建 pipfile 和 pipfile.lock
- c++ - Ubuntu 19.10:启用和使用 Raspberry Pi 摄像头模块 v2.1
- python - 从 CSV 中删除 b' 前缀