【问题标题】:How to check if hand contains one pair of cards in poker game using Mathematica?如何在使用 Mathematica 的扑克游戏中检查手牌中是否包含一对牌?
【发布时间】:2020-01-14 18:06:31
【问题描述】:

在每个玩家从 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]

【问题讨论】:

    标签: set conditional-statements wolfram-mathematica probability poker


    【解决方案1】:

    试试这个

    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。

    【讨论】:

      猜你喜欢
      • 2016-03-01
      • 1970-01-01
      • 2012-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      相关资源
      最近更新 更多