Update chengdu_mahjong_engine.py

pull/1/head
wsy182 2024-11-30 13:41:00 +08:00
parent 7a36a5b861
commit 2292729566
1 changed files with 8 additions and 50 deletions

View File

@ -44,53 +44,11 @@ class ChengduMahjongEngine:
else:
raise ValueError("杠牌条件不满足")
# 设置缺门
def set_missing_suit(player, missing_suit):
# 确定玩家的缺门
valid_suits = ["", "", ""]
if missing_suit not in valid_suits:
raise ValueError("缺门设置无效")
player.missing_suit = missing_suit
# 判断是否可以流局
def check_blood_battle(self):
if len(self.winners) >= 3 or self.state.remaining_tiles == 0:
self.game_over = True
# 判断是否可以胡牌
def can_win(hand):
# 判断是否满足胡牌条件(四组+一对)
from collections import Counter
# 判断缺一门
suits = [get_suit(tile) for tile, count in enumerate(hand) if count > 0]
if len(set(suits)) > 2:
return False # 不满足缺一门
# 判断是否满足四组+一对
def is_valid_group(tiles):
# 判断是否为顺子、刻子或对子
return len(tiles) == 3 and (tiles[0] == tiles[1] == tiles[2] or
tiles[0] + 1 == tiles[1] and tiles[1] + 1 == tiles[2])
counter = Counter(hand)
pairs = [tile for tile, count in counter.items() if count >= 2]
for pair in pairs:
temp_hand = hand[:]
temp_hand[pair] -= 2
if is_valid_group(temp_hand):
return True
return False
# 计算番数
def calculate_fan(hand, melds, is_self_draw, is_cleared):
fan = 1 # 基本胡
if is_cleared:
fan += 2 # 清一色
if len(melds) >= 2:
fan += len(melds) # 根据杠加番
if is_self_draw:
fan += 1 # 自摸加番
return fan
def check_blood_battle(self):
"""
检查游戏是否流局或血战结束
如果满足结束条件设置 self.game_over True
"""
if len(self.state.winners) >= 3 or self.state.remaining_tiles == 0:
print(f"游戏结束,赢家列表: {self.state.winners}")
self.game_over = True