python 玩 21点游戏 (自己玩自己不好玩)

# -*- coding: UTF-8 -*-
import random




# 1
def ask_user(prompt, response1, response2):
    """
    Ask user for a response in two responses.
    prompt (str) - The prompt to print to the user
    response1 (str) - One possible response that the user can give
    response2 (str) - Another possible response that the user can give
    """


    while True:
        # ask user for response
        user_response = input(prompt)


        # if response is response1 or response2 then return
        if user_response == response1 or user_response == response2:
            return user_response




# 2
def print_card(name, num):
    """
    print "______ draws a _____" with the first blank replaced by the user's
    name and the second blank replaced by the value given to the function.
    name (str) - the user name to print out
    num (int) - the number to print out
    """


    # if the value is a 1, 11, 12, 13, print Ace, Jack, Queen, King
    if num == 1:
        num = "Ace"
    elif num == 11:
        num = "Jack"
    elif num == 12:
        num = "Queen"
    elif num == 13:
        num = "King"
    else:
        num = str(num)


    # print the string
    print(name, "draws a", num)




# 3
def get_ace_value():
    """
    Ask the user if they want to use a 1 or 11 as their Ace value.
    """


    # get the value use "ask_user" function
    value = ask_user("Should the Ace be 1 or 11?", "1", "11")


    # retrun the value
    return int(value)




# 4
def deal_card(name):
    """
    Pick a random number between 1 and 13, and print out what the user drew.
    name (str) - the user name to print out
    """


    # get a random number in range 1 to 13
    num = random.randrange(1, 13 + 1)


    # use "print_card" function to print out what the user drew
    print_card(name, num)


    if num > 10:
        # if the card is a Jack, Queen, or King, the point-value is 10
        return 10
    elif num == 1:
        # If the card is an Ace, ask the user if the value should be 1 or 11
        return get_ace_value()
    else:
        return num




# 5
def adjust_for_bust(num):
    """
    If the given number is greater than 21, print "Bust!" and return -1.
    Otherwise return the number that was passed in.
    num (int) - the given number
    """


    # determine the value of num
    if num > 21:
        print("Bust!")
        return -1
    else:
        return num




# 6
def hit_or_stay(num):
    """
    Prompt the user hit or stay and return user's chose.
    num (int) - the value of a player's card hand
    """


    if num <= 21:


        #chose = ask_user("Hit or stay?", "hit", "stay")
        #hit 要牌
        chose = ask_user("还要不要牌?", "hit", "stay")
        # if num less than 21 and user chose hit return True
        if chose == "hit":
            return True


    # otherwise return False
    return False




# 7
def play_turn(name):
    """
    Play whole the trun for a user.
    name (str) - the player's name
    """


    # print out that it's the current players turn
    print("==========[ Current player:", name, "]==========")


    # set total score zero
    total_score = 0


    # deal the player a card for loop
    while True:


        # get total score
        total_score += deal_card(name)


        # if not busted print out the player's total score
        print("Total:", total_score)


        # if player chose stay return the result, otherwise continue the loop
        if not hit_or_stay(total_score):
            return adjust_for_bust(total_score)




# 8
def determine_winner(name1, name2, score1, score2):
    """
    Determine_the game's winner.
    name1 (str) - the first player's name
    name2 (str) - the second player's name
    score1 (str) - the first player's score
    score2 (str) - the second player's score
    """


    if score1 == score2:
        print(name1, "and", name2, "平局!")
    elif score1 > score2:
        print(name1, "wins!")
    elif score1 < score2:
        print(name2, "wins!")




# 9
def main():
    """
    The main program of BlackJack game
    """


    while True:


        # Ask each player for their name
        name1 = input("Player 1 name:")
        name2 = input("Player 2 name:")


        # Greet them
        print("Welcome to BlackJack", name1, "and", name2)
        print()


        # Let the first player play a turn
        score1 = play_turn(name1)
        print()


        # Let the second player play a turn
        score2 = play_turn(name2)
        print()


        # Determine who won
        determine_winner(name1, name2, score1, score2)


        # Play again if they say yes and end the loop if they say no
        if ask_user("Would you like to play again?", "yes", "no") == "no":
            break




if __name__ == "__main__":
    main()
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值