1
1
This commit is contained in:
37
src/engine/mahjong/meld.py
Normal file
37
src/engine/mahjong/meld.py
Normal file
@@ -0,0 +1,37 @@
|
||||
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 == '杠'
|
||||
Reference in New Issue
Block a user