gpt4 book ai didi

Python 二十一点游戏 : Issues with dictionary for values

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

我一直在学习 Jose Portilla 的 udemy python 类(class),对于我们的一个项目,我们必须创建一个基于文本的二十一点游戏。我的部分代码有错误,如下所示。

我为卡片创建了一个值字典,这样我就可以很容易地看到一张卡片值多少钱(例如,如果我有一张梅花国王,我希望它等于 10)。

rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}

然后我有一个处理玩家手牌的类。我在这部分遇到了问题,所以解决方案如下所示。

class Hand:

def __init__(self):
self.cards = [] # start with an empty list as we did in the Deck class
self.value = 0 # start with zero value
self.aces = 0 # add an attribute to keep track of aces

def add_card(self,card):
self.cards.append(card)
self.value += rankValues[card.rank] #Where I need help!#
if card.rank == 'Ace':
self.aces += 1 # add to self.aces

def adjust_for_ace(self):
while self.value > 21 and self.aces:
self.value -= 10
self.aces -= 1

我完全不明白这一行。非常感谢您的帮助!

编辑 - 完整代码

import random
import time


suits = ['Clubs', 'Spades', 'Diamonds', 'Hearts']
ranks = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace']
rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}


class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank

def __str__(self):
return(f'{self.rank} of {self.suit}')


class Deck:
def __init__(self):
self.deck = []
for suit in suits:
for rank in ranks:
self.deck.append(Card(rank, suit))

def __str__(self):
comp = ''
for card in self.deck:
comp += '\n' + card.__str__()
return comp

def shuffle(self):
random.shuffle(self.deck)

def deal(self):
single_card = self.deck.pop()
return single_card

class Chips:

def __init__(self):
self.chips = 100

def win_bet(self, opponent_bet):
self.chips += opponent_bet

def lose_bet(self, my_bet):
self.chips -= my_bet

def __str__(self):
return(str(self.chips))

class Hand:
def __init__(self):
self.cards = []
self.points = 0
self.aces = 0

def add_card(self, card):
self.cards.append(card)
self.points += rankValues[card.rank] #error

def adjust_for_aces(self):
if self.points > 21 and self.aces:
self.aces -= 1
self.points -= 10

def __str__(self):
strversion = ' '
for card in self.cards:
strversion.append(card)

最佳答案

EDIT后,还不清楚你不明白的地方。你有一个字典,其中包含在类卡中定义的卡的值:

class Card:
def __init__(self, suit, rank):
self.suit = suit
self.rank = rank

rankValues = {'Two': 2, 'Three': 3, 'Four': 4, 'Five': 5, 'Six': 6, 'Seven': 7, 'Eight': 8, 'Nine': 9, 'Ten': 10, 'Jack': 10, 'Queen': 10, 'King': 10, 'Ace': 11}

示例:

c1 = Card("Clubs", "Queen")

# To find the value, you need to look at what a Queen is worth:
rankValues["Queen"] # Returns 10. The key if place between [].

# Same as:
rankValues[c1.rank] # Because the attribute rank of c1 is "Queen"

现在是:

class Hand:
# Init an empty hand
def __init__(self):
self.cards = []
self.points = 0
self.aces = 0

# Add an object card to the hand
def add_card(self, card):
self.cards.append(card)
self.points += rankValues[card.rank]

c1 示例:

my_hand = Hand()
my_hand.add_card(c1) # Enters the method add_card()

# What it does:
# self.cards.append(card) => my_hand.cards is a list() and will have the card c1 appended.
# self.points += rankValues[card.rank]
# => self.points is a value that will be incremented by the value of the card added.
# The value is given by the dictionnary rankValues, and is fetch by the rank of the card as the key.

关于Python 二十一点游戏 : Issues with dictionary for values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51038515/

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