用python模拟斗地主发牌
时间: 2024-11-09 17:21:09 浏览: 14
在Python中模拟斗地主游戏的发牌过程,可以创建一副洗好的牌,并随机分配给三位玩家。这里是一个简单的示例:
```python
import random
# 定义扑克牌的一组牌
suits = ['♠', '♥', '♦', '♣']
ranks = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
# 创建一副完整的牌
deck = [(rank, suit) for rank in ranks for suit in suits]
# 洗牌函数
def shuffle_deck(deck):
random.shuffle(deck)
return deck
# 发牌函数,每个玩家分得17张牌
def deal_cards(deck, num_players=3):
cards_per_player = len(deck) // num_players
players_cards = [[] for _ in range(num_players)]
for i in range(cards_per_player * num_players):
player_index = i % num_players
players_cards[player_index].append(deck.pop())
return players_cards
# 示例:给三个玩家发牌
deck = shuffle_deck(deck)
player_cards = deal_cards(deck)
print("每位玩家的牌:")
for player, cards in enumerate(player_cards):
print(f"玩家{player + 1}: {cards}")
```
这个示例简单地展示了如何创建一副牌、洗牌以及平均分配给三个玩家。你可以根据需要调整发牌的数量,或者添加更多游戏规则。
阅读全文