如何使用 Mathematica 检查扑克牌中是否有一对牌?

How to check if hand contains one pair of cards in poker game using Mathematica?

在每个玩家从 32 张牌组中获得 5 张牌的扑克游戏中,我试图使用 Mathematica 计算有多少子集恰好包含一对。我创建了一个有四套花色的套牌,并创建了所有可能的纸牌组合的子集,现在我正在尝试使用不同的方法过滤掉所有错误的组合,以排除四张和三张以及满屋。但过滤器仍然显示比实际值更高的值。 我程序的输出是“115584”,但实际结果应该是“107520”。有没有我忘记删除的组合? 这是代码

deck = Sort[Join[Range[7, 14], Range[7, 14], Range[7, 14], Range[7, 14]]]
hand = Subsets[deck, {5}]
SetAttributes[onePair, Orderless]
onePair [{x_, x_, y_, z_, w_} /; x != y != z != w] := True;
Count[hand, _?onePair]

我也尝试了下面的代码,但输出仍然不正确

onePair [{___, x_, x_, ___}] := True; (*we need two cards with same number but different suit*)
onePair [{___, x_, x_, x_, ___}] := False; (*this is to remove three of a kind*)
onePair[{___, x_, x_, y_, y_, ___} /; x != y] := False;(*to exclude the full house probability*)
onePair[{___}] := False;
Count[hand, _?onePair]

试试这个

deck=Sort[Join[Range[7, 14], Range[7, 14], Range[7, 14], Range[7, 14]]];
hands =Subsets[deck, {5}];
onePair [{___, x_, x_, ___}] := True;
onePair [{___, x_, x_, x_, ___}] := False;
onePair[{___, x_, x_, ___, y_, y_, ___} /; x != y] := False;
onePair[{___}] := False;
Count[hands, _?onePair]

我修改了倒数第二个模式以在第一对和第二对之间插入 ___。该代码给出了您想要的 107520。

我发现的方法是使用

Take[Cases[hands, _?onePair],100]

查看应该只包含一对的前一百个示例手牌,立即看到像 {7,7,8,9,9} 这样的手牌。

您也可以消除模式上的条件,无论 x 是否等于 y,所需的结果都是 False,它仍然会产生您想要的 107520。