Poker hand analyser: Difference between revisions

m (→‎{{header|REXX}}: added/changed comments, simplified the function.)
Line 1,162:
joker Q♦ joker A♦ 10♦: straight-flush
joker 2♦ 2♠ joker q♦: four-of-a-kind
 
=={{header|Phix}}==
Woke up this morning with a neat idea for detecting straights, though jokers messed it up a bit.<br>
Uses an ad-hoc ranking system/tie breaker, not recommended for use in tournaments! Displays hands best-first.<br>
Note: I have left a copy of this in demo\HelloUTF8.exw to prove it works, but non-ascii on a Windows
console is not Phix's forte.<br>
For an example of using the unicode card characters see [[Playing_cards#Phix]]
<lang Phix>function poker(string hand)
hand = substitute(hand,"10","t")
sequence cards = split(hand,no_empty:=1)
if length(cards)!=5 then return "invalid hand" end if
sequence ranks = repeat(0,13),
suits = repeat(0,4)
integer jokers = 0
for i=1 to length(cards) do
sequence ci = utf8_to_utf32(cards[i])
if ci="joker" then
jokers += 1
if jokers>2 then return "invalid hand" end if
else
if length(ci)!=2 then return "invalid hand" end if
integer rank = find(lower(ci[1]),"23456789tjqka")
integer suit = find(ci[2],utf8_to_utf32("♥♣♦♠"))
if rank=0 or suit=0 then return "invalid hand" end if
ranks[rank] += 1
suits[suit] += 1
end if
end for
integer straight = match({1,1,1,1,1},ranks)
if not straight then
straight = sort(ranks)[$]=1 and match({0,0,0,0,0,0,0,0},ranks)
end if
integer _flush = (max(suits)+jokers = 5)
integer _pairs = max(ranks)+jokers
integer pair = find(2,ranks)
integer full_house = _pairs=3 and pair and (jokers=0 or find(2,ranks,pair+1))
integer two_pair = find(2,ranks,pair+1)
integer high_card = rfind(1,sq_ne(ranks,0))+1
if jokers and _pairs=jokers+1 then
straight = 1
integer k = find(1,ranks), j = jokers
for l=k to min(k+5-j,13) do
if ranks[l]=0 then
if j=0 then
straight = 0
exit
end if
j -= 1
end if
end for
if straight and j then
high_card = min(high_card+j,14)
end if
elsif straight and ranks[1]!=0 then
high_card = find(0,ranks)
end if
if _pairs=5 then return {10,"five of a kind", find(5-jokers,ranks)+1} end if
if straight and _flush then return {9,"straight flush", high_card} end if
if _pairs=4 then return {8,"four of a kind", find(4-jokers,ranks)+1} end if
if full_house then return {7,"full house", find(3-jokers,ranks)+1} end if
if _flush then return {6,"flush", high_card} end if
if straight then return {5,"straight", high_card} end if
if _pairs=3 then return {4,"three of a kind", find(3-jokers,ranks)+1} end if
if pair and two_pair then return {3,"two pair", two_pair+1} end if
if pair then return {2,"one pair", pair+1} end if
if jokers then return {2,"one pair", high_card} end if
return {1,"high card",high_card}
end function
 
sequence hands = {{0,"2♥ 2♦ 2♣ k♣ q♦"},
{0,"2♥ 5♥ 7♦ 8♣ 9♠"},
{0,"a♥ 2♦ 3♣ 4♣ 5♦"},
{0,"2♥ 3♥ 2♦ 3♣ 3♦"},
{0,"2♥ 7♥ 2♦ 3♣ 3♦"},
{0,"2♥ 7♥ 7♦ 7♣ 7♠"},
{0,"10♥ j♥ q♥ k♥ a♥"},
{0,"4♥ 4♠ k♠ 5♦ 10♠"},
{0,"q♣ 10♣ 7♣ 6♣ 4♣"},
{0,"joker 2♦ 2♠ k♠ q♦"},
{0,"joker 5♥ 7♦ 8♠ 9♦"},
{0,"joker 2♦ 3♠ 4♠ 5♠"},
{0,"joker 3♥ 2♦ 3♠ 3♦"},
{0,"joker 7♥ 2♦ 3♠ 3♦"},
{0,"joker 7♥ 7♦ 7♠ 7♣"},
{0,"joker j♥ q♥ k♥ A♥"},
{0,"joker 4♣ k♣ 5♦ 10♠"},
{0,"joker k♣ 7♣ 6♣ 4♣"},
{0,"joker 2♦ joker 4♠ 5♠"},
{0,"joker Q♦ joker A♠ 10♠"},
{0,"joker Q♦ joker A♦ 10♦"},
{0,"joker 2♦ 2♠ joker q♦"}}
 
for i=1 to length(hands) do
hands[i][1] = poker(hands[i][2])
end for
ppOpt({pp_Ascii,{#20,#FF}})
pp(reverse(sort(hands)))</lang>
{{out}}
<pre>
{{{10, "five of a kind", 7}, "joker 7♥ 7♦ 7♠ 7♣"},
{{9, "straight flush", 14}, "joker j♥ q♥ k♥ A♥"},
{{9, "straight flush", 14}, "joker Q♦ joker A♦ 10♦"},
{{9, "straight flush", 14}, "10♥ j♥ q♥ k♥ a♥"},
{{8, "four of a kind", 7}, "2♥ 7♥ 7♦ 7♣ 7♠"},
{{8, "four of a kind", 3}, "joker 3♥ 2♦ 3♠ 3♦"},
{{8, "four of a kind", 2}, "joker 2♦ 2♠ joker q♦"},
{{7, "full house", 3}, "2♥ 3♥ 2♦ 3♣ 3♦"},
{{6, "flush", 13}, "joker k♣ 7♣ 6♣ 4♣"},
{{6, "flush", 12}, "q♣ 10♣ 7♣ 6♣ 4♣"},
{{5, "straight", 14}, "joker Q♦ joker A♠ 10♠"},
{{5, "straight", 9}, "joker 5♥ 7♦ 8♠ 9♦"},
{{5, "straight", 6}, "joker 2♦ joker 4♠ 5♠"},
{{5, "straight", 5}, "joker 2♦ 3♠ 4♠ 5♠"},
{{5, "straight", 5}, "a♥ 2♦ 3♣ 4♣ 5♦"},
{{4, "three of a kind", 3}, "joker 7♥ 2♦ 3♠ 3♦"},
{{4, "three of a kind", 2}, "joker 2♦ 2♠ k♠ q♦"},
{{4, "three of a kind", 2}, "2♥ 2♦ 2♣ k♣ q♦"},
{{3, "two pair", 3}, "2♥ 7♥ 2♦ 3♣ 3♦"},
{{2, "one pair", 13}, "joker 4♣ k♣ 5♦ 10♠"},
{{2, "one pair", 4}, "4♥ 4♠ k♠ 5♦ 10♠"},
{{1, "high card", 9}, "2♥ 5♥ 7♦ 8♣ 9♠"}}
</pre>
 
=={{header|PicoLisp}}==
7,831

edits