用python写一个麻将游戏
时间: 2024-05-29 19:03:25 浏览: 209
好的,这里是一个简单的麻将游戏的代码实现,你可以根据自己的需求进行修改和优化:
```python
import random
class MahjongGame:
def __init__(self):
self.tiles = [] # 所有牌
self.players = [] # 玩家列表
self.current_player = None # 当前玩家
self.dora = None # 宝牌
self.init_tiles() # 初始化牌
self.init_players() # 初始化玩家
def init_tiles(self):
# 初始化牌
self.tiles = [
'1s', '2s', '3s', '4s', '5s', '6s', '7s', '8s', '9s', # 数牌
'1p', '2p', '3p', '4p', '5p', '6p', '7p', '8p', '9p', # 数牌
'1m', '2m', '3m', '4m', '5m', '6m', '7m', '8m', '9m', # 数牌
'1z', '2z', '3z', '4z', '5z', 6z', '7z', # 字牌
]
random.shuffle(self.tiles) # 打乱牌
def init_players(self):
# 初始化玩家
for i in range(4):
player = {
'name': 'Player{}'.format(i+1),
'tiles': [],
'is_ready': False,
'is_dealer': False,
'score': 0,
}
self.players.append(player)
# 分发初始牌
for i in range(4):
for j in range(13):
tile = self.tiles.pop()
self.players[i]['tiles'].append(tile)
# 随机一个玩家成为庄家
dealer_index = random.randint(0, 3)
self.players[dealer_index]['is_dealer'] = True
def start(self):
# 开始游戏
round_count = 0 # 记录局数
while round_count < 4: # 总共玩4局
round_count += 1
print('第{}局开始:'.format(round_count))
self.init_round() # 初始化本局
self.play_round() # 开始本局
self.end_round() # 结束本局
def init_round(self):
# 初始化本局
for player in self.players:
player['tiles'] = [] # 清空手牌
player['is_ready'] = False # 取消准备状态
# 分发初始牌
for i in range(4):
for j in range(13):
tile = self.tiles.pop()
self.players[i]['tiles'].append(tile)
# 随机一个玩家成为本局的起家
self.current_player = random.randint(0, 3)
def play_round(self):
# 开始本局
while True:
# 判断是否有人胡牌或流局
if self.check_win() or self.check_draw():
break
# 当前玩家出牌
self.play()
# 判断其他玩家是否可以吃、碰、杠
self.check_others()
# 切换到下一个玩家
self.next_player()
def end_round(self):
# 结束本局
for i in range(4):
player = self.players[i]
if player['is_ready']:
print('{}胡了!'.format(player['name']))
else:
print('{}流局!'.format(player['name']))
def play(self):
# 当前玩家出牌
player = self.players[self.current_player]
tile = input('{}请出牌:'.format(player['name']))
player['tiles'].remove(tile)
def check_others(self):
# 判断其他玩家是否可以吃、碰、杠
for i in range(1, 4):
player = self.players[(self.current_player+i) % 4]
if self.check_chi(player):
action = input('{}是否吃?(y/n)'.format(player['name']))
if action == 'y':
# TODO: 吃牌操作
pass
if self.check_peng(player):
action = input('{}是否碰?(y/n)'.format(player['name']))
if action == 'y':
# TODO: 碰牌操作
pass
if self.check_gang(player):
action = input('{}是否杠?(y/n)'.format(player['name']))
if action == 'y':
# TODO: 杠牌操作
pass
def next_player(self):
# 切换到下一个玩家
self.current_player = (self.current_player + 1) % 4
def check_win(self):
# 判断是否有人胡牌
for player in self.players:
if self.is_win(player['tiles']):
player['is_ready'] = True
return True
return False
def check_draw(self):
# 判断是否流局
for player in self.players:
if not self.is_draw(player['tiles']):
return False
return True
def check_chi(self, player):
# 判断是否可以吃牌
# TODO: 实现吃牌判断
return False
def check_peng(self, player):
# 判断是否可以碰牌
# TODO: 实现碰牌判断
return False
def check_gang(self, player):
# 判断是否可以杠牌
# TODO: 实现杠牌判断
return False
def is_win(self, tiles):
# 判断是否胡牌
# TODO: 实现胡牌判断
return False
def is_draw(self, tiles):
# 判断是否流局
if len(self.tiles) > 0:
return False
for tile in tiles:
if tile[-1] == 'z' or int(tile[:-1]) % 3 != 1:
return False
return True
if __name__ == '__main__':
game = MahjongGame()
game.start()
```
这个代码中并没有实现完整的麻将规则,只是提供了一个简单的框架,你可以根据需要自行添加和修改。
阅读全文