From 15eaaa42b6f13d92e756958cb2b71ee0e4442afc Mon Sep 17 00:00:00 2001 From: wangsiyuan <2392948297@qq.com> Date: Sun, 1 Dec 2024 03:47:31 +0800 Subject: [PATCH] 1 1 --- src/engine/fan_type.py | 54 ++++++++++++++++++++++++++++++ tests/test_fan_type.py | 76 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 128 insertions(+), 2 deletions(-) diff --git a/src/engine/fan_type.py b/src/engine/fan_type.py index feabaa1..50f14bd 100644 --- a/src/engine/fan_type.py +++ b/src/engine/fan_type.py @@ -69,4 +69,58 @@ def calculate_terminal_fan(hand, melds): return 0 +def is_seven_pairs(hand): + """ + 判断是否符合七对番型。 + + 七对要求:手牌由 7 个对子组成。 + + 参数: + - hand: Hand 对象,表示玩家当前的手牌。 + + 返回: + - int: 如果符合七对,返回 2(番数);否则返回 0。 + """ + # 获取手牌的计数 + tile_counts = hand.tile_count + + # 统计对子数量 + pairs_count = sum(1 for count in tile_counts.values() if count == 2) + + # 检查是否有 7 个对子 + if pairs_count == 7: + return 2 # 七对计为 2 番 + return 0 + +def is_full_request(hand, melds, winning_tile): + """ + 判断是否符合全求人番型。 + + 全求人要求: + - 玩家所有牌都通过碰、杠、吃完成。 + - 玩家手上只剩下 1 张牌。 + - 胡牌必须通过其他玩家打出的牌。 + + 参数: + - hand: Hand 对象,表示玩家当前的手牌。 + - melds: list[Meld] 对象,表示碰、杠等明牌。 + - winning_tile: MahjongTile 对象,表示胡的那张牌。 + + 返回: + - int: 如果符合全求人,返回 6(番数);否则返回 0。 + """ + # 检查手牌中是否只剩下 1 张牌 + if len(hand.tiles) != 1: + return 0 + + # 检查手中剩余的这张牌是否是胡牌 + if hand.tiles[0] != winning_tile: + return 0 + + # 检查是否有明牌(碰、杠、吃),且满足全求人条件 + if not melds or not all(meld.is_triplet() or meld.is_kong() or meld.is_sequence() for meld in melds): + return 0 + + # 符合全求人 + return 6 diff --git a/tests/test_fan_type.py b/tests/test_fan_type.py index 3d596c6..a8e7260 100644 --- a/tests/test_fan_type.py +++ b/tests/test_fan_type.py @@ -1,6 +1,6 @@ from src.engine.hand import Hand from src.engine.mahjong_tile import MahjongTile -from src.engine.fan_type import is_basic_win,is_cleared,calculate_terminal_fan +from src.engine.fan_type import is_basic_win,is_cleared,calculate_terminal_fan,is_seven_pairs,is_full_request from src.engine.meld import Meld def test_is_basic_win(): @@ -100,4 +100,76 @@ def test_calculate_terminal_fan(): hand.add_tile(MahjongTile("条", 5)) hand.add_tile(MahjongTile("条", 5)) melds = [] - assert calculate_terminal_fan(hand, melds) == 3, "测试失败:基本带幺九应为 3 番" \ No newline at end of file + assert calculate_terminal_fan(hand, melds) == 3, "测试失败:基本带幺九应为 3 番" + +def test_is_seven_pairs(): + """测试七对番型""" + hand = Hand() + + # 示例1:符合七对 + hand.add_tile(MahjongTile("条", 1)) + hand.add_tile(MahjongTile("条", 1)) + hand.add_tile(MahjongTile("条", 2)) + hand.add_tile(MahjongTile("条", 2)) + hand.add_tile(MahjongTile("条", 3)) + hand.add_tile(MahjongTile("条", 3)) + hand.add_tile(MahjongTile("条", 4)) + hand.add_tile(MahjongTile("条", 4)) + hand.add_tile(MahjongTile("条", 5)) + hand.add_tile(MahjongTile("条", 5)) + hand.add_tile(MahjongTile("条", 6)) + hand.add_tile(MahjongTile("条", 6)) + hand.add_tile(MahjongTile("条", 7)) + hand.add_tile(MahjongTile("条", 7)) + + assert is_seven_pairs(hand) == 2, "测试失败:符合七对,应为 2 番" + + # 示例2:不符合七对(少一个对子) + hand = Hand() + hand.add_tile(MahjongTile("条", 1)) + hand.add_tile(MahjongTile("条", 1)) + hand.add_tile(MahjongTile("条", 2)) + hand.add_tile(MahjongTile("条", 2)) + hand.add_tile(MahjongTile("条", 3)) + hand.add_tile(MahjongTile("条", 3)) + hand.add_tile(MahjongTile("条", 4)) + hand.add_tile(MahjongTile("条", 4)) + hand.add_tile(MahjongTile("条", 5)) + hand.add_tile(MahjongTile("条", 5)) + hand.add_tile(MahjongTile("条", 6)) + hand.add_tile(MahjongTile("条", 6)) + hand.add_tile(MahjongTile("条", 7)) + + assert is_seven_pairs(hand) == 0, "测试失败:不符合七对,应为 0 番" + + print("所有七对测试通过!") + +def test_is_full_request(): + """测试全求人番型""" + hand = Hand() + + # 示例1:符合全求人 + hand.add_tile(MahjongTile("筒", 5)) # 玩家手中只剩下 1 张牌 + melds = [ + Meld(MahjongTile("筒", 1), "碰"), + Meld(MahjongTile("筒", 2), "杠"), + Meld(MahjongTile("筒", 3), "碰"), + Meld(MahjongTile("筒", 4), "碰"), + ] + winning_tile = MahjongTile("筒", 5) # 胡牌通过别人打出的牌 + assert is_full_request(hand, melds, winning_tile) == 6, "测试失败:符合全求人,应为 6 番" + + # 示例2:不符合全求人(玩家手上有多张牌) + hand = Hand() + hand.add_tile(MahjongTile("筒", 5)) + hand.add_tile(MahjongTile("筒", 6)) + assert is_full_request(hand, melds, winning_tile) == 0, "测试失败:不符合全求人,应为 0 番" + + # 示例3:不符合全求人(没有碰或杠的明牌) + hand = Hand() + hand.add_tile(MahjongTile("筒", 5)) + melds = [] + assert is_full_request(hand, melds, winning_tile) == 0, "测试失败:不符合全求人,应为 0 番" + + print("所有全求人测试通过!") +