简单21点游戏:牌组列表变为非典型

2024-03-28 21:01:16 发布

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

我厌倦了用Python制作一个简单的21点游戏。当执行代码时,卡片组中的卡片列表(self.deck)会产生问题-当它应该是一个列表时,会变成非类型

'''
This is a blackjack game for the project 2
'''
import random

cardsuits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cardrank = ['Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace']
cardvalues = {'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:
    #class card has two attributes: suit and rank. asking for string will result in rank of suit response. There is a value calcluated, using cardvalues dictionary.
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
    def __str__(self):
        return self.rank + ' of ' + self.suit
    #def value(self):
    #   self.value = cardvalues[self.rank]
    #   #think about ace here; it can be worth 10 or 1 depending on hand 

class Deck:
    def __init__(self):
        self.deck = []
        for suit in cardsuits:
            for rank in cardrank:
                self.deck.append(Card(suit,rank))
    def __str__(self):
        deck_comp = ""
        for card in self.deck:
            deck_comp = deck_comp + "\n" + card.__str__()
        return "Deck contains " + deck_comp

    def reroll(self):
        self.deck = random.shuffle(self.deck)
    def take_a_card(self):
        single_card = self.deck.pop()
        return single_card


class Hand:
    def __init__(self):
        self.cards = []
        self.value = 0
        self.aces = 0
    def add_card(self, card):
        self.cards.append(card)
        self.value = self.value + cardvalues[card.rank]
        if card.rank == "Ace":
            self.aces = self.aces + 1
    def ace_adjust(self):
        if self.value > 21 and self.aces > 0:
            self.value = self.value - 10
            self.aces = self.aces -1

class Funds:
    def __init__(self):
        self.balance= 0
        self.bet = 0
    def winbet(self):
        self.balance = self.balance + self.bet * 2
    def losebet(self):
        self.bet = 0
    def draw(self):
        self.balance = self.balance + self.bet
        self.bet = 0


def Place_a_Bet():
    while True:
        placebetamount = int(input(print('Select amount you want to bet.')))
        if isinstance(placebetamount, int) == False:
            print('Invalid bet type. You have to input a digit.')
            continue
        elif placebetamount > PlayerFunds.balance:
            print('You tried to bet more money than you have available. You cannot bet more than: ' + PlayerFunds.balance)
            continue
        elif placebetamount <= 0:
            print('Invaild Bet. You have to bet a positive number.')
            continue
        else:
            PlayerFunds.balance = PlayerFunds.balance - placebetamount
            PlayerFunds.bet = placebetamount
            break

def hit(deck,hand):

    hand.add_card(CurrentGameDeck.take_a_card())
    hand.ace_adjust()

def hit_or_stand(deck, hand):
    global playing  # to control an upcoming while loop

    Decision = input(str(print('Your Turn. Type "Hit" to get another card. Type "Stand" to move onto Dealers move')))
    while True:
        if Decision == "Hit":
            hit(deck,hand)
        elif Decision == "Stand":
            playing = False
        else: 
            print('just Hit or Stand. Other inputs are invalid')
            continue
        break


def CardStateGame(PlayerHand, DealerHand):
    print('Dealer Shows : ' + DealerHand.cards[0] + ' out of ' + len(DealerHand.cards)) 
    print('Dealer score is :' + DealerHand.value)
    print('Your cards are ')
    for card in PlayerHand.cards:
        print(card)
    print('and their value is: ' + PlayerHand.value)

def CardStateEndgame(PlayerHand, DealerHand):
    print('Dealer value is ' + DealerHand.value + ' and his cards are: ')
    for card in DealerHand.cards:
        print(card)
    print('Your cards are ')
    for card in PlayerHand.cards:
        print(card)
    print('and their value is: ' + PlayerHand.value)

#end conditions
def player_bust(PlayerHand, PlayerFunds):
    print('Value of your cards (' + PlayerHand.value +') exceeds 21. You lose.')
    PlayerFunds.losebet()
def player_wins(PlayerFunds):
    print('You won.')
    PlayerFunds.winbet()
def dealer_bust(DealerHand, PlayerFunds):
    print('Dealer loses, because value of his cards (' + DealerHand.value +') exceeds 21')
    PlayerFunds.winbet()
def dealer_wins(PlayerFunds):
    print('Dealer won.')
    PlayerFunds.losebet()
def draw(PlayerFunds):
    print('Both participants have same values. Its a draw')
    PlayerFunds.draw()

#gameplay
print('This is a game of blackjack. You and the dealer (AI) will try to reach card value of 21, or just higher than your opponent. Scores higher than 21 will lose')
PlayerFunds = Funds()

while True:
    startamount = int(input(print('Select the amount of money you enter the game with. Try to keep it plausible')))
    if startamount <= 0 or isinstance(startamount, int) == False:
        print('You cant play with that amount of money. try again')
        continue
    else:
        PlayerFunds.balance = startamount
        break
playing = True
while True:

    CurrentGameDeck = Deck()
    CurrentGameDeck.reroll()

    Place_a_Bet()


    DealerHand = Hand()
    PlayerHand = Hand()

    DealerHand.add_card(CurrentGameDeck.take_a_card())  
    PlayerHand.add_card(CurrentGameDeck.take_a_card())
    DealerHand.add_card(CurrentGameDeck.take_a_card())
    PlayerHand.add_card(CurrentGameDeck.take_a_card())

    CardStateGame(PlayerHand, DealerHand)

    while playing == True:

        hit_or_stand(CurrentGameDeck,PlayerHand)

        CardStateGame(PlayerHand, DealerHand)

        if PlayerHand.value >21:
            player_bust(PlayerHand, PlayerFunds)
            break
    if PlayerHand.value <= 21:

        while DealerHand.value <17:
            hit(CurrentGameDeck, DealerHand)

        CardStateEndgame

    if DealerHand.value > 21:
        dealer_bust()
    elif DealerHand.value > PlayerHand.value:
        dealer_wins()
    elif PlayerHand.value > DealerHand.value:
        player_wins()
    elif PlayerHand.value == DealerHand.value:
        draw()

    newgame = 0
    newgame = input(print('Do you want to play again? Type Y or N'))

    if newgame.lower() == 'Y' and PlayerFunds.balance >=0:
        playing = True
        continue
    elif newgame.lower() == 'Y' and PlayerFunds.balance < 0:
        print('no more funds. GG')
        break
    else:
        print('thanks for playing')
        break

我试着只使用

cardsuits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']
cardrank = ['Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten','Jack','Queen','King','Ace']
cardvalues = {'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:
    #class card has two attributes: suit and rank. asking for string will result in rank of suit response. There is a value calcluated, using cardvalues dictionary.
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
    def __str__(self):
        return self.rank + ' of ' + self.suit
    #def value(self):
    #   self.value = cardvalues[self.rank]
    #   #think about ace here; it can be worth 10 or 1 depending on hand 

class Deck:
    def __init__(self):
        self.deck = []
        for suit in cardsuits:
            for rank in cardrank:
                self.deck.append(Card(suit,rank))
    def __str__(self):
        deck_comp = ""
        for card in self.deck:
            deck_comp = deck_comp + "\n" + card.__str__()
        return "Deck contains " + deck_comp

    def reroll(self):
        self.deck = random.shuffle(self.deck)
    def take_a_card(self):
        single_card = self.deck.pop()
        return single_card

然后

test = Deck()

允许我洗牌,并弹出一张牌,所以代码的其他部分可能是一个问题,但我不知道哪个,在哪里,为什么


Tags: inselfforvaluedefcardcardsprint
1条回答
网友
1楼 · 发布于 2024-03-28 21:01:16

在将来,您应该尝试精简代码以查明问题所在。因为问题围绕着甲板,所以你只需要发布这个类。您收到的错误是由于以下函数引起的:

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

如果您查看一下docs,您将看到此函数在适当的位置执行shuffle。将此函数改为:

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

相关问题 更多 >