Python写命令交互的斗地主游戏,包括玩家昵称、入桌、洗牌、发牌、叫分、拿底牌,斗地主出牌规则,记录得分
时间: 2023-10-20 14:10:47 浏览: 137
python实现斗地主分牌洗牌
好的,这是一个比较复杂的项目,需要比较多的时间和代码来完成,我会尽可能详细地介绍每个步骤的实现方法。
首先,我们需要定义一些基本的数据结构和变量,包括玩家信息、牌库、每个玩家手中的牌、底牌、当前叫分、当前出牌等等。这些变量可以定义为全局变量,方便在各个函数中使用。
接下来,我们需要实现各个功能函数,包括:
1. 玩家昵称的输入和保存:可以通过 input 函数获取玩家输入的昵称,然后将其保存在一个列表中。
```python
players = []
for i in range(3):
name = input("请输入玩家%d的昵称:" % (i+1))
players.append({"name": name, "cards": []})
```
2. 入桌和洗牌:可以将所有牌按照顺序放入一个列表中,然后使用 random.shuffle 函数进行洗牌。
```python
cards = []
for i in range(1, 14):
for j in range(4):
cards.append("%d%s" % (i, ["♠", "♥", "♣", "♦"][j]))
random.shuffle(cards)
```
3. 发牌:可以将洗好的牌平均分给每个玩家,并将剩余的三张牌作为底牌。
```python
for i in range(3):
players[i]["cards"] = cards[i*17:i*17+17]
bottom = cards[51:]
```
4. 叫分:可以使用 input 函数获取玩家输入的叫分,如果当前玩家不叫分,则下一个玩家继续叫分,直到有玩家叫分为止。
```python
score = 0
for i in range(3):
while True:
call = input("%s,请输入叫分(0-3):" % players[i]["name"])
if call.isdigit() and 0 <= int(call) <= 3:
call = int(call)
if call > score:
score = call
break
print("输入有误,请重新输入!")
```
5. 拿底牌:可以将底牌加入当前玩家手中的牌中,并将底牌清空。
```python
players[(i+1)%3]["cards"].extend(bottom)
bottom = []
```
6. 斗地主出牌规则:可以定义一个函数来判断当前出牌是否符合规则,包括牌型、大小等等。具体实现方法可以参考以下代码:
```python
def check(cards):
if not cards:
return False
if len(cards) == 1:
return True
if len(cards) == 2 and cards[0][:-1] == cards[1][:-1]:
return True
if len(cards) == 3 and cards[0][:-1] == cards[1][:-1] == cards[2][:-1]:
return True
if len(cards) == 4 and cards[0][:-1] == cards[1][:-1] == cards[2][:-1] == cards[3][:-1]:
return True
if len(cards) == 5:
values = [int(card[:-1]) for card in cards]
if len(set(values)) == 5 and max(values) - min(values) == 4:
return True
return False
```
7. 记录得分:可以使用一个字典来记录每个玩家的得分情况。
```python
scores = {"地主": 0, "农民": 0}
if i == landlord:
scores["地主"] += score * 100
else:
scores["农民"] += score * 50
```
最后,我们可以将以上函数整合成一个完整的游戏程序,具体实现方法可以参考以下代码:
```python
import random
def check(cards):
if not cards:
return False
if len(cards) == 1:
return True
if len(cards) == 2 and cards[0][:-1] == cards[1][:-1]:
return True
if len(cards) == 3 and cards[0][:-1] == cards[1][:-1] == cards[2][:-1]:
return True
if len(cards) == 4 and cards[0][:-1] == cards[1][:-1] == cards[2][:-1] == cards[3][:-1]:
return True
if len(cards) == 5:
values = [int(card[:-1]) for card in cards]
if len(set(values)) == 5 and max(values) - min(values) == 4:
return True
return False
players = []
for i in range(3):
name = input("请输入玩家%d的昵称:" % (i+1))
players.append({"name": name, "cards": []})
cards = []
for i in range(1, 14):
for j in range(4):
cards.append("%d%s" % (i, ["♠", "♥", "♣", "♦"][j]))
random.shuffle(cards)
for i in range(3):
players[i]["cards"] = cards[i*17:i*17+17]
bottom = cards[51:]
score = 0
for i in range(3):
while True:
call = input("%s,请输入叫分(0-3):" % players[i]["name"])
if call.isdigit() and 0 <= int(call) <= 3:
call = int(call)
if call > score:
score = call
break
print("输入有误,请重新输入!")
landlord = -1
for i in range(3):
if players[i]["cards"] == sorted(players[i]["cards"]):
landlord = i
break
print("%s成为了地主!" % players[landlord]["name"])
players[landlord]["cards"].extend(bottom)
bottom = []
current = landlord
last_cards = []
while True:
print("%s,轮到你出牌了:" % players[current]["name"])
print("你的手牌是:%s" % " ".join(players[current]["cards"]))
print("底牌是:%s" % " ".join(bottom))
cards = input("请输入你要出的牌(以空格分隔):").split()
if cards == ["pass"]:
last_cards = []
print("你选择了不出!")
elif check(cards) and (not last_cards or (len(cards) == len(last_cards) and cards[0][:-1] > last_cards[0][:-1])):
players[current]["cards"] = [card for card in players[current]["cards"] if card not in cards]
last_cards = cards
print("出牌成功!")
if not players[current]["cards"]:
break
else:
print("出牌有误,请重新选择!")
continue
current = (current + 1) % 3
scores = {"地主": 0, "农民": 0}
if current == landlord:
scores["地主"] += score * 100
else:
scores["农民"] += score * 50
for i in range(3):
if i == landlord:
continue
if not players[i]["cards"]:
scores["地主"] += 50
else:
scores["农民"] += len(players[i]["cards"])
print("游戏结束!")
for i in range(3):
print("%s得分:%d" % (players[i]["name"], scores["地主"] if i == landlord else scores["农民"]))
```
这样,一个简单的命令行斗地主游戏就完成了。当然,这只是一个最基本的实现方法,还有很多可以优化和改进的地方,比如加入 GUI 界面、增加 AI 玩家等等。
阅读全文