生成一副扑克牌,用T,J、Q、K、A分别代表10,Jack,Queen,King和Ace, R代表大王,B代表小王, 电脑随机将54张牌发给3名打牌人,在屏幕上显示每位牌手的牌;
时间: 2024-10-01 12:09:33 浏览: 47
好的,为了模拟这个场景,我们可以创建一个简单的Python程序,使用random库来随机分配扑克牌。这里我们假设每个玩家都会收到18张牌。首先,我们需要定义所有的牌和洗牌函数。
```python
import random
# 定义牌的种类和对应的点数
suits = ['♠️', '♥️', '♦️', '♣️']
ranks = ['T', 'J', 'Q', 'K', 'A', 'R', 'B'] + list(range(2, 11))
deck = [(rank, suit) for rank in ranks for suit in suits]
# 洗牌
def shuffle_deck(deck):
random.shuffle(deck)
return deck
# 分配牌
def deal_cards(deck, players, num_per_player=18):
cards_to_each_player = [num_per_player] * len(players)
remaining_cards = deck[:sum(cards_to_each_player)]
player_hands = {player: [] for player in players}
while remaining_cards:
for i, player in enumerate(players):
if cards_to_each_player[i]:
player_hands[player].append(random.choice(remaining_cards))
remaining_cards.remove(player_hands[player][-1])
cards_to_each_player[i] -= 1
return player_hands
# 示例玩家
players = ['Player1', 'Player2', 'Player3']
player_hands = deal_cards(deck, players)
# 在屏幕上显示每位牌手的牌
for player, hand in player_hands.items():
print(f"{player}的牌是:{hand}")
阅读全文