BasicGames Python 源码解析 01 AceyDucey

本文涉及的产品
全局流量管理 GTM,标准版 1个月
云解析 DNS,旗舰版 1个月
公共DNS(含HTTPDNS解析),每月1000万次HTTP解析
简介: BasicGames Python 源码解析 01 AceyDucey

导入

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()
相关文章
|
3天前
|
XML 前端开发 数据格式
Beautiful Soup 解析html | python小知识
在数据驱动的时代,网页数据是非常宝贵的资源。很多时候我们需要从网页上提取数据,进行分析和处理。Beautiful Soup 是一个非常流行的 Python 库,可以帮助我们轻松地解析和提取网页中的数据。本文将详细介绍 Beautiful Soup 的基础知识和常用操作,帮助初学者快速入门和精通这一强大的工具。【10月更文挑战第11天】
18 2
|
3天前
|
数据安全/隐私保护 流计算 开发者
python知识点100篇系列(18)-解析m3u8文件的下载视频
【10月更文挑战第6天】m3u8是苹果公司推出的一种视频播放标准,采用UTF-8编码,主要用于记录视频的网络地址。HLS(Http Live Streaming)是苹果公司提出的一种基于HTTP的流媒体传输协议,通过m3u8索引文件按序访问ts文件,实现音视频播放。本文介绍了如何通过浏览器找到m3u8文件,解析m3u8文件获取ts文件地址,下载ts文件并解密(如有必要),最后使用ffmpeg合并ts文件为mp4文件。
|
6天前
|
Web App开发 SQL 数据库
使用 Python 解析火狐浏览器的 SQLite3 数据库
本文介绍如何使用 Python 解析火狐浏览器的 SQLite3 数据库,包括书签、历史记录和下载记录等。通过安装 Python 和 SQLite3,定位火狐数据库文件路径,编写 Python 脚本连接数据库并执行 SQL 查询,最终输出最近访问的网站历史记录。
17 4
|
7天前
|
机器学习/深度学习 算法 Python
深度解析机器学习中过拟合与欠拟合现象:理解模型偏差背后的原因及其解决方案,附带Python示例代码助你轻松掌握平衡技巧
【10月更文挑战第10天】机器学习模型旨在从数据中学习规律并预测新数据。训练过程中常遇过拟合和欠拟合问题。过拟合指模型在训练集上表现优异但泛化能力差,欠拟合则指模型未能充分学习数据规律,两者均影响模型效果。解决方法包括正则化、增加训练数据和特征选择等。示例代码展示了如何使用Python和Scikit-learn进行线性回归建模,并观察不同情况下的表现。
69 3
|
10天前
|
缓存 Java 程序员
Map - LinkedHashSet&Map源码解析
Map - LinkedHashSet&Map源码解析
26 0
|
10天前
|
算法 Java 容器
Map - HashSet & HashMap 源码解析
Map - HashSet & HashMap 源码解析
25 0
|
10天前
|
存储 Java C++
Collection-PriorityQueue源码解析
Collection-PriorityQueue源码解析
21 0
|
10天前
|
安全 Java 程序员
Collection-Stack&Queue源码解析
Collection-Stack&Queue源码解析
24 0
|
8天前
|
存储
让星星⭐月亮告诉你,HashMap的put方法源码解析及其中两种会触发扩容的场景(足够详尽,有问题欢迎指正~)
`HashMap`的`put`方法通过调用`putVal`实现,主要涉及两个场景下的扩容操作:1. 初始化时,链表数组的初始容量设为16,阈值设为12;2. 当存储的元素个数超过阈值时,链表数组的容量和阈值均翻倍。`putVal`方法处理键值对的插入,包括链表和红黑树的转换,确保高效的数据存取。
30 5
|
10天前
|
Java Spring
Spring底层架构源码解析(三)
Spring底层架构源码解析(三)

推荐镜像

更多