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

二次信任 提交于 2019-12-24 21:27:55

问题


In a poker where each player gets 5 cards from a 32 card deck, I am trying to calculate how many subsets contain exactly one pair using Mathematica. I created a deck with four suits and created subsets of all possible combinations of the cards and now I am trying to filter out all the wrong combinations using different methods to exclude the four of a kind and three of a kind and the full house. but the filter is still showing higher values than it actually is. The out put of my program is "115584" but the actual result should be "107520". Is there a combination that i forgot to remove? here is the code

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]

I also tried the following code but the output is still not correct

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]

回答1:


Try this

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]

where I modified your next to last pattern to insert ___ between your first pair and your second pair. That code gives your desired 107520.

The way I discovered that was to use

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

to look at the first hundred example hands that were supposed to contain only one pair and immediately saw hands like {7,7,8,9,9}.

You can also eliminate the condition on the pattern, the desired result will be False whether x equals y or not and it still yields your desired 107520.



来源:https://stackoverflow.com/questions/57913879/how-to-check-if-hand-contains-one-pair-of-cards-in-poker-game-using-mathematica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!