Files
card-game-engine/RuleEngine/Core/MahjongTile.cs
xiaoou d734c0a384 fix: 圈风/门风/单钓将/四归一加_fanConfig守卫 + 精牌显示 + TileName修复
问题1: 圈风/门风/单钓将/四归一在南昌武汉等非Guobiao玩法中误生成
  修复: 4个新识别器加 _fanConfig.ContainsKey() 守卫,只有配了才生成
  Guobiao DSL 补充4个番型定义(圈风2/门风2/四归一2/单钓将1)

问题2: 翻牌定精信息未在demo显示
  修复: RenderDeal() 显示精牌(翻牌+正精+副精)

问题3: TileName?{tile}→ToStringCore(tile)
  修复: 带🀫前缀的数字牌显示 '🀫3万' 而非 '🀫?21'
2026-07-04 23:42:42 +08:00

126 lines
4.5 KiB
C#

namespace RuleEngine.Core;
/// Mahjong tile int encoding + static utilities
public static class MahjongTile
{
// Encoding: 万1-9=1-9, 条1-9=11-19, 筒1-9=21-29
// 字: 东=31,南=32,西=33,北=34,中=35,发=36,白=37
// 花: 春=41..菊=48
// 宝牌: 50-59 (extra wildcards added to deck)
public const int WildcardBase = 50;
/// <summary>Check if tile is in the encoding-50+ wildcard range only (no configured set).</summary>
public static bool IsWildcard(int tile) => tile >= WildcardBase && tile <= WildcardBase + 9;
/// <summary>Check if tile is a wildcard including game-specific configured set.</summary>
public static bool IsWildcard(int tile, WildcardRegistry wildcards) =>
tile >= WildcardBase && tile <= WildcardBase + 9 || wildcards.IsWildcard(tile);
public static int Encode(string suit, int rank)
{
return suit switch
{
"万" => rank,
"条" => 10 + rank,
"筒" => 20 + rank,
_ => throw new ArgumentException($"Unknown suit: {suit}")
};
}
/// <summary>ToString with optional wildcard display context.</summary>
public static string ToString(int tile) => ToStringCore(tile);
/// <summary>ToString with game-specific wildcard registry for 🀫 display.</summary>
public static string ToString(int tile, WildcardRegistry wildcards) =>
wildcards.IsWildcard(tile) ? $"🀫{TileName(tile)}" : ToStringCore(tile);
private static string ToStringCore(int tile)
{
if (tile >= 1 && tile <= 9) return $"{tile}万";
if (tile >= 11 && tile <= 19) return $"{tile - 10}条";
if (tile >= 21 && tile <= 29) return $"{tile - 20}筒";
return tile switch
{
31 => "东", 32 => "南", 33 => "西", 34 => "北",
35 => "中", 36 => "发", 37 => "白",
41 => "春", 42 => "夏", 43 => "秋", 44 => "冬",
45 => "梅", 46 => "兰", 47 => "竹", 48 => "菊",
>= 50 and <= 59 => "🃏",
_ => $"?{tile}"
};
}
private static string TileName(int tile) => tile switch
{
31 => "东", 32 => "南", 33 => "西", 34 => "北",
35 => "中", 36 => "发", 37 => "白",
_ => ToStringCore(tile)
};
/// 0=万, 1=条, 2=筒, 3=字, 4=花, 5=宝牌
public static int Suit(int tile)
{
if (tile >= 1 && tile <= 9) return 0;
if (tile >= 11 && tile <= 19) return 1;
if (tile >= 21 && tile <= 29) return 2;
if (tile >= 31 && tile <= 37) return 3;
if (tile >= 41 && tile <= 48) return 4;
if (tile >= 50 && tile <= 59) return 5;
return -1;
}
public static int Rank(int tile)
{
if (tile >= 1 && tile <= 9) return tile;
if (tile >= 11 && tile <= 19) return tile - 10;
if (tile >= 21 && tile <= 29) return tile - 20;
if (tile >= 31 && tile <= 37) return tile;
if (tile >= 41 && tile <= 48) return tile;
return tile;
}
public static string SuitName(int suit) => suit switch
{
0 => "万", 1 => "条", 2 => "筒", 3 => "字", 4 => "花", 5 => "宝牌", _ => "?"
};
public static bool IsHonor(int tile) => tile >= 31 && tile <= 37;
public static bool IsFlower(int tile) => tile >= 41 && tile <= 48;
public static bool IsNumbered(int tile) => tile >= 1 && tile <= 29;
public static bool IsTerminal(int tile)
{
if (IsHonor(tile)) return true;
if (IsNumbered(tile))
{
int r = Rank(tile);
return r == 1 || r == 9;
}
return false;
}
public static bool SameSuit(int a, int b) => Suit(a) == Suit(b);
public static int[] AllTiles(bool includeHonors = false, bool includeFlowers = false, int wildcardCount = 0,
int wildcardBase = 50)
{
int count = 27 + (includeHonors ? 7 : 0) + (includeFlowers ? 8 : 0) + wildcardCount;
var tiles = new int[count];
int idx = 0;
for (int i = 1; i <= 9; i++) tiles[idx++] = i;
for (int i = 1; i <= 9; i++) tiles[idx++] = 10 + i;
for (int i = 1; i <= 9; i++) tiles[idx++] = 20 + i;
if (includeHonors)
{
for (int i = 31; i <= 37; i++) tiles[idx++] = i;
}
if (includeFlowers)
{
for (int i = 41; i <= 48; i++) tiles[idx++] = i;
}
for (int i = 0; i < wildcardCount; i++)
tiles[idx++] = wildcardBase + i;
return tiles;
}
}