Files
mjAi/src/engine/mahjong/meld.py
2024-12-01 22:14:23 +08:00

37 lines
1.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from src.engine.mahjong.mahjong_tile import MahjongTile
class Meld:
def __init__(self, tile, type: str):
"""
初始化一个碰或杠的对象。
:param tile: MahjongTile 对象,表示碰或杠的牌。
:param type: 字符串,'''',表示碰或杠。
"""
if not isinstance(tile, MahjongTile):
raise TypeError("tile 必须是 MahjongTile 类型")
if type not in ['', '']:
raise ValueError("type 必须是 ''''")
self.tile = tile
self.type = type
self.count = 3 if type == '' else 4 # 碰为3张杠为4张
def __repr__(self):
return f"({self.type}: {self.tile} x{self.count})"
def __eq__(self, other):
if not isinstance(other, Meld):
return False
return self.tile == other.tile and self.type == other.type
def __hash__(self):
return hash((self.tile, self.type))
def is_triplet(self):
"""是否为碰"""
return self.type == ''
def is_kong(self):
"""是否为杠"""
return self.type == ''