Texas Hold'em poker

Initializations

Create a function to render the picture of the card:

Clear[cardPicture];

cardPicture[{suit_,val_}]:=Framed[Column[{val,suit},Center,0,ItemSize->{3,3},ItemStyle->Directive[24,"Label",Bold,Switch[suit, ♡| ♢, Darker[Red, 0.2], ♣| ♠, Black,"",White]]],FrameStyle->GrayLevel[1],RoundingRadius->6,Background->GrayLevel[1]];

cardPicture[{"",""}]=Framed[Column[{"",""},Center,0,ItemSize->{3,3},ItemStyle->Directive[24,"Label",Bold]],FrameStyle->RGBColor[0,0.501961,0],RoundingRadius->6,Background->RGBColor[0,0.501961,0]];

cardPicture[{" "," "}]=Framed[Column[{"",""},Center,0,ItemSize->{3,3},ItemStyle->Directive[24,"Label",Bold]],FrameStyle->ColorData["HTML"]["Indigo"],RoundingRadius->6,Background->ColorData["HTML"]["Indigo"]];

Create a list of the cards in a deck:

Clear[cardDeck,deal];

cardDeck=Reverse[Flatten[Map[({{♡,#},{♣,#},{♢,#},{♠,#}})&,Join[Table[i,{i,2,10}],{"J","Q","K","A"}]],1]];

deal=RandomSample[cardDeck,12]

Make a function to evaluate the poker hand:

Clear[evaluatePokerHand];

evaluatePokerHand[hand_List]:=Module[{ls,suit,tmp,tmp1,flushHighCard,straightHighCard,len,result},
ls=hand/.{"J"-> 11,"Q"-> 12,"K"->13,"A"-> 14};
tmp1=Sort[Tally[ls[[All,2]]],#1[[2]]<#2[[2]]&];

result=Which[
(* test for flush *)
MemberQ[Tally[ls[[All,1]]][[All,2]],5|6|7],
(* flush true, which suit? *)
suit=Select[Tally[ls[[All,1]]],(#[[2]]==5||#[[2]]==6||#[[2]]==7&)][[1,1]];
(* select and sort all cards of that suit *)
tmp=Sort@Select[ls,#[[1]]==suit&];
(* find high card *)
flushHighCard=flushHighCard=tmp[[-1,2]];
flushHighCard=flushHighCard/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
(* test list for straight within flush *)
tmp1=Differences@Sort@Flatten@tmp[[All,2]];
If[(* is straight *)
Length@Cases[{tmp1},{___,1,1,1,1,___}]>0,
(* then straight flush, find high card *)
len=Length@Cases[{tmp1},{___,1,1,1,1,y___}->y];
straightHighCard=Last[(Sort@Flatten@tmp[[All,2]])[[3-len;;-len-1]]];
straightHighCard=straightHighCard/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
ToString[straightHighCard]<>" high straight flush",
(* else just flush *)
ToString[flushHighCard]<> " high flush"
],

(* test for straight *)
tmp=Differences@Sort@Flatten@ls[[All,2]];Length@Cases[{tmp},{___,1,1,1,1,___}]>0,
len=Length@Cases[{tmp},{___,1,1,1,1,y___}->y];
straightHighCard=Last[(Sort@Flatten@tmp1[[All,1]])[[3-len;;-len-1]]];
straightHighCard=straightHighCard/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
ToString[straightHighCard]<>" high straight",

(* a pair *)
tmp=Cases[{tmp1},{__,y:{_,1},x:{_,2}}-> Sequence[x]]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
Length[tmp]==1,
"Pair of "<>ToString[tmp[[1,1]]]<>"s",

(* high card *)
tmp=Cases[{tmp1},{__,y:{_,1},x:{_,1}}-> Sequence[x]];
Length[tmp]==1,
tmp=Sort[Tally[ls[[All,2]]],#1[[1]]>#2[[1]]&]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
ToString[tmp[[1,1]]]<>" high",

(* two pair *)
tmp=Cases[{tmp1},{__,y:{_,2},x:{_,2}}-> Sequence[x,y]]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
Length[tmp]==2,
"Two pair, "<>ToString[tmp[[1,1]]]<>"s and "<>ToString[tmp[[2,1]]]<>"s",

(* three of a kind *)
tmp=Cases[{tmp1},{__,y:{_,1},x:{_,3}}-> Sequence[x]]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
Length[tmp]==1,
"Three "<>ToString[tmp[[1,1]]]<>"s",

(* full house *)
tmp=Cases[{tmp1},{__,y:{_,2|3},x:{_,3}}-> Sequence[x,y]]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
Length[tmp]==2,
"Full House, "<>ToString[tmp[[1,1]]]<>"s and "<>ToString[tmp[[2,1]]]<>"s",

(* four of a kind *)
tmp=Cases[{tmp1},{__,x:{_,4}}-> Sequence[x]]/.{11->"Jack",12->"Queen",13->"King",14->"Ace"};
Length[tmp]==1,
"Four "<>ToString[tmp[[1,1]]]<>"s"
];

(* set style for displaying result *)
Text[Style[result,FontFamily ->"Century Gothic",FontColor->RGBColor[1,1,0], FontSize->12,  FontWeight->"Bold",FontSlant->"Italic"]]
]

Implementation

Manipulate[Dynamic[Module[{g,j,opponent,result1,result2},
j=0;
result1="";
result2="";
opponent={{" "," "},{" "," "}};

g=Which[
i=="Deal",{{"",""},{"",""},{"",""},{"",""},{"",""}},
i=="Flop",Join[deal[[{6,7,8}]],{{"",""},{"",""}}],
i=="Turn",Join[deal[[{6,7,8,10}]],{{"",""}}],
i=="River",
result2=evaluatePokerHand[deal[[{2,4,6,7,8,10,12}]]];
deal[[{6,7,8,10,12}]],
i=="Opponent",
opponent=deal[[{1,3}]];
result1=evaluatePokerHand[deal[[{1,3,6,7,8,10,12}]]];
result2=evaluatePokerHand[deal[[{2,4,6,7,8,10,12}]]];
deal[[{6,7,8,10,12}]]
];

Column[cc=Row[Flatten[{cardPicture/@# ,j++;"tmp"<>ToString[j]}],"  "]&/@{opponent,g,deal[[{2,4}]]},Spacings->3,Background->RGBColor[0,0.501961,0]]/.{"tmp1"-> result1,"tmp2"-> "","tmp3"-> result2}
]
],
Row[{Button["Deal",i="Deal";deal=RandomSample[cardDeck,12]]}],
{{i,"Deal",""},{"Flop","Turn","River","Opponent"}},
ImageMargins->{{150,0},{0,0}},
ControlPlacement->Bottom,
Paneled->False,
AppearanceElements->None
]

(* nb=InputNotebook[];
SelectionMove[nb,Previous,Cell]
SetOptions[NotebookSelection[nb],Background->RGBColor[0,0.501961,0],ShowCellLabel-> False,CellMargins-> {{Inherited,Inherited},{50,50}}] *)