斗地主python代码
时间: 2023-10-12 12:13:09 浏览: 58
以下是一个简单的斗地主Python代码示例:
```python
import random
# 生成一副牌
def generate_deck():
deck = []
for i in range(3, 16):
for j in range(4):
deck.append(i)
deck.extend([16, 17])
return deck
# 洗牌
def shuffle_deck(deck):
random.shuffle(deck)
# 发牌
def deal_cards(deck):
player1 = deck[:17]
player2 = deck[17:34]
player3 = deck[34:51]
remain = deck[51:]
return player1, player2, player3, remain
# 打印牌面
def print_cards(cards):
card_strs = []
for card in cards:
if card == 16:
card_strs.append('小王')
elif card == 17:
card_strs.append('大王')
else:
card_strs.append(str(card))
return ' '.join(card_strs)
# 判断牌型
def get_cards_type(cards):
cards_type = ''
if len(cards) == 1:
cards_type = '单牌'
elif len(cards) == 2 and cards[0] == cards[1]:
cards_type = '一对'
elif len(cards) == 3 and cards[0] == cards[1] == cards[2]:
cards_type = '三张'
elif len(cards) == 4 and cards[0] == cards[1] == cards[2] == cards[3]:
cards_type = '炸弹'
elif len(cards) == 5:
sorted_cards = sorted(cards)
if sorted_cards[0] == sorted_cards[1] == sorted_cards[2] and sorted_cards[3] == sorted_cards[4]:
cards_type = '三带二'
elif sorted_cards[0] == sorted_cards[1] and sorted_cards[2] == sorted_cards[3] == sorted_cards[4]:
cards_type = '二带三'
elif sorted_cards[0] + 1 == sorted_cards[1] and sorted_cards[1] + 1 == sorted_cards[2] and sorted_cards[2] + 1 == sorted_cards[3] and sorted_cards[3] + 1 == sorted_cards[4]:
cards_type = '顺子'
return cards_type
# 主函数
def main():
# 生成牌,洗牌,发牌
deck = generate_deck()
shuffle_deck(deck)
player1, player2, player3, remain = deal_cards(deck)
# 打印牌面
print('玩家1:', print_cards(player1))
print('玩家2:', print_cards(player2))
print('玩家3:', print_cards(player3))
print('底牌:', print_cards(remain))
# 判断牌型
print('玩家1牌型:', get_cards_type(player1))
print('玩家2牌型:', get_cards_type(player2))
print('玩家3牌型:', get_cards_type(player3))
if __name__ == '__main__':
main()
```
这个代码实现了一个简单的斗地主游戏,包括生成牌、洗牌、发牌、打印牌面和判断牌型等功能。但是这个代码只是一个示例,如果要实现一个完整的斗地主游戏,还需要更多的代码和逻辑。
阅读全文