python 建立一个简单的斗地主小游戏
时间: 2023-07-01 08:07:48 浏览: 108
好的,以下是一个简单的斗地主小游戏的代码示例:
```python
import random
# 定义牌面值
poker_dict = {
0: "大王",
1: "小王",
2: "2",
3: "3",
4: "4",
5: "5",
6: "6",
7: "7",
8: "8",
9: "9",
10: "10",
11: "J",
12: "Q",
13: "K",
}
# 定义牌组
poker_list = []
for i in range(54):
if i == 52:
poker_list.append(0)
elif i == 53:
poker_list.append(1)
else:
poker_list.append(i % 13 + 2)
random.shuffle(poker_list)
# 定义玩家
player1 = poker_list[0:17]
player2 = poker_list[17:34]
player3 = poker_list[34:51]
three_cards = poker_list[51:54]
# 排序函数
def sort_poker(poker):
return sorted(poker, key=lambda x: (x % 13, x // 13))
# 打印函数
def print_poker(poker):
for i in range(len(poker)):
print(poker_dict[poker[i]], end=" ")
if i % 13 == 12:
print("")
print("")
# 输出每个玩家的牌
print("玩家1的牌:")
print_poker(sort_poker(player1))
print("玩家2的牌:")
print_poker(sort_poker(player2))
print("玩家3的牌:")
print_poker(sort_poker(player3))
print("三张底牌:")
print_poker(sort_poker(three_cards))
```
运行以上代码,即可输出每个玩家的牌和三张底牌。你可以在此基础上进行扩展,实现更完整的斗地主游戏。
阅读全文