Compare commits

..

2 Commits

Author SHA1 Message Date
0c8a2ff668 Update test_calculate_fan.py 2024-11-30 16:44:57 +08:00
8ad0177ee0 Update calculate_fan.py 2024-11-30 16:44:55 +08:00
2 changed files with 129 additions and 12 deletions

View File

@@ -16,17 +16,15 @@ def calculate_fan(hand, melds, is_self_draw, is_cleared, conditions):
# 定义番种规则
rules = {
"basic_win": lambda: 1 if not (conditions.get("is_seven_pairs", False) or
conditions.get("is_big_pairs", False) or
conditions.get("is_dragon_seven_pairs", False) or
conditions.get("is_pure_cleared", False) or
conditions.get("is_double_pure_cleared", False)) else 0,
# 基本胡:当清对或更高级规则生效时,屏蔽
# 清对:高级规则,优先处理
"is_pure_cleared": lambda: 3 if is_cleared and len(melds) == 3 and not conditions.get("is_double_pure_cleared", False) else 0,
# 极中极:高级规则
"is_double_pure_cleared": lambda: 4 if is_cleared and len(melds) >= 2 and conditions.get("is_double_pure_cleared", False) else 0,
# 清一色:基础规则,仅在高级规则未生效时计算
"is_cleared": lambda: 2 if is_cleared and not (conditions.get("is_pure_cleared", False) or
conditions.get("is_double_pure_cleared", False)) else 0,
"is_pure_cleared": lambda: 3 if is_cleared and len(melds) >= 1 and not conditions.get("is_double_pure_cleared",
False) else 0,
"is_double_pure_cleared": lambda: 4 if is_cleared and len(melds) >= 2 else 0,
# 极中极带两杠的清一色或清一色对子胡带杠计为4番称为“极中极”或“精品”点炮80分。
"is_seven_pairs": lambda: 2 if conditions.get("is_seven_pairs", False) and not conditions.get(
"is_dragon_seven_pairs", False) else 0,
# 七对手牌由7个对子组成计为2番。
@@ -43,9 +41,14 @@ def calculate_fan(hand, melds, is_self_draw, is_cleared, conditions):
"is_under_the_sea": lambda: 1 if conditions.get("is_under_the_sea", False) else 0, # 海底捞月最后一张牌被玩家摸到并胡牌计为1番。
"is_tian_hu": lambda: 12 if conditions.get("is_tian_hu", False) else 0, # 天胡庄家起牌后直接胡牌计为12番。
"is_di_hu": lambda: 12 if conditions.get("is_di_hu", False) else 0, # 地胡闲家在第一轮打牌时就胡牌计为12番。
# 基本胡:基础规则,仅在高级规则未生效时计算
"basic_win": lambda: 1 if not (conditions.get("is_seven_pairs", False) or
conditions.get("is_big_pairs", False) or
conditions.get("is_dragon_seven_pairs", False) or
conditions.get("is_pure_cleared", False) or
conditions.get("is_double_pure_cleared", False)) else 0,
}
# 逐一应用规则
print("\nCalculating fan...")
# 逐一应用规则
for rule, func in rules.items():
result = func()

View File

@@ -53,9 +53,28 @@ def test_clear_win():
fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=is_cleared(hand), conditions=conditions)
assert fan == 3, f"Expected 3 fans (1 basic + 2 cleared), got {fan}"
def test_pure_cleared():
"""
测试清对计分
"""
hand = [2] * 6 + [0] * 102 # 手牌:清对
melds = [0, 1, 2] # 模拟碰
conditions = {"is_pure_cleared": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions)
assert fan == 3, f"Expected 3 fans (pure cleared), got {fan}"
def test_pure_cleared():
"""
测试清对计分
"""
hand = [2] * 6 + [0] * 102 # 手牌:清对
melds = [0, 1, 2] # 模拟碰
conditions = {"is_pure_cleared": True} # 明确条件
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions)
assert fan == 3, f"Expected 3 fans (pure cleared), got {fan}"
def test_seven_pairs():
"""
测试七对计分
@@ -77,6 +96,40 @@ def test_seven_pairs():
assert fan == 2, f"Expected 2 fans (7 pairs), got {fan}"
def test_small_pairs():
"""
测试小七对计分
"""
hand = [2] * 6 + [0] * 102
melds = []
conditions = {"is_small_pairs": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions)
assert fan == 2, f"Expected 2 fans (small pairs), got {fan}"
def test_clear_seven_pairs():
"""
测试清七对计分
"""
hand = [2] * 7 + [0] * 101
melds = []
conditions = {"is_clear_seven_pairs": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=True, conditions=conditions)
assert fan == 12, f"Expected 12 fans (clear seven pairs), got {fan}"
def test_full_request():
"""
测试全求人计分
"""
hand = [2] + [0] * 107
melds = []
conditions = {"is_full_request": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions)
assert fan == 6, f"Expected 6 fans (full request), got {fan}"
def test_big_pairs():
"""
测试大对子计分
@@ -126,6 +179,59 @@ def test_gang_flower():
assert fan == 2, f"Expected 2 fans (1 basic + 1 gang flower), got {fan}"
def test_rob_gang():
"""
测试抢杠胡计分
"""
hand = [0] * 108
melds = []
conditions = {"is_rob_gang": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions)
assert fan == 1, f"Expected 1 fan (rob gang), got {fan}"
def test_under_the_sea():
"""
测试海底捞月计分
"""
hand = [0] * 108
melds = []
conditions = {"is_under_the_sea": True}
fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions)
assert fan == 1, f"Expected 1 fan (under the sea), got {fan}"
def test_cannon():
"""
测试放炮计分
"""
hand = [0] * 108
melds = []
conditions = {"is_cannon": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions)
assert fan == 1, f"Expected 1 fan (cannon), got {fan}"
def test_tian_hu():
"""
测试天胡计分
"""
hand = [0] * 108
melds = []
conditions = {"is_tian_hu": True}
fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions)
assert fan == 12, f"Expected 12 fans (tian hu), got {fan}"
def test_di_hu():
"""
测试地胡计分
"""
hand = [0] * 108
melds = []
conditions = {"is_di_hu": True}
fan = calculate_fan(hand, melds, is_self_draw=False, is_cleared=False, conditions=conditions)
assert fan == 12, f"Expected 12 fans (di hu), got {fan}"
def test_dragon_seven_pairs():
"""
测试龙七对计分
@@ -149,5 +255,13 @@ def test_dragon_seven_pairs():
fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions)
assert fan == 13, f"Expected 13 fans (1 self-draw + 12 dragon seven pairs), got {fan}"
def test_self_draw():
"""
测试自摸计分
"""
hand = [0] * 108
melds = []
conditions = {}
fan = calculate_fan(hand, melds, is_self_draw=True, is_cleared=False, conditions=conditions)
assert fan == 1, f"Expected 1 fan (self-draw), got {fan}"