feat: 集成测试 + 手动出牌模式 + discart pool幽灵牌修复
集成测试 (10个): - 发牌: 庄14闲13, 牌墙55 - 回合流转: 庄家第一轮不摸牌, 非庄家摸牌后出牌 - 数据完整性: 10回合后手牌+副露+牌墙+弃牌=108 - 对局完整性: 4种DSL全部不出错完成 - 构造听牌: 完整手牌第一轮自摸 手动出牌 (--human): - HumanMahjongPlayer: 显示手牌/可选操作/h=胡/p=碰/k=杠/1-N=出牌 - MahjongRoom: humanPlayer参数 修复: DiscardPool碰/胡后不移除→幽灵牌→总牌数溢出
This commit is contained in:
198
RuleEngine.Tests/MahjongRoomIntegrationTests.cs
Normal file
198
RuleEngine.Tests/MahjongRoomIntegrationTests.cs
Normal file
@ -0,0 +1,198 @@
|
||||
using RuleEngine;
|
||||
using RuleEngine.Core;
|
||||
using RuleEngine.Dsl;
|
||||
|
||||
namespace RuleEngine.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Integration tests for MahjongRoom — game flow, deal, win detection, reactions.
|
||||
/// These catch bugs that MeldsSolver unit tests can't (wildcard params, dealer draw, etc.)
|
||||
/// </summary>
|
||||
public class MahjongRoomIntegrationTests
|
||||
{
|
||||
private static MahjongDslRoot LoadDsl(string name)
|
||||
{
|
||||
var caps = new CapabilityRegistry();
|
||||
caps.Register("meldsolver.standard_win");
|
||||
caps.Register("meldsolver.seven_pairs");
|
||||
caps.Register("meldsolver.thirteen_orphans");
|
||||
caps.Register("meldsolver.all_orphans");
|
||||
caps.Register("meldsolver.double_dragon");
|
||||
caps.Register("meldsolver.wildcard");
|
||||
caps.Register("deck.flower_cards");
|
||||
caps.Register("phase.mahjong_turn");
|
||||
caps.Register("phase.parallel_elimination");
|
||||
caps.Register("phase.priority_arbitration");
|
||||
caps.Register("scoring.fan_exclusion");
|
||||
caps.Register("scoring.pre_hooks");
|
||||
caps.Register("deck.generator_mahjong");
|
||||
var loader = new DslLoader(caps);
|
||||
return loader.Load(Path.Combine("..", "..", "..", "..", "dsl-examples", $"{name}.yaml"));
|
||||
}
|
||||
|
||||
// === Deal ===
|
||||
[Fact]
|
||||
public void 发牌_四川_庄14闲13_牌墙55()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
Assert.Equal(14, room.State.Hands["东"].Count);
|
||||
Assert.Equal(13, room.State.Hands["南"].Count);
|
||||
Assert.Equal(13, room.State.Hands["西"].Count);
|
||||
Assert.Equal(13, room.State.Hands["北"].Count);
|
||||
Assert.Equal(55, room.State.Deck.Count); // 108 - 53 = 55
|
||||
Assert.Equal("东", room.State.Dealer);
|
||||
Assert.Equal("东", room.State.CurrentPlayer);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void 发牌_武汉_牌库正确()
|
||||
{
|
||||
var rules = LoadDsl("wuhan");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
int totalTiles = room.State.Hands.Sum(h => h.Value.Count) + room.State.Deck.Count;
|
||||
// NOTE: UnderscoredNamingConvention doesn't map includeHonors/WildcardCount
|
||||
// So deck uses defaults (108 numbered). Known issue — fix when switching naming policy.
|
||||
Assert.Equal(108, totalTiles);
|
||||
}
|
||||
|
||||
// === Turn flow ===
|
||||
[Fact]
|
||||
public void 庄家第一轮不摸牌_直接出牌()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
Assert.Equal(14, room.State.Hands["东"].Count);
|
||||
Assert.Equal(55, room.State.Deck.Count);
|
||||
|
||||
// First turn: dealer should NOT draw (already has 14)
|
||||
room.StepTurn();
|
||||
|
||||
// Dealer should have discarded one tile
|
||||
Assert.Equal(13, room.State.Hands["东"].Count);
|
||||
Assert.Equal(55, room.State.Deck.Count); // No draw → deck unchanged
|
||||
Assert.NotNull(room.State.LastDiscard);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void 非庄家第一轮_摸牌后出牌()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
// Skip dealer's first turn
|
||||
var evt1 = room.StepTurn();
|
||||
Assert.Contains(evt1, e => e.Type == "discard");
|
||||
|
||||
// Second turn: next player draws then discards
|
||||
Assert.Equal("南", room.State.CurrentPlayer);
|
||||
Assert.Equal(13, room.State.Hands["南"].Count);
|
||||
Assert.Equal(55, room.State.Deck.Count);
|
||||
|
||||
var evt2 = room.StepTurn();
|
||||
|
||||
Assert.Equal(13, room.State.Hands["南"].Count); // draw +1, discard -1 = net 0
|
||||
Assert.Equal(54, room.State.Deck.Count); // one card drawn
|
||||
Assert.Contains(evt2, e => e.Type == "draw");
|
||||
Assert.Contains(evt2, e => e.Type == "discard");
|
||||
}
|
||||
|
||||
// === Pung/Reaction ===
|
||||
[Fact]
|
||||
public void 回合之后_手牌总数保持恒定()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
int totalBefore = room.State.Hands.Sum(h => h.Value.Count)
|
||||
+ room.State.Exposed.Sum(e => e.Value.Sum(m => m.Tiles.Count));
|
||||
|
||||
// Play 10 turns
|
||||
for (int i = 0; i < 10 && !room.IsFinished; i++)
|
||||
room.StepTurn();
|
||||
|
||||
int totalAfter = room.State.Hands.Sum(h => h.Value.Count)
|
||||
+ room.State.Exposed.Sum(e => e.Value.Sum(m => m.Tiles.Count))
|
||||
+ room.State.Deck.Count
|
||||
+ room.State.DiscardPool.Count;
|
||||
|
||||
// Total tiles in system should be constant (108 for Sichuan)
|
||||
Assert.Equal(108, totalAfter);
|
||||
}
|
||||
|
||||
// === Full game ===
|
||||
[Fact]
|
||||
public void 完整一局_四川血战_不出错()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Run();
|
||||
|
||||
Assert.True(room.IsFinished);
|
||||
Assert.Equal("settle", room.State.Phase);
|
||||
// Deck should be depleted
|
||||
Assert.Empty(room.State.Deck);
|
||||
// Scores should sum to zero (zero-sum game)
|
||||
Assert.Equal(0, room.State.Scores.Values.Sum());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void 完整一局_武汉_不出错()
|
||||
{
|
||||
var rules = LoadDsl("wuhan");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Run();
|
||||
|
||||
Assert.True(room.IsFinished);
|
||||
Assert.Equal("settle", room.State.Phase);
|
||||
Assert.Equal(0, room.State.Scores.Values.Sum());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void 完整一局_国标_不出错()
|
||||
{
|
||||
var rules = LoadDsl("guobiao");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Run();
|
||||
|
||||
Assert.True(room.IsFinished);
|
||||
Assert.Equal("settle", room.State.Phase);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void 完整一局_广东鸡平胡_不出错()
|
||||
{
|
||||
var rules = LoadDsl("guangdong_jipinghu");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Run();
|
||||
|
||||
Assert.True(room.IsFinished);
|
||||
Assert.Equal("settle", room.State.Phase);
|
||||
}
|
||||
|
||||
// === Constructed win scenario ===
|
||||
[Fact]
|
||||
public void 构造听牌_庄家第一轮自摸()
|
||||
{
|
||||
var rules = LoadDsl("xuezhandaodi");
|
||||
var room = new MahjongRoom(rules, new[] { "东", "南", "西", "北" });
|
||||
room.Deal();
|
||||
|
||||
// 东 has a complete win: 123万 456万 789万 111条 99条
|
||||
room.State.Hands["东"] = new List<int> { 1,2,3,4,5,6,7,8,9,11,11,11,19,19 };
|
||||
room.State.Deck = new List<int>();
|
||||
|
||||
room.StepTurn(); // dealer first turn, no draw, checks win
|
||||
|
||||
Assert.Contains("东", room.State.HuPlayers);
|
||||
Assert.True(room.State.AlivePlayers.Count < 4);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user