• BasicGames Python 源码解析 01 AceyDucey


    阅读更多:apachecn/python-code-anal

    导入

    import random
    

    cards

    # 定义卡牌面值和名称的映射
    cards = {
        1: "1",
        2: "2",
        3: "3",
        4: "4",
        5: "5",
        6: "6",
        7: "7",
        8: "8",
        9: "9",
        10: "Jack",
        11: "Queen",
        12: "King",
        13: "Ace",
    }
    

    get_user_bet()

    # 获取玩家输入的赌金
    # 保证它是正数,并且小于等于可用资金
    def get_user_bet(cash):
        while True:
            try:
                bet = int(input("What is your bet? "))
                if bet < 0:
                    print("Bet must be more than zero")
                elif bet == 0:
                    print("CHICKEN!!\n")
                elif bet > cash:
                    print("Sorry, my friend but you bet too much")
                    print(f"You only have {cash} dollars to bet")
                else:
                    return bet
            except ValueError:
                print("Please enter a positive number")
    

    draw_3cards()

    # 无放回抽三张牌,保证第一张小于第二张
    def draw_3cards():
        round_cards = list(cards.keys())
        random.shuffle(round_cards)
        card_a, card_b, card_c = round_cards.pop(), round_cards.pop(), round_cards.pop()
        if card_a > card_b:
            card_a, card_b = card_b, card_a
        return (card_a, card_b, card_c)
    

    play_game()

    # 游戏的主要逻辑
    def play_game():
        """Play the game"""
        cash = 100
        while cash > 0:
            print(f"You now have {cash} dollars\n")
            print("Here are you next two cards")
            # 抽三张牌,展示前两张
            card_a, card_b, card_c = draw_3cards()
            print(f" {cards[card_a]}")
            print(f" {cards[card_b]}\n")
            # 玩家猜测第三张是否在前两张之间,并输入赌金
            bet = get_user_bet(cash)
            # 扣掉赌金,展示第三张
            cash -= bet
            print(f" {cards[card_c]}")
            # 检查猜测结果
            # 如果猜测正确,返还双倍赌金,否则什么也不做
            if card_a < card_c < card_b:
                print("You win!!!")
                cash += bet * 2
            else:
                print("Sorry, you lose")
    
        # 可用资金为 0,就结束游戏
        print("Sorry, friend, but you blew your wad")
    

    main()

    # 程序入口
    def main():
        # 首先打印游戏介绍
        print("""
    Acey-Ducey is played in the following manner
    The dealer (computer) deals two cards face up
    You have an option to be or not bet depending
    on whether or not you feel the card will have
    a value between the first two.
    If you do not want to bet, input a 0
        """)
        while True:
            # 在循环中开始游戏
            play_game()
            # 游戏结束之后,询问玩家是否继续,不继续就跳出循环
            keep_playing = input("Try again? (yes or no) ").lower() in ["yes", "y"]
            if not keep_playing: break
        print("Ok hope you had fun")
    
    
    if __name__ == "__main__": main()
    
  • 相关阅读:
    《贝叶斯优化: 一种更好的超参数调优方式》
    《从Google Visor到Microsoft NNI再到Advisor调参服务接口发展史》
    《归一化激活层的进化:谷歌Quoc Le等人利用AutoML 技术发现新型ML模块》
    《通用视觉 & NAS》
    《Object Detection-IOU Net笔记》
    《Detectron2之ROIAlign》
    《IoUNet(6)_源码_RoIAlign(1)》
    聊天机器人资源合集:项目,语聊,论文,教程。
    一个使用 Python 的人工智能聊天机器人框架
    【TensorFlow 官网 可以直接访问】让中国开发者更容易地使用TensorFlow打造人工智能应用
  • 原文地址:https://www.cnblogs.com/apachecn/p/15884029.html
Copyright © 2020-2023  润新知