前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >BasicGames Python 源码解析 01 AceyDucey

BasicGames Python 源码解析 01 AceyDucey

作者头像
ApacheCN_飞龙
发布2022-04-02 08:59:17
1510
发布2022-04-02 08:59:17
举报
文章被收录于专栏:信数据得永生信数据得永生

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

导入

代码语言:javascript
复制
import random

cards

代码语言:javascript
复制
# 定义卡牌面值和名称的映射
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()

代码语言:javascript
复制
# 获取玩家输入的赌金
# 保证它是正数,并且小于等于可用资金
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()

代码语言:javascript
复制
# 无放回抽三张牌,保证第一张小于第二张
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()

代码语言:javascript
复制
# 游戏的主要逻辑
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()

代码语言:javascript
复制
# 程序入口
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()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 导入
  • cards
  • get_user_bet()
  • draw_3cards()
  • play_game()
  • main()
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档