-1
from random import choice as rc

def total(hand):

    aces = hand.count(11)


    t = sum(hand)

    if t > 21 and aces > 0:
        while aces > 0 and t > 21:

            t -= 10
            aces -= 1
    return t

cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]

cwin = 0  
pwin = 0 
while True:
    player = []

    player.append(rc(cards))
    player.append(rc(cards))
    pbust = False  
    cbust = False  
    while True:

        tp = total(player)
        print ("The player has these cards %s with a total value of %d" % (player, tp))
        if tp > 21:
            print ("--> The player is busted!")
            pbust = True
            break

        elif tp == 21:
            print ("\a BLACKJACK!!!")
            break

        else:
            hs = input("Hit or Stand/Done (h or s): ").lower()
            if 'h' in hs:
                player.append(rc(cards))
            else:
                break
    while True:

        comp = []
        comp.append(rc(cards))
        comp.append(rc(cards))

        while True:
            tc = total(comp)                
            if tc < 18:
                comp.append(rc(cards))
            else:
                break

        print ("the computer has %s for a total of %d" % (comp, tc))

        if tc > 21:
            print ("--> The computer is busted!")
            cbust = True
            if pbust == False:
                print ("The player wins!")
                pwin += 1
        elif tc > tp:
            print ("The computer wins!")
            cwin += 1
        elif tc == tp:
            print ("It's a draw!")
        elif tp > tc:
            if pbust == False:
                print ("The player wins!")
                pwin += 1
            elif cbust == False:
                print ("The computer wins!")
                cwin += 1
        break

    print ("Wins, player = %d  computer = %d" % (pwin, cwin))
    exit = input("Press Enter (q to quit): ").lower()
    if 'q' in exit:
        break
print ("Thanks for playing blackjack with the computer!")

我是 python 新手。只是一个初学者。这不是我的实际代码。我做了一些调整。但大多数情况下,整体功能及其执行方式几乎相似。除了只显示数字,还有什么方法可以更改为:“黑桃”、“俱乐部”、“红心”、“钻石”、“A”、“2”、“3”、“4”、“ 5”、“6”、“7”、“8”、“9”、“10”、“J”、“Q”、“K”但仍能传递表示的值以集成到部分代码中. 我希望随着时间的推移可以改进我的代码。我对类还不是很熟悉,尽管它会使代码看起来很干净。希望任何人都可以指出错误或做出一些改进。

4

2 回答 2

0
from random import choice as rc

def printCardNames(hand):


    for i in hand:
        name = ""
        if(i < 13):
            name += "Spades "
        elif(i < 26):
            name += "Clubs "
        elif(i < 39):
            name += "Hearts "
        else:
            name += "Diamonds "

        cardName = i % 13
        if(cardName == 0):
            name += "2,"
        elif(cardName == 1):
            name += "3,"
        elif(cardName == 2):
            name += "4,"
        elif(cardName == 3):
            name += "5,"
        if(cardName == 4):
            name += "6,"
        if(cardName == 5):
            name += "7,"
        if(cardName == 6):
            name += "8,"
        if(cardName == 7):
            name += "9,"
        if(cardName == 8):
            name += "10,"
        if(cardName == 9):
            name += "J,"
        if(cardName == 10):
            name += "Q,"
        if(cardName == 11):
            name += "K,"
        if(cardName == 12):
            name += "A,"
        print(name)






def total(hand):

    values = []  # I will use value of the cards to calculate total value
    for i in hand:
        values.append(card_values[i])


    aces = values.count(11)


    t = sum(values)

    if t > 21 and aces > 0:
        while aces > 0 and t > 21:

            t -= 10
            aces -= 1
    return t

cards = range(0,52) # I will pick a card, not a card value

card_values = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11, # corresponding card values
         2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
         2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11,
         2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]

cwin = 0  
pwin = 0 
while True:
    player = []

    player.append(rc(cards))
    player.append(rc(cards))
    pbust = False  
    cbust = False  
    while True:

        tp = total(player)
        print ("The player has these cards:")
        printCardNames(player)
        print ("Total Value of:", tp)
        if tp > 21:
            print ("--> The player is busted!")
            pbust = True
            break

        elif tp == 21:
            print ("\a BLACKJACK!!!")
            break

        else:
            hs = input("Hit or Stand/Done (h or s): ").lower()
            if 'h' in hs:
                player.append(rc(cards))
            else:
                break
    while True:

        comp = []
        comp.append(rc(cards))
        comp.append(rc(cards))

        while True:
            tc = total(comp)                
            if tc < 18:
                comp.append(rc(cards))
            else:
                break

        print ("the computer has")
        printCardNames(comp)
        print ("Total Value of:", tc)

        if tc > 21:
            print ("--> The computer is busted!")
            cbust = True
            if pbust == False:
                print ("The player wins!")
                pwin += 1
        elif tc > tp:
            print ("The computer wins!")
            cwin += 1
        elif tc == tp:
            print ("It's a draw!")
        elif tp > tc:
            if pbust == False:
                print ("The player wins!")
                pwin += 1
            elif cbust == False:
                print ("The computer wins!")
                cwin += 1
        break

    print ("Wins, player = %d  computer = %d" % (pwin, cwin))
    exit = input("Press Enter (q to quit): ").lower()
    if 'q' in exit:
        break
print ("Thanks for playing blackjack with the computer!")
于 2019-11-07T19:56:49.153 回答
0

我认为您应该从制作套牌开始,而不仅仅是数字:

import itertools
suits = ["hearts", "spades", "diamonds", "clubs"]
cards = [("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)]

deck = list(itertools.product(suits, cards))

然后你可以根据你想要的索引。数的时候可以选择数值,打印的时候可以看名字和西装。

一些通用的编程技巧:

  • 将更多内容提取到函数中。如果您的游戏循环调用命名良好的函数,则更容易查看您的整体程序结构。
  • while True:这个程序中的循环数是可怕的。仅在绝对必要时使用它们,因为它们非常容易出错。
  • 更明智地使用空格。将它们视为代码中的段落刹车。
  • 使您的变量名称更具描述性(player_total而不是 tpcomputer_hand而不是comp
于 2019-11-07T19:48:34.600 回答