鱼C论坛

 找回密码
 立即注册
查看: 1060|回复: 2

[技术交流] Python 课后作业-发牌-斗地主生成器

[复制链接]
发表于 2022-11-16 11:43:45 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
两次课后作业让我想要皮一下,
请大家想象一下小甲鱼,不二如是和鱼C小狮美三人一起斗地主的和谐场景,
嘿嘿~画面太美,
我从观摩中练习了字典使用和洗牌算法,掌握了sorted()key参数与lambda表达式的‘联动’
------斗👲地🎴主👲生🎴成👲器------
明牌:
♦3 ♦4 ♦5 ♦6 ♦7 ♦8 ♦9 ♦10 ♦J ♦Q ♦K ♦A ♦2
♥3 ♥4 ♥5 ♥6 ♥7 ♥8 ♥9 ♥10 ♥J ♥Q ♥K ♥A ♥2
♣3 ♣4 ♣5 ♣6 ♣7 ♣8 ♣9 ♣10 ♣J ♣Q ♣K ♣A ♣2
♠3 ♠4 ♠5 ♠6 ♠7 ♠8 ♠9 ♠10 ♠J ♠Q ♠K ♠A ♠2
🌙 ☀

打乱:
[['🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴'],
['🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴'],
['🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴', '🎴']]

地主牌: ['🎴', '🎴', '🎴']
请输入第一位游戏玩家名称:小甲鱼
请输入第二位游戏玩家名称:不二如是
请输入第三位游戏玩家名称:鱼C小狮美

【不二如是】叫地主

发牌<( ̄︶ ̄)↗[GO!]
[小甲鱼]拿到的牌是:&#127769; &#9829;3 &#9827;8 &#9824;Q &#9829;8 &#9824;6 &#9827;2 &#9830;10 &#9824;9 &#9829;7 &#9824;7 &#9830;8 &#9824;K &#9827;9 &#9824;3 &#9827;3 &#9824;A

[不二如是]拿到的牌是:&#9829;6 &#9830;6 &#9824;10 &#9830;Q &#9728; &#9827;4 &#9830;4 &#9827;10 &#9830;7 &#9827;J &#9827;Q &#9829;Q &#9830;9 &#9827;7 &#9824;5 &#9829;J &#9824;4 &#9830;3 &#9827;6 &#9830;A

[鱼C小狮美]拿到的牌是:&#9829;10 &#9829;5 &#9829;4 &#9827;A &#9829;2 &#9829;K &#9830;5 &#9824;2 &#9829;9 &#9824;8 &#9827;5 &#9830;J &#9824;J &#9830;K &#9830;2 &#9829;A &#9827;K

对&#127924;:
[小甲鱼]拿到的牌是:&#127769; &#9827;2 &#9824;A &#9824;K &#9824;Q &#9830;10 &#9827;9 &#9824;9 &#9830;8 &#9829;8 &#9827;8 &#9824;7 &#9829;7 &#9824;6 &#9827;3 &#9824;3 &#9829;3

[不二如是]拿到的牌是:&#9728; &#9830;A &#9829;Q &#9827;Q &#9830;Q &#9829;J &#9827;J &#9827;10 &#9824;10 &#9830;9 &#9827;7 &#9830;7 &#9827;6 &#9830;6 &#9829;6 &#9824;5 &#9824;4 &#9830;4 &#9827;4 &#9830;3

[鱼C小狮美]拿到的牌是:&#9830;2 &#9824;2 &#9829;2 &#9829;A &#9827;A &#9827;K &#9830;K &#9829;K &#9824;J &#9830;J &#9829;10 &#9829;9 &#9824;8 &#9827;5 &#9830;5 &#9829;5 &#9829;4


源代码:
import random

cards = ["&#9830;A", "&#9830;2", "&#9830;3", "&#9830;4", "&#9830;5", "&#9830;6", "&#9830;7", "&#9830;8", "&#9830;9", "&#9830;10", "&#9830;J", "&#9830;Q", "&#9830;K",
         "&#9829;A", "&#9829;2", "&#9829;3", "&#9829;4", "&#9829;5", "&#9829;6", "&#9829;7", "&#9829;8", "&#9829;9", "&#9829;10", "&#9829;J", "&#9829;Q", "&#9829;K",
         "&#9827;A", "&#9827;2", "&#9827;3", "&#9827;4", "&#9827;5", "&#9827;6", "&#9827;7", "&#9827;8", "&#9827;9", "&#9827;10", "&#9827;J", "&#9827;Q", "&#9827;K",
         "&#9824;A", "&#9824;2", "&#9824;3", "&#9824;4", "&#9824;5", "&#9824;6", "&#9824;7", "&#9824;8", "&#9824;9", "&#9824;10", "&#9824;J", "&#9824;Q", "&#9824;K",
         "&#9728;", "&#127769;"]
