mjAi/tests/test_chengdu_majiang_engine.py

97 lines
3.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

def test_draw_tile():
from src.engine.chengdu_mahjong_engine import ChengduMahjongEngine
engine = ChengduMahjongEngine()
initial_remaining = engine.state.remaining_tiles
tile = engine.draw_tile()
# 验证牌堆数量减少
assert engine.state.remaining_tiles == initial_remaining - 1, "牌堆数量未正确减少"
# 验证牌已加入当前玩家手牌
assert engine.state.hands[engine.state.current_player][tile] > 0, "摸牌未加入玩家手牌"
print(f"test_draw_tile passed: 摸到了 {tile}")
def test_discard_tile():
from src.engine.chengdu_mahjong_engine import ChengduMahjongEngine
engine = ChengduMahjongEngine()
tile = engine.draw_tile() # 玩家先摸牌
engine.discard_tile(tile) # 打出摸到的牌
# 验证手牌数量减少
assert engine.state.hands[engine.state.current_player][tile] == 0, "手牌未正确移除"
# 验证牌加入了牌河
assert tile in engine.state.discards[engine.state.current_player], "牌未正确加入牌河"
print(f"test_discard_tile passed: 打出了 {tile}")
def test_set_missing_suit():
from src.engine.game_state import ChengduMahjongState
state = ChengduMahjongState()
player = 0
missing_suit = ""
state.set_missing_suit(player, missing_suit)
# 验证缺门是否正确设置
assert state.missing_suits[player] == missing_suit, "缺门设置错误"
print(f"test_set_missing_suit passed: 缺门设置为 {missing_suit}")
def test_can_win():
from src.engine.game_state import ChengduMahjongState
state = ChengduMahjongState()
hand = [0] * 108
hand[0] = 2 # 两张1条对子
hand[3] = 1 # 2条
hand[4] = 1 # 3条
hand[5] = 1 # 4条
hand[10] = 1 # 5条
hand[11] = 1 # 6条
hand[12] = 1 # 7条
hand[20] = 1 # 8条
hand[21] = 1 # 9条
hand[22] = 1 # 1筒
hand[30] = 1 # 2筒
hand[31] = 1 # 3筒
hand[32] = 1 # 4筒
result = state.can_win(hand)
assert result is True, "胡牌判断失败"
print(f"test_can_win passed: 胡牌条件正确")
def test_peng():
from src.engine.chengdu_mahjong_engine import ChengduMahjongEngine
engine = ChengduMahjongEngine()
tile = 5 # 模拟手牌中有3张牌
engine.state.hands[engine.state.current_player][tile] = 3
engine.peng(tile)
# 验证手牌减少
assert engine.state.hands[engine.state.current_player][tile] == 1, "碰牌后手牌数量错误"
# 验证明牌记录
assert ("peng", tile) in engine.state.melds[engine.state.current_player], "碰牌未正确记录"
print(f"test_peng passed: 碰牌成功")
def test_gang():
from src.engine.chengdu_mahjong_engine import ChengduMahjongEngine
engine = ChengduMahjongEngine()
tile = 10 # 模拟手牌中有4张牌
engine.state.hands[engine.state.current_player][tile] = 4
engine.gang(tile, mode="an")
# 验证手牌减少
assert engine.state.hands[engine.state.current_player][tile] == 0, "杠牌后手牌数量错误"
# 验证明牌记录
assert ("an_gang", tile) in engine.state.melds[engine.state.current_player], "杠牌未正确记录"
print(f"test_gang passed: 杠牌成功")