Root cause: 1. Deal()完全忽略 _rules.Deck.Total → 牌库大小由 includeFlags 决定 2. Guangdong DSL: total=136 但 include_flowers=true → 实际144张 3. DS与引擎脱节导致广东无限对局(flaky test根因之一) 修复: - Deck构造加 expectedTotal 可选参数,不一致时抛 InvalidOperationException - Deal()传 _rules.Deck.Total → 所有DSL变更牌数时必须引擎一致 - Guangdong DSL: total 136→144(广东麻将含花牌=144张) Breaks: 无(向后兼容,expectedTotal默认null=不校验)
55 lines
1.9 KiB
C#
55 lines
1.9 KiB
C#
namespace RuleEngine.Core;
|
|
|
|
public class MahjongDeck
|
|
{
|
|
public List<int> Tiles { get; private set; } = new();
|
|
|
|
public MahjongDeck(bool includeHonors = false, bool includeFlowers = false, int wildcardCount = 0, int? expectedTotal = null)
|
|
{
|
|
var allTiles = MahjongTile.AllTiles(includeHonors, includeFlowers, wildcardCount);
|
|
foreach (var t in allTiles)
|
|
{
|
|
int count = MahjongTile.IsFlower(t) ? 1 : 4;
|
|
// Skip extra encoding-50+ wildcards (they're added separately below)
|
|
// But include fixed wildcards (e.g. 红中) as normal tiles
|
|
if (t >= MahjongTile.WildcardBase) continue;
|
|
for (int i = 0; i < count; i++)
|
|
Tiles.Add(t);
|
|
}
|
|
// Add extra wildcard tiles
|
|
for (int i = 0; i < wildcardCount; i++)
|
|
Tiles.Add(MahjongTile.WildcardBase + i);
|
|
|
|
// Validate against DSL total if specified
|
|
if (expectedTotal.HasValue && Tiles.Count != expectedTotal.Value)
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"牌库大小不匹配: 引擎生成 {Tiles.Count} 张, DSL 声明 {expectedTotal.Value} 张。" +
|
|
$" includeHonors={includeHonors} includeFlowers={includeFlowers} wildcardCount={wildcardCount}." +
|
|
$" 请检查 DSL deck.total 字段与 include_honors/include_flowers 的一致性。");
|
|
}
|
|
}
|
|
|
|
public void Shuffle(Random? rng = null)
|
|
{
|
|
rng ??= Random.Shared;
|
|
int n = Tiles.Count;
|
|
while (n > 1)
|
|
{
|
|
int k = rng.Next(n--);
|
|
(Tiles[n], Tiles[k]) = (Tiles[k], Tiles[n]);
|
|
}
|
|
}
|
|
|
|
public int Draw()
|
|
{
|
|
if (Tiles.Count == 0)
|
|
throw new InvalidOperationException("Deck is empty");
|
|
int last = Tiles[^1];
|
|
Tiles.RemoveAt(Tiles.Count - 1);
|
|
return last;
|
|
}
|
|
|
|
public int Count => Tiles.Count;
|
|
}
|