首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单Blackjack游戏

简单Blackjack游戏
EN

Code Review用户
提问于 2022-10-23 19:19:09
回答 1查看 105关注 0票数 1

这是我用Python制作的简单Blackjack游戏。我希望有一些反馈来编写更好的代码。

代码语言:javascript
复制
from art import logo    
import random

input("Do you want to play black-jack ? 'y' or 'no': ")
print()
print(logo)

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


def print_score(score_to_print):

    if score_to_print == "user":
        print(f"user_hand {user_hand} your score = {get_score(user_hand)}")
    elif score_to_print == "com":
        print(f"com_hand{com_hand} com_score = {get_score(com_hand)}")


def get_score(hand):
    score = 0
    for i in range(len(hand)):
        score += hand[i]

    return score


def restart_game(result):

    print(result)
    print_score("user")
    print_score("com")

    user_hand.clear()
    com_hand.clear()
    user_input = input("Do you want to play black-jack ? 'y' or 'no': ")
    if user_input == 'y':
        deal_cards(cards)
    else:
        print("ok bye bye have fun in the afterlife")


def check_for_blackjack(hand):
    if get_score(com_hand) == 21:
        restart_game("You Lose")
    elif get_score(user_hand) == 21:
        restart_game("you Win")


def check_for_score_over_21():
    if get_score(user_hand) > 21:
        restart_game("You Lose")
    elif get_score(com_hand) > 21:
        restart_game("You Win")


def check_for_ace(card, hand):
    if get_score(hand) + card > 21:
        card = 1
    return card


def check_for_winner():
    user_score = get_score(user_hand)
    com_score = get_score(com_hand)
    if user_score == com_score:
        restart_game("Draw")
    elif user_score < com_score:
        restart_game("You Lose")
    else:
        restart_game("You Win")


def deal_computer_cards():
    while get_score(com_hand) < 17:
        new_card = random.choice(cards)
        com_hand.append(check_for_ace(new_card, com_hand))


def ask_for_new_card():
    print_score("user")
    user_choice = input("Do you want another card ?: ")

    if user_choice == 'y':
        new_card = random.choice(cards)
        user_hand.append(check_for_ace(hand=user_hand, card=new_card))
        if get_score(user_hand) < 21:
            ask_for_new_card()
        elif get_score(user_hand) == 21:
            restart_game("You Win")
        elif get_score(user_hand) > 21:
            restart_game("You Lose")
        else:
            print_score("user")
    elif user_choice == 'n':
        deal_computer_cards()
        check_for_winner()


def add_cards_to_hand(hand):
    card1 = random.choice(cards)
    card2 = random.choice(cards)
    hand.append(check_for_ace(card1, hand))
    hand.append(check_for_ace(card2, hand))


def deal_cards(cards):
    add_cards_to_hand(user_hand)
    print_score("user")
    
    add_cards_to_hand(com_hand)
    check_for_blackjack(com_hand)
    check_for_blackjack(user_hand)
    
    print(f"com_hand {com_hand} com score = {get_score(com_hand)}")
    print(f"computer first card is a {com_hand[0]}")
    
    ask_for_new_card()

deal_cards(cards)
EN

回答 1

Code Review用户

发布于 2022-10-23 20:34:20

input("Do you want to play black-jack ? 'y' or 'no': ")

这一行不执行任何操作,因为您从未将input函数的结果分配给名称或将其用作参数。

代码语言:javascript
复制
def print_score(score_to_print):
    if score_to_print == "user":
        print(f"user_hand {user_hand} your score = {get_score(user_hand)}")
    elif score_to_print == "com":
        print(f"com_hand{com_hand} com_score = {get_score(com_hand)}")

为了避免不必要的分支,可以将其分成两部分:

代码语言:javascript
复制
def print_user_info():
    print(f"Your hand: {*user_hand}")
    print(f"Your score: {get_score(user_hand)}")


def print_bot_info():
    print(f"Bot's hand: {*bot_hand}")
    print(f"Bot's score: {get_score(bot_hand)}")

或为一般情况重写:

代码语言:javascript
复制
def print_info(player_name: str, player_hand: List[int]):
    print(f"{player_name}'s hand: {*player_hand}")
    print(f"{player_name}'s score: {get_score(player_hand)}")
代码语言:javascript
复制
def get_score(hand):
    score = 0
    for i in range(len(hand)):
        score += hand[i]

    return score

这可以重写为

代码语言:javascript
复制
def get_score(hand: List[int]):
    return sum(hand)

实际上,您实际上并不需要这样的函数,在代码中使用sum(hand)很好地传达了这个意思。

ask_for_new_card()要求用户输入并检查它是y还是n。它从来没有告诉球员,这些是唯一的选择。例如,如果键入no,程序就会退出。

您需要说明其他可能的输入。例如,再次询问用户是否没有输入其中一个选项。您还可以尝试将输入更改为标准表单。例如,对于以y开头的任何单词,这一行将返回"Y"

response = input()[0].upper()

代码语言:javascript
复制
def check_for_ace(card, hand):
    if get_score(hand) + card > 21:
        card = 1
    return card

这并不能真正检查这张卡是否是王牌。因为你每次向别人的手上加一张卡片,你都不可能输掉:

代码语言:javascript
复制
user_hand [7, 7] your score = 14
Do you want another card ?: y
user_hand [7, 7, 1] your score = 15
Do you want another card ?: y
user_hand [7, 7, 1, 1] your score = 16
Do you want another card ?: y
user_hand [7, 7, 1, 1, 1] your score = 17
Do you want another card ?: y
user_hand [7, 7, 1, 1, 1, 1] your score = 18
Do you want another card ?: y
user_hand [7, 7, 1, 1, 1, 1, 1] your score = 19
Do you want another card ?: y
user_hand [7, 7, 1, 1, 1, 1, 1, 1] your score = 20
Do you want another card ?: y
You Win
user_hand [7, 7, 1, 1, 1, 1, 1, 1, 1] your score = 21

请测试您的代码,然后再张贴在这里。

票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/280699

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档