用python写一个麻将游戏
时间: 2023-07-18 22:35:02 浏览: 190
好的,我可以给你提供一个非常简单的麻将游戏的代码,但它只包含基本的游戏逻辑,没有图形界面和音效。你可以根据自己的需求进行修改和完善。
```python
import random
class Mahjong:
def __init__(self):
self.tiles = []
self.discards = []
self.players = []
self.current_player = 0
self.current_tile = None
def create_tiles(self):
for i in range(1, 10):
for j in range(4):
self.tiles.append(str(i) + "萬")
self.tiles.append(str(i) + "索")
self.tiles.append(str(i) + "筒")
random.shuffle(self.tiles)
def init_players(self, num):
for i in range(num):
player = {"hand": [], "melds": [], "win": False}
for j in range(13):
tile = self.tiles.pop()
player["hand"].append(tile)
self.players.append(player)
def print_tiles(self, tiles):
for tile in tiles:
print(tile, end=" ")
print()
def print_game(self):
print("Current player: Player", self.current_player + 1)
print("Current tile: ", self.current_tile)
print("Discards: ", end="")
self.print_tiles(self.discards)
for i, player in enumerate(self.players):
print("Player", i + 1, "hand: ", end="")
self.print_tiles(player["hand"])
print("Melds: ", end="")
self.print_tiles(player["melds"])
print("Win: ", player["win"])
print()
def draw_tile(self):
if len(self.tiles) == 0:
return None
else:
return self.tiles.pop()
def discard_tile(self, tile):
self.current_tile = tile
self.discards.append(tile)
self.players[self.current_player]["hand"].remove(tile)
def next_player(self):
self.current_player = (self.current_player + 1) % len(self.players)
def is_chow(self, tiles):
if len(tiles) < 3:
return False
tiles.sort()
if tiles[0][:-1] != tiles[1][:-1] or tiles[1][:-1] != tiles[2][:-1]:
return False
if int(tiles[0][-1]) + 1 == int(tiles[1][-1]) and int(tiles[1][-1]) + 1 == int(tiles[2][-1]):
return True
if int(tiles[0][-1]) + 2 == int(tiles[1][-1]) and int(tiles[1][-1]) + 1 == int(tiles[2][-1]):
return True
if int(tiles[0][-1]) + 1 == int(tiles[1][-1]) and int(tiles[1][-1]) + 2 == int(tiles[2][-1]):
return True
return False
def is_pong(self, tiles):
if len(tiles) != 3:
return False
if tiles[0][:-1] == tiles[1][:-1] and tiles[1][:-1] == tiles[2][:-1]:
return True
return False
def is_kong(self, tiles):
if len(tiles) != 4:
return False
if tiles[0][:-1] == tiles[1][:-1] and tiles[1][:-1] == tiles[2][:-1] and tiles[2][:-1] == tiles[3][:-1]:
return True
return False
def is_win(self, hand, melds):
if len(hand) != 14:
return False
if len(melds) != 4:
return False
# TODO: add win checking logic
return False
def play_game(self):
self.create_tiles()
self.init_players(4)
self.print_game()
while True:
player = self.players[self.current_player]
tile = self.draw_tile()
if tile is None:
break
player["hand"].append(tile)
self.print_game()
while True:
action = input("Enter action (d=discard, c=chow, p=pong, k=kong, w=win): ")
if action == "d":
self.discard_tile(player["hand"][0])
self.next_player()
break
elif action == "c":
tiles = input("Enter tiles to chow (e.g. 1萬 2萬 3萬): ").split()
if self.is_chow(tiles):
player["melds"].append(tiles)
for tile in tiles:
player["hand"].remove(tile)
self.next_player()
break
else:
print("Invalid chow")
elif action == "p":
tiles = input("Enter tiles to pong (e.g. 1萬 1萬 1萬): ").split()
if self.is_pong(tiles):
player["melds"].append(tiles)
for tile in tiles:
player["hand"].remove(tile)
self.next_player()
break
else:
print("Invalid pong")
elif action == "k":
tiles = input("Enter tiles to kong (e.g. 1萬 1萬 1萬 1萬): ").split()
if self.is_kong(tiles):
player["melds"].append(tiles)
for tile in tiles:
player["hand"].remove(tile)
self.next_player()
break
else:
print("Invalid kong")
elif action == "w":
if self.is_win(player["hand"], player["melds"]):
player["win"] = True
break
else:
print("Not a winning hand")
else:
print("Invalid action")
print("Game over")
```
这个代码实现了一个基本的四人麻将游戏,可以进行摸牌、出牌、吃牌、碰牌、杠牌和胡牌等基本操作。但是需要注意的是,这个代码只是一个非常简单的实现,还有很多需要改进的地方,例如胡牌判断、摸牌逻辑等等。
阅读全文