编辑!!!使用Python开发的21点游戏,玩时发生错误

时间:2020-04-21 12:41:20

标签: python python-3.x random while-loop blackjack

代码有效,但是结果却不是我想要得到的。

二十一点游戏应该提供“ A”为1或11,但不能正常工作

由于某些原因,如果我“ h”(击中)卡,它不会让我回到“ s”(站立)

如果我有二十一点,即使经销商没有二十一点,庄家也会再次开卡。

可能还有其他问题。...我不知道:(

我对def不熟悉,所以下次我可能会尝试再次学习def。.

我真的很想解决这个问题。


import random

card_dict = {'A': 11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}

cardcolor = ['S', 'H', 'C', 'D']


def player_draw():
    return random.choice(list(card_dict))

def dealer_draw():
    return random.choice(list(card_dict))

player_color = random.choice(cardcolor)
dealer_color = random.choice(cardcolor)

player_score = 0
dealer_score = 0

player_cards = []

while True:
    player_card = []
    player_card.append(player_draw())
    player_card.append(player_draw())
    print('your cards:',player_card[0]+player_color,player_card[1]+player_color)
    player_total = card_dict[player_card[0]]+card_dict[player_card[1]] 

    dealer_card = []
    dealer_card.append(dealer_draw())
    dealer_card.append(dealer_draw())

    dealer_cards = []

    for i in dealer_card:
        dealer_cards.append(card_dict[i])

    dealer_total = sum(dealer_cards)    

    stand_hit = input('(S)tand or (H)it?...: ')



    if player_total == 21:
        if dealer_total != 21:
            print('Blackjack!')
            player_score += 1
            print('Dealer:', dealer_score, 'Player:', player_score)
            play_again = input('Play again (Y/N)?...: ')

            if play_again == 'y':
                print()
            elif play_again == 'n':
                print('\n')
                break
        elif dealer_total == 21:
            print('It is too bad')


    if stand_hit == 's':

        print('Dealer cards:', dealer_card[0]+dealer_color, dealer_card[1]+dealer_color)

        if dealer_total >= 17:

            if dealer_total > player_total:
                print('Dealer wins')
                dealer_score += 1
                print('Dealer:', dealer_score, 'Player:', player_score)
                play_again = input('Play again (Y/N)?...: ')
                if play_again == 'y':
                    print()
                    continue
                elif play_again == 'n':
                    print('\n')
                    break

            elif dealer_total == player_total:
                print('Draw')
                print('Dealer:', dealer_score, 'Player:', player_score)
                play_again = input('Play again (Y/N)?...: ')
                if play_again == 'y':
                    print()
                    continue
                elif play_again == 'n':
                    print('\n')
                    break

            elif dealer_total < player_total:
                print('You win!')
                player_score += 1
                print('Dealer:', dealer_score, 'Player:', player_score)

                play_again = input('Play again (Y/N)?...: ')
                if play_again == 'y':
                    print()
                    continue
                elif play_again == 'n':
                    print('\n')
                    break
        elif dealer_total < 17:
            while dealer_total < 17:
                if player_total or dealer_total < 11:
                    card_dict['A'] = 11

                elif player_total or dealer_total > 10:
                    card_dict['A'] = 1

                a = random.choice(list(card_dict))
                print('Dealer drew:', a + dealer_color)
                dealer_total += card_dict[a]

                if dealer_total > 21:
                    print('You win!')
                    player_score += 1
                    print('Dealer:', dealer_score, 'Player:', player_score)
                    play_again = input('Play again (Y/N)?...: ')
                    if play_again == 'y':
                        print()
                        continue
                    elif play_again == 'n':
                        print('\n')
                        break

                elif dealer_total <= 21:
                    if dealer_total > player_total:
                        print('Dealer wins')
                        dealer_score += 1
                        print('Dealer:', dealer_score, 'Player:', player_score)
                        play_again = input('Play again (Y/N)?...: ')
                        if play_again == 'y':
                            print()
                            continue
                        elif play_again == 'n':
                            print('\n')
                            break

                    elif dealer_total == player_total:
                        print('Draw')
                        print('Dealer:', dealer_score, 'Player:', player_score)
                        play_again = input('Play again (Y/N)?...: ')
                        if play_again == 'y':
                            print()
                            continue
                        elif play_again == 'n':
                            print('\n')
                            break

                    elif dealer_total < player_total:
                        print('You win!')
                        player_score += 1
                        print('Dealer:', dealer_score, 'Player:', player_score)

                        play_again = input('Play again (Y/N)?...: ')
                        if play_again == 'y':
                            print()
                            continue
                        elif play_again == 'n':
                            print('\n')
                            break


    elif stand_hit == 'h':

        if player_total or dealer_total < 11:
            card_dict['A'] = 11

        elif player_total or dealer_total > 10:
            card_dict['A'] = 1

        a_cards = random.choice(list(card_dict))
        print('You drew:', a_cards + player_color) 
        player_total += card_dict[a_cards]

        if player_total > 21:
            print('Dealer wins')
            dealer_score += 1
            print('Dealer:', dealer_score, 'Player:', player_score)
            play_again = input('Play again (Y/N)?...: ')
            if play_again == 'y':
                print()
                continue
            elif play_again == 'n':
                print('\n')
                break
        elif player_total <= 21:

            stand_hit = input('(S)tand or (H)it?...: ')            

1 个答案:

答案 0 :(得分:0)

像这样的程序需要一个基础来解决。我创建了一个简单的基础和一些简单的功能,这些功能将使您能够进行抽奖卡,打印卡和获得玩家卡的总和(同时避免在所有情况下重复)。您应该能够弄清楚何时玩家/经销商应该再抓一张牌。

import random

card_dict = {'A': 11, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, 'T':10, 'J':10, 'Q':10, 'K':10}

cardcolor = ['S', 'H', 'C', 'D']

#We define an empty list for three sets of cards. A players cards, a dealers cards, and all drawn cards
player_cards = []
dealer_cards = []
drawn_cards = []

player_score = 0
dealer_score = 0

# This is a function that will draw a card and add it to a list
def drawCard(cardlist):
    player_card = (random.choice(list(card_dict)), random.choice(cardcolor))
    while player_card in drawn_cards:
        player_card = (random.choice(list(card_dict)), random.choice(cardcolor))
    cardlist.append(player_card)
    drawn_cards.append(player_card)

# This is a simple function to convert a card tuple to a string
def printCard(card):
    return card[0] + card[1]

def printCards(cardlist):
    cards = ""
    for card in cardlist:
        cards += printCard(card) + " "
    return cards



# This is a function to return the total value of a list of cards
def getTotalValue(cards):
    total_sum = 0
    for card in cards:
        total_sum += card_dict[card[0]]
    return total_sum

# Draw 2 for player
for i in range(2):
    drawCard(player_cards)

# Draw 2 for dealer
for i in range(2):
    drawCard(dealer_cards)

# If you want to print players cards
print('Players cards: ', printCards(player_cards))

# If you want to print dealer cards
print('Dealer cards: ', printCards(dealer_cards))

# If you want to draw a card for player
drawCard(player_cards)
# If you want to draw a card for dealer
drawCard(dealer_cards)

#If you want to get the sum of players cards
player_total = getTotalValue(player_cards)

#If you want to get sum of dealers cards
dealer_total = getTotalValue(dealer_cards)