cardsvalues = {"&#9830;A":14, "&#9830;2":15, "&#9830;3":3, "&#9830;4":4, "&#9830;5":5, "&#9830;6":6, "&#9830;7":7, "&#9830;8":8, "&#9830;9":9, "&#9830;10":10, "&#9830;J":11, "&#9830;Q":12, "&#9830;K":13,
               "&#9829;A":14, "&#9829;2":15, "&#9829;3":3, "&#9829;4":4, "&#9829;5":5, "&#9829;6":6, "&#9829;7":7, "&#9829;8":8, "&#9829;9":9, "&#9829;10":10, "&#9829;J":11, "&#9829;Q":12, "&#9829;K":13,
               "&#9827;A":14, "&#9827;2":15, "&#9827;3":3, "&#9827;4":4, "&#9827;5":5, "&#9827;6":6, "&#9827;7":7, "&#9827;8":8, "&#9827;9":9, "&#9827;10":10, "&#9827;J":11, "&#9827;Q":12, "&#9827;K":13,
               "&#9824;A":14, "&#9824;2":15, "&#9824;3":3, "&#9824;4":4, "&#9824;5":5, "&#9824;6":6, "&#9824;7":7, "&#9824;8":8, "&#9824;9":9, "&#9824;10":10, "&#9824;J":11, "&#9824;Q":12, "&#9824;K":13,
               "&#9728;":17, "&#127769;":16}

def show_cards():
    print('------斗&#128114;地&#127924;主&#128114;生&#127924;成&#128114;器------')
    print('明牌:')
    all_cards = ["&#9830;3", "&#9830;4", "&#9830;5", "&#9830;6", "&#9830;7", "&#9830;8", "&#9830;9", "&#9830;10", "&#9830;J", "&#9830;Q", "&#9830;K", "&#9830;A", "&#9830;2",
                 "&#9829;3", "&#9829;4", "&#9829;5", "&#9829;6", "&#9829;7", "&#9829;8", "&#9829;9", "&#9829;10", "&#9829;J", "&#9829;Q", "&#9829;K", "&#9829;A", "&#9829;2",
                 "&#9827;3", "&#9827;4", "&#9827;5", "&#9827;6", "&#9827;7", "&#9827;8", "&#9827;9", "&#9827;10", "&#9827;J", "&#9827;Q", "&#9827;K", "&#9827;A", "&#9827;2",
                 "&#9824;3", "&#9824;4", "&#9824;5", "&#9824;6", "&#9824;7", "&#9824;8", "&#9824;9", "&#9824;10", "&#9824;J", "&#9824;Q", "&#9824;K", "&#9824;A", "&#9824;2",
                 "&#127769;", "&#9728;"]

    all_cards = all_cards[::-1]

    for i in range(54):
        print(all_cards.pop(), end=' ')
        if (i+1) % 13 == 0:
            print()
    print()
    print('\n打乱:')
    print([['&#127924;']*17 for i in range(3)])
    print('\n地主牌:',['&#127924;' for i in range(3)])




def fy_shuffle(x, n=1):
    for i in range(n):
        target = list(x)
        result = []
        while target:
            r = random.randint(0, len(target)-1) # 步骤2
            result.append(target.pop(r)) # 步骤3

    return result

def dealCards():
    a = input("请输入第一位游戏玩家名称:")
    b = input("请输入第二位游戏玩家名称:")
    c = input("请输入第三位游戏玩家名称:")

    r = {}
    r[a], r[b], r[c] = [], [], []

    new_cards = fy_shuffle(cards, 3)

    for i in range(17):
        r[a].append(new_cards.pop())
        r[b].append(new_cards.pop())
        r[c].append(new_cards.pop())

    d = random.sample((a, b, c), 1)[0]
    print(f"\n【{d}】叫地主\n")
    print('发牌<( ̄︶ ̄)↗[GO!]')
    r[d].extend((new_cards.pop(), new_cards.pop(), new_cards.pop()))

    print(f"[{a}]拿到的牌是:{' '.join(r[a])}\n")
    print(f"[{b}]拿到的牌是:{' '.join(r[b])}\n")
    print(f"[{c}]拿到的牌是:{' '.join(r[c])}")

    r[a] = sorted(r[a],key=lambda x : cardsvalues[x])[::-1]
    r[b] = sorted(r[b],key=lambda x : cardsvalues[x])[::-1]
    r[c] = sorted(r[c],key=lambda x : cardsvalues[x])[::-1]

    print('\n对&#127924;:')
    print(f"[{a}]拿到的牌是:{' '.join(r[a])}\n")
    print(f"[{b}]拿到的牌是:{' '.join(r[b])}\n")
    print(f"[{c}]拿到的牌是:{' '.join(r[c])}")

show_cards()   
dealCards()



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-16 11:58:41 | 显示全部楼层
建议用代码格式发代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-17 09:29:41 | 显示全部楼层
嘉岳呀 发表于 2022-11-16 11:58
建议用代码格式发代码

第一次发没经验
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-15 15:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表