|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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!]
[小甲鱼]拿到的牌是:🌙 ♥3 ♣8 ♠Q ♥8 ♠6 ♣2 ♦10 ♠9 ♥7 ♠7 ♦8 ♠K ♣9 ♠3 ♣3 ♠A
[不二如是]拿到的牌是:♥6 ♦6 ♠10 ♦Q ☀ ♣4 ♦4 ♣10 ♦7 ♣J ♣Q ♥Q ♦9 ♣7 ♠5 ♥J ♠4 ♦3 ♣6 ♦A
[鱼C小狮美]拿到的牌是:♥10 ♥5 ♥4 ♣A ♥2 ♥K ♦5 ♠2 ♥9 ♠8 ♣5 ♦J ♠J ♦K ♦2 ♥A ♣K
对🎴:
[小甲鱼]拿到的牌是:🌙 ♣2 ♠A ♠K ♠Q ♦10 ♣9 ♠9 ♦8 ♥8 ♣8 ♠7 ♥7 ♠6 ♣3 ♠3 ♥3
[不二如是]拿到的牌是:☀ ♦A ♥Q ♣Q ♦Q ♥J ♣J ♣10 ♠10 ♦9 ♣7 ♦7 ♣6 ♦6 ♥6 ♠5 ♠4 ♦4 ♣4 ♦3
[鱼C小狮美]拿到的牌是:♦2 ♠2 ♥2 ♥A ♣A ♣K ♦K ♥K ♠J ♦J ♥10 ♥9 ♠8 ♣5 ♦5 ♥5 ♥4
源代码:
import random
cards = ["♦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", "♠3", "♠4", "♠5", "♠6", "♠7", "♠8", "♠9", "♠10", "♠J", "♠Q", "♠K",
"☀", "🌙"]
cardsvalues = {"♦A":14, "♦2":15, "♦3":3, "♦4":4, "♦5":5, "♦6":6, "♦7":7, "♦8":8, "♦9":9, "♦10":10, "♦J":11, "♦Q":12, "♦K":13,
"♥A":14, "♥2":15, "♥3":3, "♥4":4, "♥5":5, "♥6":6, "♥7":7, "♥8":8, "♥9":9, "♥10":10, "♥J":11, "♥Q":12, "♥K":13,
"♣A":14, "♣2":15, "♣3":3, "♣4":4, "♣5":5, "♣6":6, "♣7":7, "♣8":8, "♣9":9, "♣10":10, "♣J":11, "♣Q":12, "♣K":13,
"♠A":14, "♠2":15, "♠3":3, "♠4":4, "♠5":5, "♠6":6, "♠7":7, "♠8":8, "♠9":9, "♠10":10, "♠J":11, "♠Q":12, "♠K":13,
"☀":17, "🌙":16}
def show_cards():
print('------斗👲地🎴主👲生🎴成👲器------')
print('明牌:')
all_cards = ["♦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",
"🌙", "☀"]
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([['🎴']*17 for i in range(3)])
print('\n地主牌:',['🎴' 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对🎴:')
print(f"[{a}]拿到的牌是:{' '.join(r[a])}\n")
print(f"[{b}]拿到的牌是:{' '.join(r[b])}\n")
print(f"[{c}]拿到的牌是:{' '.join(r[c])}")
show_cards()
dealCards()
|
|