Редактировать!!! Создание игры в блэкджек в Python, во время игры произошла ошибка - PullRequest
0 голосов
/ 21 апреля 2020

Код работал, но результаты были не те, что я хочу получить.

В игре «Блэкджек» должно быть «A» как 1 или 11, но она не работала

By по некоторым причинам, если я 'h' (ударил) по карте, она не позволит мне go вернуться к '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 голосов
/ 21 апреля 2020

Для такой программы требуется база для работы. Я создал простую базу и несколько простых функций, которые позволят вам делать такие вещи, как извлечение карт, печать карт и получение суммы карт игроков (избегая дублирования во всех сценариях ios). Вы должны быть в состоянии выяснить логи c относительно того, когда игрок / дилер должен взять другую карту.

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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...