3个人用一副扑克牌打争上游,已知扑克牌有红桃,黑桃,方片,梅花4种花色,每个花色又有1~13这13个数字,其中1用A表示,11用J表示,12用Q表示,13用K表示。编写python程序,模拟3个人摸到
时间: 2024-10-28 18:11:47 浏览: 9
你可以使用Python的random模块来随机生成每个人抽取的扑克牌。这里是一个简单的示例,它会先创建一副完整的扑克牌,然后随机分配给三个玩家:
```python
import random
# 创建扑克牌列表
suits = ['红桃', '黑桃', '方片', '梅花']
ranks = [str(i) for i in range(2, 15)] + ['A', 'J', 'Q', 'K']
deck = [(suit, rank) for suit in suits for rank in ranks]
def deal_cards(num_players, deck):
if num_players > len(deck):
raise ValueError("玩家数超过牌的数量")
# 确保每人得到相同的牌数
cards_per_player = len(deck) // num_players
remainder = len(deck) % num_players
players = [[] for _ in range(num_players)]
# 分配剩余的牌
for player, extra_cards in enumerate(range(remainder)):
players[player].extend(random.sample(deck, extra_cards+1))
# 平均分配基本牌
for player in range(num_players):
while players[player]:
card = random.choice(deck)
deck.remove(card)
players[player].append(card)
return players
# 模拟3人玩争上游
players = deal_cards(3, deck)
for player, cards in enumerate(players):
print(f"玩家{player+1}的牌:", cards)
#
阅读全文