Poker hand analyser: Difference between revisions

Added FreeBASIC
mNo edit summary
(Added FreeBASIC)
 
(42 intermediate revisions by 13 users not shown)
Line 9:
A poker hand is specified as a space separated list of five playing cards.
 
Each input card has two characters indicating face and suit.   For example:   '''2d'''   (two of diamonds).
 
Faces are: '''a''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''10''', '''j''', '''q''', '''k'''
 
;Example:
Suits are: '''h''' (hearts), '''d''' (diamonds), '''c''' (clubs), and '''s''' (spades), or alternatively the unicode card-suit characters: ♥ ♦ ♣ ♠
::::'''2d'''       (two of diamonds).
 
 
 
Faces are:    '''a''', '''2''', '''3''', '''4''', '''5''', '''6''', '''7''', '''8''', '''9''', '''10''', '''j''', '''q''', '''k'''
 
Suits are:    '''h''' (hearts),   '''d''' (diamonds),   '''c''' (clubs),   and   '''s''' (spades),   or
<br>alternatively, &nbsp; the unicode card-suit characters: &nbsp;&nbsp; <big> ♥ ♦ ♣ ♠ </big>
 
 
Duplicate cards are illegal.
Line 29 ⟶ 37:
invalid
 
 
Examples:
;Examples:
2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
2♥ 5♥2♦ 7♦2♣ 8♣k♣ 9♠q♦: high three-of-a-cardkind
a♥2♥ 2♦5♥ 3♣7♦ 4♣8♣ 5♦9♠: straight high-card
2♥ 3♥a♥ 2♦ 3♣ 3♦4♣ 5♦: full-house straight
2♥ 7♥3♥ 2♦ 3♣ 3♦: two full-pairhouse
2♥ 7♥ 7♦2♦ 7♣3♣ 7♠3♦: four two-of-a-kind pair
10♥2♥ j♥7♥ q♥7♦ k♥7♣ a♥7♠: straight four-flushof-a-kind
4♥10♥ 4♠j♥ k♠q♥ 5♦k♥ 10♠a♥: one straight-pairflush
q♣4♥ 10♣4♠ 7♣k♠ 6♣5♦ 4♣10♠: flush one-pair
q♣ 10♣ 7♣ 6♣ q♣: invalid
 
The programs output for the above examples should be displayed here on this page.
Line 46 ⟶ 55:
# use the playing card characters introduced with Unicode 6.0 (U+1F0A1 - U+1F0DE).
# allow two jokers
::* use the symbol &nbsp; '''joker'''
::* duplicates would be allowed (for jokers only)
::* five-of-a-kind would then be the highest hand
Line 52 ⟶ 61:
 
;More extra credit examples:
joker 2♦ 2♠ k♠ q♦: three-of-a-kind
joker 5♥ 7♦ 8♠ 9♦: straight
joker 2♦ 3♠ 4♠ 5♠: straight
joker 3♥ 2♦ 3♠ 3♦: four-of-a-kind
joker 7♥ 2♦ 3♠ 3♦: three-of-a-kind
joker 7♥ 7♦ 7♠ 7♣: five-of-a-kind
joker j♥ q♥ k♥ A♥: straight-flush
joker 4♣ k♣ 5♦ 10♠: one-pair
joker k♣ 7♣ 6♣ 4♣: flush
joker 2♦ joker 4♠ 5♠: straight
joker Q♦ joker A♠ 10♠: straight
joker Q♦ joker A♦ 10♦: straight-flush
joker 2♦ 2♠ joker q♦: four-of-a-kind
 
 
;Related tasks:
* [[Playing cards]]
* [[Card shuffles]]
* [[Deal cards_for_FreeCell]]
* [[War Card_Game]]
* [[Go Fish]]
<br><br>
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F analyzeHandHelper(faceCount, suitCount)
V
p1 = 0B
p2 = 0B
t = 0B
f = 0B
fl = 0B
st = 0B
 
L(fc) faceCount
S fc
2 {I p1 {p2 = 1B} E p1 = 1B}
3 {t = 1B}
4 {f = 1B}
 
L(sc) suitCount
I sc == 5
fl = 1B
 
I !p1 & !p2 & !t & !f
V s = 0
L(fc) faceCount
I fc != 0
s++
E
s = 0
I s == 5
L.break
 
st = (s == 5) | (s == 4 & faceCount[0] != 0 & faceCount[1] == 0)
 
I st & fl {R ‘straight-flush’}
E I f {R ‘four-of-a-kind’}
E I p1 & t {R ‘full-house’}
E I fl {R ‘flush’}
E I st {R ‘straight’}
E I t {R ‘three-of-a-kind’}
E I p1 & p2 {R ‘two-pair’}
E I p1 {R ‘one-pair’}
E {R ‘high-card’}
 
F analyzeHand(inHand)
V handLen = 5
V face = ‘A23456789TJQK’
V suit = ‘SHCD’
V errorMessage = ‘invalid hand.’
 
V hand = sorted(inHand.split(‘ ’))
I hand.len != handLen
R errorMessage
I Set(hand).len != handLen
R errorMessage‘ Duplicated cards.’
 
V faceCount = [0] * face.len
V suitCount = [0] * suit.len
L(card) hand
I card.len != 2
R errorMessage
V? n = face.find(card[0])
V? l = suit.find(card[1])
I n == N | l == N
R errorMessage
faceCount[n]++
suitCount[l]++
 
R analyzeHandHelper(faceCount, suitCount)
 
L(hand) [‘2H 2D 2S KS QD’,
‘2H 5H 7D 8S 9D’,
‘AH 2D 3S 4S 5S’,
‘2H 3H 2D 3S 3D’,
‘2H 7H 2D 3S 3D’,
‘2H 7H 7D 7S 7C’,
‘TH JH QH KH AH’,
‘4H 4C KC 5D TC’,
‘QC TC 7C 6C 4C’]
print(hand‘: ’analyzeHand(hand))</syntaxhighlight>
 
{{out}}
<pre>
2H 2D 2S KS QD: three-of-a-kind
2H 5H 7D 8S 9D: high-card
AH 2D 3S 4S 5S: straight
2H 3H 2D 3S 3D: full-house
2H 7H 2D 3S 3D: two-pair
2H 7H 7D 7S 7C: four-of-a-kind
TH JH QH KH AH: straight-flush
4H 4C KC 5D TC: one-pair
QC TC 7C 6C 4C: flush
</pre>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">PokerHand(hand){
StringUpper, hand, hand
Sort, hand, FCardSort D%A_Space%
Line 91 ⟶ 202:
b := (b = "T") ? 10 : (b = "J") ? 11 : (b = "Q") ? 12 : (b = "K") ? 13 : b
return a > b ? 1 : a < b ? -1 : 0
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">hands =
(join`r`n
2♥ 2♦ 2♣ k♣ q♦
Line 109 ⟶ 220:
res .= PokerHand(A_LoopField) "`n"
MsgBox, 262144, , % res
return</langsyntaxhighlight>
Outputs:<pre>2♦ 2♣ 2♥ Q♦ K♣ Three of a Kind
2♥ 5♥ 7♦ 8♣ 9♠ High Card
Line 124 ⟶ 235:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <ctype.h>
#include <string.h>
Line 252 ⟶ 363:
}
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 270 ⟶ 381:
=={{header|C sharp}}==
{{works with|C sharp|8}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using static System.Linq.Enumerable;
Line 431 ⟶ 542:
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 459 ⟶ 570:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang Cpp>
#include <iostream>
#include <sstream>
Line 534 ⟶ 645:
cout << p.analyze( "qc tc 7c 6c 4c" ) << endl << endl; return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 549 ⟶ 660:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn rank [card]
(let [[fst _] card]
(if (Character/isDigit fst)
Line 629 ⟶ 740:
["QC" "TC" "7C" "6C" "4C"]])
(run! println (map #(str % " : " (check-hand %)) hands))
</syntaxhighlight>
</lang>
 
{{out}}
Line 648 ⟶ 759:
No bonus for this simple version.
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.algorithm, std.range;
 
string analyzeHand(in string inHand) pure /*nothrow @safe*/ {
Line 733 ⟶ 844:
"QC TC 7C 6C 4C"])
writeln(hand, ": ", hand.analyzeHand);
}</langsyntaxhighlight>
{{out}}
<pre>2H 2D 2S KS QD: three-of-a-kind
Line 748 ⟶ 859:
{{trans|Ruby}}
{{works with|Elixir|1.2}}
<langsyntaxhighlight lang="elixir">defmodule Card do
@faces ~w(2 3 4 5 6 7 8 9 10 j q k a)
@suits ~w(♥ ♦ ♣ ♠) # ~w(h d c s)
Line 874 ⟶ 985:
best = Enum.map(cards_list, &Hand.new &1) |> Enum.max
IO.puts "#{hand}:\t#{elem(best,3)}"
end)</langsyntaxhighlight>
 
{{out}}
Line 911 ⟶ 1,022:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
type Card = int * int
 
Line 1,011 ⟶ 1,122:
]
|> List.iter showHandRank
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,041 ⟶ 1,152:
=={{header|Factor}}==
Factor comes with a poker hand evaluator.
<langsyntaxhighlight lang="factor">USING: formatting kernel poker sequences ;
{
"2H 2D 2C KC QD"
Line 1,052 ⟶ 1,163:
"4H 4S KS 5D TS"
"QC TC 7C 6C 4C"
} [ dup string>hand-name "%s: %s\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,065 ⟶ 1,176:
QC TC 7C 6C 4C: Flush
</pre>
 
=={{header|FreeBASIC}}==
{{trans|C}}
<syntaxhighlight lang="vbnet">Const As String FACES = "23456789tjqka"
Const As String SUITS = "shdc"
 
Type card
face As Integer ' FACES map to 0..12 respectively
suit As String
End Type
 
Dim Shared As card cards(4)
 
Sub insertionSort(arr() As card, Byval n As Integer)
Dim As Integer i, key, j
For i = 1 To n-1
key = arr(i).face
j = i-1
While j >= 0 And arr(j).face > key
arr(j+1) = arr(j)
j = j-1
Wend
arr(j+1).face = key
Next i
End Sub
 
Function compareCard(Byval a As card, Byval b As card) As Integer
Return a.face - b.face
End Function
 
Function equalsCard(Byval c1 As card, Byval c2 As card) As Integer
If c1.face = c2.face And c1.suit = c2.suit Then Return True
Return False
End Function
 
Function areDistinct() As Integer
Dim As Integer i, j
For i = 0 To 3
For j = i + 1 To 4
If equalsCard(cards(i), cards(j)) = True Then Return False
Next j
Next i
Return True
End Function
 
Function isStraight() As Boolean
insertionSort(cards(), 5)
If cards(0).face + 4 = cards(4).face Then Return True
If cards(4).face = 12 And cards(0).face = 0 And cards(3).face = 3 Then Return True
Return False
End Function
 
Function isFlush() As Boolean
Dim As String suit = cards(0).suit
For i As Integer = 1 To 4
If cards(i).suit <> suit Then Return False
Next i
Return True
End Function
 
Function analyzeHand(Byval hand As String) As String
Dim As Integer i, j, cp, gs = 0
Dim As String suit
Dim As Integer found, flush, straight
Dim As Integer groups(12)
If Len(hand) <> 14 Then Return "invalid"
For i = 0 To 13 Step 3
cp = Instr(FACES, Lcase(Mid(hand, i + 1, 1)))
If cp = 0 Then Return "invalid"
j = i \ 3
cards(j).face = cp - 1
suit = Lcase(Mid(hand, i + 2, 1))
cp = Instr(SUITS, suit)
If cp = 0 Then Return "invalid"
cards(j).suit = suit
Next i
If areDistinct() = False Then Return "invalid"
For i = 0 To 12
groups(i) = 0
Next i
For i = 0 To 4
groups(cards(i).face) += 1
Next i
For i = 0 To 12
If groups(i) > 0 Then gs += 1
Next i
Select Case gs
Case 2
found = False
For i = 0 To 12
If groups(i) = 4 Then
found = True
Exit For
End If
Next i
If found = True Then Return "four-of-a-kind"
Return "full-house"
Case 3
found = False
For i = 0 To 12
If groups(i) = 3 Then
found = True
Exit For
End If
Next i
If found = True Then Return "three-of-a-kind"
Return "two-pairs"
Case 4
Return "one-pair"
Case Else
flush = isFlush()
straight = isStraight()
If flush = True And straight = True Then
Return "straight-flush"
Elseif flush = True Then
Return "flush"
Elseif straight = True Then
Return "straight"
Else
Return "high-card"
End If
End Select
End Function
 
Dim As String tipo
Dim As String hands(9) = { _
"2h 2d 2c kc qd", _
"2h 5h 7d 8c 9s", _
"ah 2d 3c 4c 5d", _
"2h 3h 2d 3c 3d", _
"2h 7h 2d 3c 3d", _
"2h 7h 7d 7c 7s", _
"th jh qh kh ah", _
"4h 4s ks 5d ts", _
"qc tc 7c 6c 4c", _
"ah ah 7c 6c 4c" }
 
For i As Integer = 0 To 9
tipo = analyzeHand(hands(i))
Print hands(i); ": "; tipo
Next i
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as C entry.</pre>
 
=={{header|Go}}==
{{trans|Kotlin}}
===Basic Version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,194 ⟶ 1,451:
fmt.Printf("%s: %s\n", hand, analyzeHand(hand))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,211 ⟶ 1,468:
 
===Extra Credit Version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,426 ⟶ 1,683:
fmt.Printf("%s: %s\n", hand, analyzeHand(hand))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,446 ⟶ 1,703:
🃂 🃞 🃍 🃁 🃊 : high-card
</pre>
 
=={{header|Haskell}}==
===Basic Version===
<syntaxhighlight lang="haskell">{-# LANGUAGE TupleSections #-}
 
import Data.Function (on)
import Data.List (group, nub, any, sort, sortBy)
import Data.Maybe (mapMaybe)
import Text.Read (readMaybe)
 
data Suit = Club | Diamond | Spade | Heart deriving (Show, Eq)
 
data Rank = Ace | Two | Three | Four | Five | Six | Seven
| Eight | Nine | Ten | Jack | Queen | King
deriving (Show, Eq, Enum, Ord, Bounded)
 
data Card = Card { suit :: Suit, rank :: Rank } deriving (Show, Eq)
 
type Hand = [Card]
 
consumed = pure . (, "")
 
instance Read Suit where
readsPrec d s = case s of "♥" -> consumed Heart
"♦" -> consumed Diamond
"♣" -> consumed Spade
"♠" -> consumed Club
"h" -> consumed Heart
_ -> []
 
instance Read Rank where
readsPrec d s = case s of "a" -> consumed Ace
"2" -> consumed Two
"3" -> consumed Three
"4" -> consumed Four
"5" -> consumed Five
"6" -> consumed Six
"7" -> consumed Seven
"8" -> consumed Eight
"9" -> consumed Nine
"10" -> consumed Ten
"j" -> consumed Jack
"q" -> consumed Queen
"k" -> consumed King
_ -> []
 
instance Read Card where
readsPrec d = fmap (, "") . mapMaybe card . lex
where
card (r, s) = Card <$> (readMaybe s :: Maybe Suit)
<*> (readMaybe r :: Maybe Rank)
 
-- Special hand
acesHigh :: [Rank]
acesHigh = [Ace, Ten, Jack, Queen, King]
 
isSucc :: (Enum a, Eq a, Bounded a) => [a] -> Bool
isSucc [] = True
isSucc [x] = True
isSucc (x:y:zs) = (x /= maxBound && y == succ x) && isSucc (y:zs)
 
nameHand :: Hand -> String
nameHand [] = "Invalid Input"
nameHand cards | invalidHand = "Invalid hand"
| straight && flush = "Straight flush"
| ofKind 4 = "Four of a kind"
| ofKind 3 && ofKind 2 = "Full house"
| flush = "Flush"
| straight = "Straight"
| ofKind 3 = "Three of a kind"
| uniqRanks == 3 = "Two pair"
| uniqRanks == 4 = "One pair"
| otherwise = "High card"
where
sortedRank = sort $ rank <$> cards
rankCounts = sortBy (compare `on` snd) $ (,) <$> head <*> length <$> group sortedRank
uniqRanks = length rankCounts
ofKind n = any ((==n) . snd) rankCounts
straight = isSucc sortedRank || sortedRank == acesHigh
flush = length (nub $ suit <$> cards) == 1
invalidHand = length (nub cards) /= 5
 
testHands :: [(String, Hand)]
testHands = (,) <$> id <*> mapMaybe readMaybe . words <$>
[ "2♥ 2♦ 2♣ k♣ q♦"
, "2♥ 5♥ 7♦ 8♣ 9♠"
, "a♥ 2♦ 3♣ 4♣ 5♦"
, "2♥ 3♥ 2♦ 3♣ 3♦"
, "2♥ 7♥ 2♦ 3♣ 3♦"
, "2♥ 7♥ 7♦ 7♣ 7♠"
, "10♥ j♥ q♥ k♥ a♥"
, "4♥ 4♠ k♠ 5♦ 10♠"
, "q♣ 10♣ 7♣ 6♣ 4♣"
, "q♣ 10♣ 7♣ 6♣ 7♣" -- duplicate cards
, "Bad input" ]
 
main :: IO ()
main = mapM_ (putStrLn . (fst <> const ": " <> nameHand . snd)) testHands</syntaxhighlight>
{{out}}
<pre>2♥ 2♦ 2♣ k♣ q♦: Three of a kind
2♥ 5♥ 7♦ 8♣ 9♠: High card
a♥ 2♦ 3♣ 4♣ 5♦: Straight
2♥ 3♥ 2♦ 3♣ 3♦: Full house
2♥ 7♥ 2♦ 3♣ 3♦: Two pair
2♥ 7♥ 7♦ 7♣ 7♠: Four of a kind
10♥ j♥ q♥ k♥ a♥: Straight flush
4♥ 4♠ k♠ 5♦ 10♠: One pair
q♣ 10♣ 7♣ 6♣ 4♣: Flush
q♣ 10♣ 7♣ 6♣ 7♣: Invalid hand
Bad input: Invalid Input</pre>
 
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">parseHand=: <;._2@,&' '@ cut 7&u:~&7 NB. hand must be well formed
Suits=: <"> 7 u: '♥♦♣♦' NB. or Suits=: 'hdcs'
Faces=: <;._1 ' 2 3 4 5 6 7 8 9 10 j q k a'
Line 1,484 ⟶ 1,851:
('straight-flush' IF (straight * flush)) Or
('five-of-a-kind' IF five)
)
)</lang>
 
Hands=: <@deb;._2 {{)n
2♥ 2♦ 2♣ k♣ q♦
2♥ 5♥ 7♦ 8♣ 9♠
a♥ 2♦ 3♣ 4♣ 5♦
2♥ 3♥ 2♦ 3♣ 3♦
2♥ 7♥ 2♦ 3♣ 3♦
2♥ 7♥ 7♦ 7♣ 7♠
10♥ j♥ q♥ k♥ a♥
4♥ 4♠ k♠ 5♦ 10♠
q♣ 10♣ 7♣ 6♣ 4♣
}}</syntaxhighlight>
 
Note that * acts as "logical and" on logical values (if you need to deal with boolean values in the original sense - which were not constrained to logical values - you should use *. instead of * to achieve boolean multiplication, but that's not needed here).
 
Output for required<code>rateHand@> examplesHands</code>:
 
<pre>2♥ 2♦ 2♣ k♣ q♦ three-of-a-kind
2♥ 5♥ 7♦ 8♣ 9♠ high-card
a♥ 2♦ 3♣ 4♣ 5♦ high-card
2♥ 3♥ 2♦ 3♣ 3♦ full-house
2♥ 7♥ 2♦ 3♣ 3♦ two-pair
2♥ 7♥ 7♦ 7♣ 7♠ four-of-a-kind
10♥ j♥ q♥ k♥ a♥ straight-flush
4♥ 4♠ k♠ 5♦ 10♠ one-pair
q♣ 10♣ 7♣ 6♣ 4♣ flush
</pre>
 
Output for extra-credit examples
Line 1,519 ⟶ 1,899:
{{works with|Java|7}}
This code does not qualify for extra credit. Although it supports wildcards, it does not allow for duplicates.
<langsyntaxhighlight lang="java">import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
Line 1,698 ⟶ 2,078:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,748 ⟶ 2,128:
=={{header|JavaScript}}==
{{works with|JavaScript|ECMAScript 6}}
<langsyntaxhighlight JavaScriptlang="javascript">const FACES = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'j', 'q', 'k', 'a'];
const SUITS = ['♥', '♦', '♣', '♠'];
 
Line 1,778 ⟶ 2,158:
else if (groups[0] === 2) return 'one-pair'
else return 'high-card';
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight JavaScriptlang="javascript">let testHands = [
"2♥ 2♦ 2♣ k♣ q♦",
"2♥ 5♥ 7♦ 8♣ 9♠",
Line 1,799 ⟶ 2,179:
];
for(hand of testHands) console.log(hand + ": " + analyzeHand(hand));</langsyntaxhighlight>
{{out}}
<pre>
Line 1,821 ⟶ 2,201:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">sorteddeck = [string(r) * s for s in "♣♦♥♠", r in "23456789TJQKA"]
 
cardlessthan(card1, card2) = indexin(x, sorteddeck)[1] < indexin(y, sorteddeck)[1]
Line 1,941 ⟶ 2,321:
println("Hand $hand is a ", scorehand(hand), " hand.")
end
</langsyntaxhighlight>{{output}}<pre>
Hand ["2♥", "2♦", "2♣", "K♣", "Q♦"] is a three-of-a-kind hand.
Hand ["2♥", "5♥", "7♦", "8♣", "9♠"] is a high-card hand.
Line 1,954 ⟶ 2,334:
 
 
=={{header|Haskell}}==
===Basic Version===
<lang Haskell>
import Data.List
import Data.Maybe
import System.Environment
 
data Suit = Club | Diamond | Spade | Heart deriving (Show, Eq)
 
data Rank = Ace | Two | Three | Four | Five | Six | Seven |
Eight | Nine | Ten | Jack | Queen | King
deriving (Show, Eq, Enum, Ord, Bounded)
 
data Card = Card { suit :: Suit
, rank :: Rank
} deriving (Show)
 
-- Special hand
acesHigh = [Ace, Ten, Jack, Queen, King]
 
isSucc :: (Enum a, Eq a, Bounded a) => [a] -> Bool
isSucc [] = True
isSucc (x:[]) = True
isSucc (x:y:zs) | x /= maxBound && y == succ x = isSucc $ y:zs
isSucc _ = False
 
parseCard :: String -> Maybe Card
parseCard [] = Nothing
parseCard xs = (\a b -> Card a b) <$> parsedSuit <*> parsedRank
where parsedRank = case r of "a" -> Just Ace
"2" -> Just Two
"3" -> Just Three
"4" -> Just Four
"5" -> Just Five
"6" -> Just Six
"7" -> Just Seven
"8" -> Just Eight
"9" -> Just Nine
"10" -> Just Ten
"j" -> Just Jack
"q" -> Just Queen
"k" -> Just King
_ -> Nothing
where r = init xs
parsedSuit = case s of '♥' -> Just Heart
'♦' -> Just Diamond
'♣' -> Just Club
'♠' -> Just Spade
_ -> Nothing
where s = head $ reverse xs
 
nameHand :: String -> String
nameHand s
| straight && flush = s <> ": straight-flush"
| ofKind 4 = s <> ": four-of-a-kind"
| uniqRanks == 2 = s <> ": full-house"
| flush = s <> ": flush"
| straight = s <> ": straight"
| ofKind 3 = s <> ": three-of-a-kind"
| uniqRanks == 3 = s <> ": two-pair"
| uniqRanks == 4 = s <> ": one-pair"
| otherwise = s <> ": high-card"
where cards = catMaybes $ map parseCard $ words s
sortedRank = sort $ map rank cards
sameRank xs = map rankCount groupedByRank
where groupedByRank = group sortedRank
rankCount c@(x:xs) = (x, length c)
ranks = sameRank cards
uniqRanks = length ranks
ofKind n = any (\(_, y) -> n == y) ranks
straight = isSucc sortedRank || sortedRank == acesHigh
flush = and $ map (\c -> s == suit c) cards
where s = suit $ head cards
 
testHands :: [String]
testHands =
[ "2♥ 2♦ 2♣ k♣ q♦"
, "2♥ 5♥ 7♦ 8♣ 9♠"
, "a♥ 2♦ 3♣ 4♣ 5♦"
, "2♥ 3♥ 2♦ 3♣ 3♦"
, "2♥ 7♥ 2♦ 3♣ 3♦"
, "2♥ 7♥ 7♦ 7♣ 7♠"
, "10♥ j♥ q♥ k♥ a♥"
, "4♥ 4♠ k♠ 5♦ 10♠"
, "q♣ 10♣ 7♣ 6♣ 4♣"]
 
main :: IO ()
main = mapM_ putStrLn $ map nameHand testHands
</lang>
{{out}}
<pre>
2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
2♥ 5♥ 7♦ 8♣ 9♠: high-card
a♥ 2♦ 3♣ 4♣ 5♦: straight
2♥ 3♥ 2♦ 3♣ 3♦: full-house
2♥ 7♥ 2♦ 3♣ 3♦: two-pair
2♥ 7♥ 7♦ 7♣ 7♠: four-of-a-kind
10♥ j♥ q♥ k♥ a♥: straight-flush
4♥ 4♠ k♠ 5♦ 10♠: one-pair
q♣ 10♣ 7♣ 6♣ 4♣: flush
</pre>
 
=={{header|Kotlin}}==
===Basic Version===
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Card(val face: Int, val suit: Char)
Line 2,133 ⟶ 2,413:
println("$hand: ${analyzeHand(hand)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,150 ⟶ 2,430:
 
===Extra Credit Version===
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Card(val face: Int, val suit: Char)
Line 2,277 ⟶ 2,557:
println("$hand : ${analyzeHand(hand)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,299 ⟶ 2,579:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Check whether t is a valid poker hand
function valid (t)
if #t ~= 5 then return false end
Line 2,421 ⟶ 2,701:
"qc 10c 7c 6c 4c" -- flush
}
for _, case in pairs(testCases) do print(case, ": " .. rank(case)) end</langsyntaxhighlight>
{{out}}
<pre>2h 2d 2c kc qd : three-of-a-kind
Line 2,432 ⟶ 2,712:
4h 4s ks 5d 10s : one-pair
qc 10c 7c 6c 4c : flush</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">import algorithm, sequtils, strutils, tables, unicode
 
type
 
Suit* = enum ♠, ♥, ♦, ♣
Face* {.pure.} = enum
Ace = (1, "a")
Two = (2, "2")
Three = (3, "3")
Four = (4, "4")
Five = (5, "5")
Six = (6, "6")
Seven = (7, "7")
Eight = (8, "8")
Nine = (9, "9")
Ten = (10, "10")
Jack = (11, "j")
Queen = (12, "q")
King = (13, "k")
 
Card* = tuple[face: Face; suit: Suit]
Hand* = array[5, Card]
 
HandValue {.pure.} = enum
Invalid = "invalid"
StraightFlush = "straight-flush"
FourOfAKind = "four-of-a-kind"
FullHouse = "full-house"
Flush = "flush"
Straight = "straight"
ThreeOfAKind = "three-of-a-kind"
TwoPair = "two-pair"
OnePair = "one-pair"
HighCard = "high-card"
 
CardError = object of ValueError
 
 
proc toCard(cardStr: string): Card =
## Convert a card string to a Card.
var runes = cardStr.toRunes
let suitStr = $(runes.pop()) # Extract the suit.
let faceStr = $runes # Take what’s left as the face.
try:
result.face = parseEnum[Face](faceStr)
except ValueError:
raise newException(CardError, "wrong face: " & faceStr)
try:
result.suit = parseEnum[Suit](suitStr)
except ValueError:
raise newException(CardError, "wrong suit: " & suitStr)
 
 
proc value(hand: openArray[Card]): HandValue =
## Return the value of a hand.
 
doAssert hand.len == 5, "Hand must have five cards."
 
var
cards: seq[Card] # The cards.
faces: CountTable[Face] # Count faces.
suits: CountTable[Suit] # Count suits.
 
for card in hand:
if card in cards: return Invalid # Duplicate card.
cards.add card
faces.inc card.face
suits.inc card.suit
 
faces.sort() # Greatest counts first.
suits.sort() # Greatest counts first.
cards.sort() # Smallest faces first.
 
# Check faces.
for face, count in faces:
case count
of 4:
return FourOfAKind
of 3:
result = ThreeOfAKind
of 2:
if result == ThreeOfAKind: return FullHouse
if result == OnePair: return TwoPair
result = OnePair
else:
if result != Invalid: return
 
# Search straight.
result = Straight
let start = if cards[0].face == Ace and cards[4].face == King: 2 else: 1
for n in start..4:
if cards[n].face != succ(cards[n - 1].face):
result = HighCard # No straight.
break
 
# Check suits.
if suits.len == 1: # A single suit.
result = if result == Straight: StraightFlush else: Flush
 
 
proc `$`(card: Card): string =
## Return the representation of a card.
var val = 0x1F0A0 + ord(card.suit) * 0x10 + ord(card.face)
if card.face >= Queen: inc val # Skip Knight.
result = $Rune(val)
 
 
 
when isMainModule:
 
const HandStrings = ["2♥ 2♦ 2♣ k♣ q♦",
"2♥ 5♥ 7♦ 8♣ 9♠",
"a♥ 2♦ 3♣ 4♣ 5♦",
"2♥ 3♥ 2♦ 3♣ 3♦",
"2♥ 7♥ 2♦ 3♣ 3♦",
"2♥ 7♥ 7♦ 7♣ 7♠",
"10♥ j♥ q♥ k♥ a♥",
"4♥ 4♠ k♠ 5♦ 10♠",
"q♣ 10♣ 7♣ 6♣ 4♣",
"4♥ 4♣ 4♥ 4♠ 4♦"]
 
for handString in HandStrings:
let hand = handString.split(' ').map(toCard)
echo hand.map(`$`).join(" "), " → ", hand.value</syntaxhighlight>
 
{{out}}
<pre>🂲 🃂 🃒 🃞 🃍 → three-of-a-kind
🂲 🂵 🃇 🃘 🂩 → high-card
🂱 🃂 🃓 🃔 🃅 → straight
🂲 🂳 🃂 🃓 🃃 → full-house
🂲 🂷 🃂 🃓 🃃 → two-pair
🂲 🂷 🃇 🃗 🂧 → four-of-a-kind
🂺 🂻 🂽 🂾 🂱 → straight-flush
🂴 🂤 🂮 🃅 🂪 → one-pair
🃝 🃚 🃗 🃖 🃔 → flush
🂴 🃔 🂴 🂤 🃄 → invalid</pre>
 
=={{header|Perl}}==
I dont like jokers. Instead I decided to give hands proper names. For example, "Kings full of Tens" rather than just "full-house".
 
<langsyntaxhighlight lang="perl">
use strict;
use warnings;
Line 2,579 ⟶ 2,997:
say Hand::describe($_) for @cards;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,605 ⟶ 3,023:
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(other) example of using the unicode card characters see [[Playing_cards#Phix]]<br>
{{libheader|Phix/online}}
<lang Phix>function poker(string hand)
You can run this online [http://phix.x10.mx/p2js/poker_hand.htm here].
hand = substitute(hand,"10","t")
<!--<syntaxhighlight lang="phix">(phixonline)-->
sequence cards = split(hand,no_empty:=1)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
if length(cards)!=5 then return "invalid hand" end if
<span style="color: #008080;">function</span> <span style="color: #000000;">poker</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">hand</span><span style="color: #0000FF;">)</span>
sequence ranks = repeat(0,13),
<span style="color: #000000;">hand</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hand</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"10"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"t"</span><span style="color: #0000FF;">)</span>
suits = repeat(0,4)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cards</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hand</span><span style="color: #0000FF;">)</span>
integer jokers = 0
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"invalid hand"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
for i=1 to length(cards) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ranks</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">),</span>
sequence ci = utf8_to_utf32(cards[i])
<span style="color: #000000;">suits</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
if ci="joker" then
<span style="color: #004080;">integer</span> <span style="color: #000000;">jokers</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
jokers += 1
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if jokers>2 then return "invalid hand" end if
<span style="color: #004080;">sequence</span> <span style="color: #000000;">ci</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">utf8_to_utf32</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cards</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
else
<span style="color: #008080;">if</span> <span style="color: #000000;">ci</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"joker"</span> <span style="color: #008080;">then</span>
if length(ci)!=2 then return "invalid hand" end if
<span style="color: #000000;">jokers</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
integer rank = find(lower(ci[1]),"23456789tjqka")
<span style="color: #008080;">if</span> <span style="color: #000000;">jokers</span><span style="color: #0000FF;">></span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"invalid hand"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer suit = find(ci[2],utf8_to_utf32("♥♣♦♠"))
<span style="color: #008080;">else</span>
if rank=0 or suit=0 then return "invalid hand" end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">2</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"invalid hand"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
ranks[rank] += 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">rank</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]),</span><span style="color: #008000;">"23456789tjqka"</span><span style="color: #0000FF;">)</span>
suits[suit] += 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">suit</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ci</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">utf8_to_utf32</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"♥♣♦♠"</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">rank</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #000000;">suit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #008000;">"invalid hand"</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #000000;">ranks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rank</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
integer straight = match({1,1,1,1,1},ranks)
<span style="color: #000000;">suits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">suit</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
if not straight then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
straight = sort(ranks)[$]=1 and match({0,0,0,0,0,0,0,0},ranks)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">straight</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)</span>
integer _flush = (max(suits)+jokers = 5)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">straight</span> <span style="color: #008080;">then</span>
integer _pairs = max(ranks)+jokers
<span style="color: #000000;">straight</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">))[$]=</span><span style="color: #000000;">1</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)</span>
integer pair = find(2,ranks)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
integer full_house = _pairs=3 and pair and (jokers=0 or find(2,ranks,pair+1))
<span style="color: #004080;">integer</span> <span style="color: #000000;">_flush</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">suits</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">jokers</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
integer two_pair = find(2,ranks,pair+1)
<span style="color: #004080;">integer</span> <span style="color: #000000;">_pairs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">max</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">jokers</span>
integer high_card = rfind(1,sq_ne(ranks,0))+1
<span style="color: #004080;">integer</span> <span style="color: #000000;">pair</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)</span>
if jokers and _pairs=jokers+1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">full_house</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">_pairs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">and</span> <span style="color: #000000;">pair</span> <span style="color: #008080;">and</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pair</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
straight = 1
<span style="color: #004080;">integer</span> <span style="color: #000000;">two_pair</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">,</span><span style="color: #000000;">pair</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
integer k = find(1,ranks), j = jokers
<span style="color: #004080;">integer</span> <span style="color: #000000;">high_card</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rfind</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_ne</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">))+</span><span style="color: #000000;">1</span>
for l=k to min(k+5-j,13) do
<span style="color: #008080;">if</span> <span style="color: #000000;">jokers</span> <span style="color: #008080;">and</span> <span style="color: #000000;">_pairs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
if ranks[l]=0 then
<span style="color: #000000;">straight</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
if j=0 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">jokers</span>
straight = 0
<span style="color: #008080;">for</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">k</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">5</span><span style="color: #0000FF;">-</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">13</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
exit
<span style="color: #008080;">if</span> <span style="color: #000000;">ranks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
end if
<span style="color: #008080;">if</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
j -= 1
<span style="color: #000000;">straight</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
end if
<span style="color: #008080;">exit</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if straight and j then
<span style="color: #000000;">j</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
high_card = min(high_card+j,14)
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
elsif straight and ranks[1]!=0 then
<span style="color: #008080;">if</span> <span style="color: #000000;">straight</span> <span style="color: #008080;">and</span> <span style="color: #000000;">j</span> <span style="color: #008080;">then</span>
high_card = find(0,ranks)
<span style="color: #000000;">high_card</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">min</span><span style="color: #0000FF;">(</span><span style="color: #000000;">high_card</span><span style="color: #0000FF;">+</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">14</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if _pairs=5 then return {10,"five of a kind", find(5-jokers,ranks)+1} end if
<span style="color: #008080;">elsif</span> <span style="color: #000000;">straight</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ranks</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if straight and _flush then return {9,"straight flush", high_card} end if
<span style="color: #000000;">high_card</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)</span>
if _pairs=4 then return {8,"four of a kind", find(4-jokers,ranks)+1} end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if full_house then return {7,"full house", find(3-jokers,ranks)+1} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">_pairs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"five of a kind"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">-</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if _flush then return {6,"flush", high_card} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">straight</span> <span style="color: #008080;">and</span> <span style="color: #000000;">_flush</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"straight flush"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">high_card</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if straight then return {5,"straight", high_card} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">_pairs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"four of a kind"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">-</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if _pairs=3 then return {4,"three of a kind", find(3-jokers,ranks)+1} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">full_house</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"full house"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">-</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if pair and two_pair then return {3,"two pair", two_pair+1} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">_flush</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">6</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"flush"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">high_card</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if pair then return {2,"one pair", pair+1} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">straight</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"straight"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">high_card</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if jokers then return {2,"one pair", high_card} end if
<span style="color: #008080;">if</span> <span style="color: #000000;">_pairs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"three of a kind"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">-</span><span style="color: #000000;">jokers</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ranks</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return {1,"high card",high_card}
<span style="color: #008080;">if</span> <span style="color: #000000;">pair</span> <span style="color: #008080;">and</span> <span style="color: #000000;">two_pair</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"two pair"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">two_pair</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end function
<span style="color: #008080;">if</span> <span style="color: #000000;">pair</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one pair"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pair</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
 
<span style="color: #008080;">if</span> <span style="color: #000000;">jokers</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"one pair"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">high_card</span><span style="color: #0000FF;">}</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
sequence hands = {{0,"2♥ 2♦ 2♣ k♣ q♦"},
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"high card"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">high_card</span><span style="color: #0000FF;">}</span>
{0,"2♥ 5♥ 7♦ 8♣ 9♠"},
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
{0,"a♥ 2♦ 3♣ 4♣ 5♦"},
{0,"2♥ 3♥ 2♦ 3♣ 3♦"},
<span style="color: #004080;">sequence</span> <span style="color: #000000;">hands</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2♥ 2♦ 2♣ k♣ q♦"</span><span style="color: #0000FF;">},</span>
{0,"2♥ 7♥ 2♦ 3♣ 3♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2♥ 5♥ 7♦ 8♣ 9♠"</span><span style="color: #0000FF;">},</span>
{0,"2♥ 7♥ 7♦ 7♣ 7♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"a♥ 2♦ 3♣ 4♣ 5♦"</span><span style="color: #0000FF;">},</span>
{0,"10♥ j♥ q♥ k♥ a♥"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2♥ 3♥ 2♦ 3♣ 3♦"</span><span style="color: #0000FF;">},</span>
{0,"4♥ 4♠ k♠ 5♦ 10♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2♥ 7♥ 2♦ 3♣ 3♦"</span><span style="color: #0000FF;">},</span>
{0,"q♣ 10♣ 7♣ 6♣ 4♣"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"2♥ 7♥ 7♦ 7♣ 7♠"</span><span style="color: #0000FF;">},</span>
{0,"joker 2♦ 2♠ k♠ q♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"10♥ j♥ q♥ k♥ a♥"</span><span style="color: #0000FF;">},</span>
{0,"joker 5♥ 7♦ 8♠ 9♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4♥ 4♠ k♠ 5♦ 10♠"</span><span style="color: #0000FF;">},</span>
{0,"joker 2♦ 3♠ 4♠ 5♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"q♣ 10♣ 7♣ 6♣ 4♣"</span><span style="color: #0000FF;">},</span>
{0,"joker 3♥ 2♦ 3♠ 3♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 2♦ 2♠ k♠ q♦"</span><span style="color: #0000FF;">},</span>
{0,"joker 7♥ 2♦ 3♠ 3♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 5♥ 7♦ 8♠ 9♦"</span><span style="color: #0000FF;">},</span>
{0,"joker 7♥ 7♦ 7♠ 7♣"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 2♦ 3♠ 4♠ 5♠"</span><span style="color: #0000FF;">},</span>
{0,"joker j♥ q♥ k♥ A♥"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 3♥ 2♦ 3♠ 3♦"</span><span style="color: #0000FF;">},</span>
{0,"joker 4♣ k♣ 5♦ 10♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 7♥ 2♦ 3♠ 3♦"</span><span style="color: #0000FF;">},</span>
{0,"joker k♣ 7♣ 6♣ 4♣"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 7♥ 7♦ 7♠ 7♣"</span><span style="color: #0000FF;">},</span>
{0,"joker 2♦ joker 4♠ 5♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker j♥ q♥ k♥ A♥"</span><span style="color: #0000FF;">},</span>
{0,"joker Q♦ joker A♠ 10♠"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 4♣ k♣ 5♦ 10♠"</span><span style="color: #0000FF;">},</span>
{0,"joker Q♦ joker A♦ 10♦"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker k♣ 7♣ 6♣ 4♣"</span><span style="color: #0000FF;">},</span>
{0,"joker 2♦ 2♠ joker q♦"}}
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 2♦ joker 4♠ 5♠"</span><span style="color: #0000FF;">},</span>
 
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker Q♦ joker A♠ 10♠"</span><span style="color: #0000FF;">},</span>
for i=1 to length(hands) do
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker Q♦ joker A♦ 10♦"</span><span style="color: #0000FF;">},</span>
hands[i][1] = poker(hands[i][2])
<span style="color: #0000FF;">{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"joker 2♦ 2♠ joker q♦"</span><span style="color: #0000FF;">}}</span>
end for
ppOpt({pp_Ascii,{#20,#FF}})
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
pp(reverse(sort(hands)))</lang>
<span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">poker</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][</span><span style="color: #000000;">2</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">hands</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">reverse</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">)))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rank</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">hand</span><span style="color: #0000FF;">,</span><span style="color: #000000;">desc</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">hands</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #0000FF;">{{</span><span style="color: #000000;">rank</span><span style="color: #0000FF;">,</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">},</span><span style="color: #000000;">hand</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">hands</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d. %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">11</span><span style="color: #0000FF;">-</span><span style="color: #000000;">rank</span><span style="color: #0000FF;">,</span><span style="color: #000000;">hand</span><span style="color: #0000FF;">,</span><span style="color: #000000;">desc</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
1. joker  7♥  7♦  7♠  7♣ five of a kind
{{{10, "five of a kind", 7}, "joker 7♥ 7♦ 7♠ 7♣"},
2. joker  j♥  q♥  k♥  A♥ straight flush
{{9, "straight flush", 14}, "joker j♥ q♥ k♥ A♥"},
2. joker  Q♦  joker  A♦ 10♦ straight flush
{{9, "straight flush", 14}, "joker Q♦ joker A♦ 10♦"},
2. 10♥ j♥ q♥ k♥ a♥ straight flush
{{9, "straight flush", 14}, "10♥ j♥ q♥ k♥ a♥"},
3. 2♥ 7♥ 7♦ 7♣ 7♠ four of a kind
{{8, "four of a kind", 7}, "2♥ 7♥ 7♦ 7♣ 7♠"},
3. joker  3♥  2♦  3♠  3♦ four of a kind
{{8, "four of a kind", 3}, "joker 3♥ 2♦ 3♠ 3♦"},
3. joker  2♦  2♠  joker  q♦ four of a kind
{{8, "four of a kind", 2}, "joker 2♦ 2♠ joker q♦"},
4. 2♥ 3♥ 2♦ 3♣ 3♦ full house
{{7, "full house", 3}, "2♥ 3♥ 2♦ 3♣ 3♦"},
5. joker  k♣  7♣  6♣  4♣ flush
{{6, "flush", 13}, "joker k♣ 7♣ 6♣ 4♣"},
5. q♣ 10♣ 7♣ 6♣ 4♣ flush
{{6, "flush", 12}, "q♣ 10♣ 7♣ 6♣ 4♣"},
6. joker  Q♦  joker  A♠ 10♠ straight
{{5, "straight", 14}, "joker Q♦ joker A♠ 10♠"},
6. joker  5♥  7♦  8♠  9♦ straight
{{5, "straight", 9}, "joker 5♥ 7♦ 8♠ 9♦"},
6. joker  2♦  joker  4♠  5♠ straight
{{5, "straight", 6}, "joker 2♦ joker 4♠ 5♠"},
6. joker  2♦  3♠  4♠  5♠ straight
{{5, "straight", 5}, "joker 2♦ 3♠ 4♠ 5♠"},
6. a♥ 2♦ 3♣ 4♣ 5♦ straight
{{5, "straight", 5}, "a♥ 2♦ 3♣ 4♣ 5♦"},
7. joker  7♥  2♦  3♠  3♦ three of a kind
{{4, "three of a kind", 3}, "joker 7♥ 2♦ 3♠ 3♦"},
7. joker  2♦  2♠  k♠  q♦ three of a kind
{{4, "three of a kind", 2}, "joker 2♦ 2♠ k♠ q♦"},
7. 2♥ 2♦ 2♣ k♣ q♦ three of a kind
{{4, "three of a kind", 2}, "2♥ 2♦ 2♣ k♣ q♦"},
8. 2♥ 7♥ 2♦ 3♣ 3♦ two pair
{{3, "two pair", 3}, "2♥ 7♥ 2♦ 3♣ 3♦"},
9. joker  4♣  k♣  5♦ 10♠ one pair
{{2, "one pair", 13}, "joker 4♣ k♣ 5♦ 10♠"},
9. 4♥ 4♠ k♠ 5♦ 10♠ one pair
{{2, "one pair", 4}, "4♥ 4♠ k♠ 5♦ 10♠"},
10. 2♥ 5♥ 7♦ 8♣ 9♠ high card
{{1, "high card", 9}, "2♥ 5♥ 7♦ 8♣ 9♠"}}
</pre>
 
=={{header|Picat}}==
<syntaxhighlight lang="picat">go =>
Hands = [
[[2,h], [7,h], [2,d], [3,c], [3,d]], % two-pair
[[2,h], [5,h], [7,d], [8,c], [9,s]], % high-card
[[a,h], [2,d], [3,c], [4,c], [5,d]], % straight
[[2,h], [3,h], [2,d], [3,c], [3,d]], % full-house
[[2,h], [7,h], [2,d], [3,c], [3,d]], % two-pair
[[2,h], [7,h], [7,d], [7,c], [7,s]], % four-of-a-kind
[[10,h],[j,h], [q,h], [k,h], [a,h]], % straight-flush
[[4,h], [4,s], [k,s], [5,d], [10,s]], % one-pair
[[q,c], [10,c],[7,c], [6,c], [4,c]], % flush
[[q,c], [q,d], [q,s], [6,c], [4,c]], % three-of-a-kind
 
[[q,c], [10,c], [7,c], [7,c], [4,c]], % invalid (duplicates)
[[q,c], [10,c], [7,c], [7,d]] % invalid (too short)
 
],
foreach(Hand in Hands)
print_hand(Hand),
analyse(Hand, H),
println(hand=H),
nl
end,
nl.
 
 
% Print the hand
print_hand(Hand) =>
println([ F.to_string() ++ S.to_string() : [F,S] in Hand]).
 
% Faces and suites
faces(Faces) => Faces = [a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, k].
faces_order1(Order1) =>
Order1 = new_map([a=1, 2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, j=11, q=12, k=13]).
faces_order2(Order2) =>
Order2 = new_map([2=2, 3=3, 4=4, 5=5, 6=6, 7=7, 8=8, 9=9, 10=10, j=11, q=12, k=13, a=14]).
 
suites(Suites) => Suites = [h,d,c,s].
 
% Order of the hand
hand_order(HandOrder) =>
HandOrder = [straight_flush,
four_of_a_kind,
full_house,
flush,
straight,
three_of_a_kind,
two_pair,
one_pair,
high_card,
invalid].
 
 
% for the straight
in_order(List) =>
foreach(I in 2..List.length)
List[I] = List[I-1] + 1
end.
 
 
% Some validity tests first
analyse(Hand,Value) ?=>
(
Hand.remove_dups.length == 5,
faces(Faces),
foreach([F,_] in Hand)
member(F,Faces)
end,
suites(Suites),
foreach([_,S] in Hand)
member(S,Suites)
end,
analyse1(Hand,Value)
;
Value = invalid
).
 
% Identify the specific hands
 
% straight flush
analyse1(Hand,Value) ?=>
permutation(Hand,Hand1),
Hand1 = [ [_F1,S], [_F2,S], [_F3,S], [_F4,S], [_F5,S] ],
(
faces_order1(Order1),
in_order([Order1.get(F) : [F,S1] in Hand1])
;
faces_order2(Order2),
in_order([Order2.get(F) : [F,S1] in Hand1]),
println("Royal Straight Flush!")
),
Value=straight_flush.
 
% four of a kind
analyse1(Hand,Value) ?=>
faces(Faces),
member(A,Faces),
[1 : [F,_S] in Hand, F = A].length == 4,
Value=four_of_a_kind.
 
% full house
analyse1(Hand,Value) ?=>
permutation(Hand,Hand1),
Hand1 = [ [F1,_S1], [F1,_S2], [F2,_S3], [F2,_S4], [F2,_S5] ],
Value = full_house.
 
% flush
analyse1(Hand,Value) ?=>
permutation(Hand,Hand1),
Hand1 = [ [_,S], [_,S], [_,S], [_,S], [_,S] ],
Value = flush.
 
% straight
analyse1(Hand,Value) ?=>
permutation(Hand,Hand1),
(
faces_order1(Order1),
in_order([Order1.get(F) : [F,_S] in Hand1])
;
faces_order2(Order2),
in_order([Order2.get(F) : [F,_S] in Hand1])
 
),
Value = straight.
 
% three of a kind
analyse1(Hand,Value) ?=>
faces(Faces),
member(A,Faces),
[1 : [F,_S] in Hand, F = A].length == 3,
Value = three_of_a_kind.
 
% two pair
analyse1(Hand,Value) ?=>
permutation(Hand,Hand1),
Hand1 = [ [F1,_S1], [F1,_S2], [F2,_S3], [F2,_S4], [_F3,_S5] ],
Value = two_pair.
 
% one pair
analyse1(Hand,Value) ?=>
faces(Faces),
member(A,Faces),
[1 : [F,_S] in Hand, F = A].length == 2,
Value = one_pair.
 
% high card
analyse1(_Hand,Value) =>
Value = high_card.</syntaxhighlight>
 
 
{{out}}
<pre>[2h,7h,2d,3c,3d]
hand = two_pair
 
[2h,5h,7d,8c,9s]
hand = high_card
 
[ah,2d,3c,4c,5d]
hand = straight
 
[2h,3h,2d,3c,3d]
hand = full_house
 
[2h,7h,2d,3c,3d]
hand = two_pair
 
[2h,7h,7d,7c,7s]
hand = four_of_a_kind
 
[10h,jh,qh,kh,ah]
Royal Straight Flush!
hand = straight_flush
 
[4h,4s,ks,5d,10s]
hand = one_pair
 
[qc,10c,7c,6c,4c]
hand = flush
 
[qc,qd,qs,6c,4c]
hand = three_of_a_kind
 
[qc,10c,7c,7c,4c]
hand = invalid
 
[qc,10c,7c,7d]
hand = invalid</pre>
 
For generating and checking random hands:
<syntaxhighlight lang="picat">go2 =>
_ = random2(),
Hand = random_hand(5),
print_hand(Hand),
analyse(Hand, H),
println(hand=H),
nl.
 
% Get one element of list L
oneof(L) = L[random(1,L.len)].
 
% Get a random hand
random_hand(N) = Hand =>
faces(Faces),
suites(Suites),
M = new_map(),
while (M.keys().length < N)
M.put([oneof(Faces),oneof(Suites)],1)
end,
Hand = [C : C=_ in M].sort().</syntaxhighlight>
 
{{out}}
<pre>[3d,5h,10c,10h,qs]
hand = one_pair</pre>
 
 
=={{header|PicoLisp}}==
(rassoc) function in picolisp after 3.1.9.10.
<langsyntaxhighlight PicoLisplang="picolisp">(setq *Rank
'(("2" . 0) ("3" . 1) ("4" . 2)
("5" . 3) ("6" . 4) ("7" . 5)
Line 2,759 ⟶ 3,403:
'two-pair )
((rassoc 2 R) 'pair)
(T 'high-card) ) ) )</langsyntaxhighlight>
 
=={{header|Prolog}}==
{{works with|GNU Prolog|1.4.4}}
Not very efficient version.
<langsyntaxhighlight lang="python">:- initialization(main).
 
 
Line 2,834 ⟶ 3,478:
, write(Cards), write('\t'), write(H), nl
.
main :- findall(_, run_tests, _), halt.</langsyntaxhighlight>
{{out}}
<pre>[c(2,♥),c(2,♦),c(2,♣),c(k,♣),c(q,♦)] three-of-a-kind(2)
Line 2,848 ⟶ 3,492:
=={{header|Python}}==
Goes a little further in also giving the ordered tie-breaker information from the wikipedia page.
<langsyntaxhighlight lang="python">from collections import namedtuple
 
class Card(namedtuple('Card', 'face, suit')):
Line 2,997 ⟶ 3,641:
for cards in hands:
r = rank(cards)
print("%-18r %-15s %r" % (cards, r[0], r[1]))</langsyntaxhighlight>
{{out}}
<pre>HAND CATEGORY TIE-BREAKER
Line 3,011 ⟶ 3,655:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require (only-in srfi/1 car+cdr))
 
Line 3,175 ⟶ 3,819:
(five-of-a-kind 7) (straight-flush 14) (one-pair 13 10 5 4) (flush 14 13 7 6 4) (straight 6)
(straight 14) (straight-flush 14) (four-of-a-kind 2)))
(for ((h e.g.-hands) (r expected-results)) (check-equal? (analyse-hand/string h) r)))</langsyntaxhighlight>
 
{{out}}
Line 3,205 ⟶ 3,849:
(formerly Perl 6)
This solution handles jokers. It has been written to use a Raku grammar.
<syntaxhighlight lang="raku" perl6line>use v6;
grammar PokerHand {
Line 3,240 ⟶ 3,884:
method TOP($/) {
my UInt @n = n-of-a-kind($/);
my $flush = 'flush' if flush($/);
my $straight = 'straight' if straight($/);
make rank(@n[0], @n[1], $flush, $straight);
}
multi sub rank(5,$,$,$*@) { 'five-of-a-kind' }
multi sub rank($,$,$f'flush',$s'straight') where {$f && $s}) { 'straight-flush' }
multi sub rank(4,$,$,$*@) { 'four-of-a-kind' }
multi sub rank($,$,$f'flush',$) where {$f}) { 'flush' }
multi sub rank($,$,$,$s where {$s}'straight') { 'straight' }
multi sub rank(3,2,$,$*@) { 'full-house' }
multi sub rank(3,$,$,$*@) { 'three-of-a-kind' }
multi sub rank(2,2,$,$*@) { 'two-pair' }
multi sub rank(2,$,$,$*@) { 'one-pair' }
multi sub rank($,$,$,$*@) is default { 'high-card' }
sub n-of-a-kind($/) {
Line 3,270 ⟶ 3,914:
# allow both ace-low and ace-high straights
constant @Faces = [ "a 2 3 4 5 6 7 8 9 10 j q k a".split: ' ' ];
constant @Possible-Straights = [ (04 ..^ (+@Faces - 5)).map: { set @Faces[$_-4 .. $_+4] } ];
 
my $faces = set @<face-card>.map: -> $/ {~$<face>.lc};
Line 3,314 ⟶ 3,958:
say "$_: $rank";
}
</syntaxhighlight>
</lang>
{{out}}
<pre>2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
Line 3,342 ⟶ 3,986:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 10.12.2013 Walter Pachl
*--------------------------------------------------------------------*/
Line 3,439 ⟶ 4,083:
err:
Say deck 'Error:' arg(1)
Return 0</langsyntaxhighlight>
{{out}}
<pre>2h 2d 2s ks qd three-of-a-kind
Line 3,465 ⟶ 4,109:
::* &nbsp; the dealt hands can be in a file &nbsp; (blank lines are ignored)
::* &nbsp; dealt hands in the file can have comments after a semicolon (''';''')
<langsyntaxhighlight lang="rexx">/*REXX program analyzes an N─card poker hand, and displays what the poker hand is. */
parse arg iFID .; if iFID=='' | iFID=="," then iFID= 'POKERHAN.DAT'
/* [↓] read the poker hands dealt. */
Line 3,504 ⟶ 4,148:
if kinds==2 & pairs==2 then return 'two-pair'
if kinds==2 then return 'one-pair'
return 'high-card'</langsyntaxhighlight>
Programming note: some older REXXes don't have the '''countstr''' BIF, so that REXX statement (above, line '''48''') can be replaced with:
<langsyntaxhighlight lang="rexx">pairs= 13 - length( space( translate( pips, , 2), 0) ) /*count # of 2's in PIPS.*/</langsyntaxhighlight>
 
{{out|input|text=&nbsp; file:}}
Line 3,543 ⟶ 4,187:
::* &nbsp; supports up to two ''jokers''
::* &nbsp; the ''joker'' card may be abbreviated (and can be in upper/lower/mixed case)
<langsyntaxhighlight lang="rexx">/*REXX program analyzes an N-card poker hand, and displays what the poker hand is, */
/*──────────────────────────────────────────── poker hands may contain up to two jokers.*/
parse arg iFID .; if iFID=='' | iFID=="," then iFID= 'POKERHAJ.DAT'
Line 3,601 ⟶ 4,245:
if kinds==2 & pairs==2 then return 'two-pair'
if kinds==2 then return 'one-pair'
return 'high-card'</langsyntaxhighlight>
Programming note: &nbsp; the method used for analyzing hands that contain jokers are limited to a maximum of two jokers.
 
Line 3,655 ⟶ 4,299:
q♣ t♣ 7♣ 6♣ jok ◄─── flush
J♥ Q♦ K♠ A♠ jok ◄─── straight
</pre>
 
=={{header|RPL}}==
{{works with|HP|48G}}
≪ → hand
≪ { }
1 15 '''FOR''' j
"A23456789TJQK" hand j DUP SUB POS 1 -
"CDHS" hand j 1 + DUP SUB POS 13 * +
+ 3 '''STEP'''
≫ ≫ '<span style="color:blue>HANDCODE</span>' STO
≪ → diffs
≪ { } 1
1 4 '''FOR''' j
'''IF''' diffs j GET '''THEN''' + 1 '''ELSE''' 1 + '''END NEXT'''
+ SORT REVLIST 1 2 SUB
≫ ≫ '<span style="color:blue>GROUPS</span>' STO
≪ DUP ΠLIST 1 ==
SWAP { 9 1 1 1 } 1 == OR
≫ '<span style="color:blue>STRAIGHT?</span>' STO
≪ <span style="color:blue>HANDCODE</span>
DUP 13 / IP ≪ == ≫ DOSUBS ΠLIST
SWAP 13 MOD SORT ΔLIST
DUP <span style="color:blue>GROUPS</span> SWAP <span style="color:blue>STRAIGHT?</span>
→ flush groups straight
≪ '''CASE'''
straight '''THEN''' flush "Straight flush" "Straight" IFTE '''END'''
groups { 4 1 } == '''THEN''' "Four of a kind" '''END'''
groups { 3 2 } == '''THEN''' "Full house" '''END'''
groups { 3 1 } == '''THEN''' "Three of a kind" '''END'''
groups { 2 2 } == '''THEN''' "Two pairs" '''END'''
groups { 2 1 } == '''THEN''' "One pair" '''END'''
flush "Flush" "High card" IFTE '''END'''
≫ ≫ '<span style="color:blue>→HAND</span>' STO
 
{ "2H 2D 2S KS QD" "2H 5H 7D 8S 9D" "AH 2D 3S 4S 5S" "2H 3H 2D 3S 3D" "2H 7H 2D 3S 3D" "2H 7H 7D 7S 7C" "TH JH QH KH AH" "4H 4C KC 5D TC" "QC TC 7C 6C 4C" }
1 ≪ <span style="color:blue>→HAND</span> ≫ DOLIST
{{out}}
<pre>
1: { "Three of a kind" "High card" "Straight" "Full house" "Two pairs" "Four of a kind" "Straight flush" "One pair" "Flush" }
</pre>
 
=={{header|Ruby}}==
Joker-less hands are sorted high to low.
<langsyntaxhighlight lang="ruby">class Card
include Comparable
attr_accessor :ordinal
Line 3,802 ⟶ 4,489:
best = all_tries.max
puts "#{line.strip}: #{best.rank}"
end</langsyntaxhighlight>
 
{{out}}
Line 3,835 ⟶ 4,522:
joker Q♦ joker A♦ 10♦: straight-flush
joker 2♦ 2♠ joker q♦: four-of-a-kind
</pre>
 
=={{header|Rust}}==
Unicode version with jokers. Also checks for Royal Flush (AKQJ10 suited).
<syntaxhighlight lang="rust">
fn main() {
let hands = vec![
"🂡 🂮 🂭 🂫 🂪",
"🃏 🃂 🂢 🂮 🃍",
"🃏 🂵 🃇 🂨 🃉",
"🃏 🃂 🂣 🂤 🂥",
"🃏 🂳 🃂 🂣 🃃",
"🃏 🂷 🃂 🂣 🃃",
"🃏 🂷 🃇 🂧 🃗",
"🃏 🂻 🂽 🂾 🂱",
"🃏 🃔 🃞 🃅 🂪",
"🃏 🃞 🃗 🃖 🃔",
"🃏 🃂 🃟 🂤 🂥",
"🃏 🃍 🃟 🂡 🂪",
"🃏 🃍 🃟 🃁 🃊",
"🃏 🃂 🂢 🃟 🃍",
"🃏 🃂 🂢 🃍 🃍",
"🃂 🃞 🃍 🃁 🃊",
];
for hand in hands{
println!("{} {}", hand, poker_hand(hand));
}
}
 
fn poker_hand(cards: &str) -> &str {
let mut suits = vec![0u8; 4];
let mut faces = vec![0u8; 15];
let mut hand = vec![];
 
for card in cards.chars(){
if card == ' ' { continue; }
let values = get_card_value(card);
if values.0 < 14 && hand.contains(&values) {
return "invalid";
}
hand.push(values);
faces[values.0 as usize]+=1;
if values.1 >= 0 {
suits[values.1 as usize]+=1;
}
}
if hand.len()!=5 {
return "invalid";
}
faces[13] = faces[0]; //add ace-high count
let jokers = faces[14];
 
//count suits
let mut colors = suits.into_iter()
.filter(|&x| x > 0).collect::<Vec<_>>();
colors.sort_unstable();
colors[0] += jokers; // add joker suits to the highest one;
let is_flush = colors[0] == 5;
 
//straight
let mut is_straight = false;
//pointer to optimise some work
//avoids looking again at cards that were the start of a sequence
//as they cannot be part of another sequence
let mut ptr = 14;
while ptr>3{
let mut jokers_left = jokers;
let mut straight_cards = 0;
for i in (0..ptr).rev(){
if faces[i]==0 {
if jokers_left == 0 {break;}
jokers_left -= 1;
}
else if i==ptr-1 { ptr-=1; }
straight_cards+=1;
}
ptr-=1;
if straight_cards == 5 {
is_straight = true;
break;
}
}
 
//count values
let mut values = faces.into_iter().enumerate().take(14).filter(|&x| x.1>0).collect::<Vec<_>>();
//sort by quantity, then by value, high to low
values.sort_unstable_by(|a, b| if b.1 == a.1 { (b.0).cmp(&a.0) } else { (b.1).cmp(&a.1)} );
let first_group = values[0].1 + jokers;
let second_group = if values.len()>1 {values[1].1} else {0};
match (is_flush, is_straight, first_group, second_group){
(_,_,5,_) => "five-of-a-kind",
(true, true, _, _) => if ptr == 8 {"royal-flush"} else {"straight-flush"},
(_,_,4,_) => "four-of-a-kind",
(_,_,3,2) => "full-house",
(true,_,_,_) => "flush",
(_,true,_,_) => "straight",
(_,_,3,_) => "three-of-a-kind",
(_,_,2,2) => "two-pair",
(_,_,2,_) => "one-pair",
_ => "high-card"
}
}
 
fn get_card_value(card: char) -> (i8,i8) {
// transform glyph to face + suit, zero-indexed
let base = card as u32 - 0x1F0A1;
let mut suit = (base / 16) as i8;
let mut face = (base % 16) as i8;
if face > 11 && face < 14 { face-=1; } // Unicode has a Knight that we do not want
if face == 14 { suit = -1; } //jokers do not have a suit
(face, suit)
}
</syntaxhighlight>
{{out}}
<pre>
🂡 🂮 🂭 🂫 🂪 royal-flush
🃏 🃂 🂢 🂮 🃍 three-of-a-kind
🃏 🂵 🃇 🂨 🃉 straight
🃏 🃂 🂣 🂤 🂥 straight
🃏 🂳 🃂 🂣 🃃 four-of-a-kind
🃏 🃂 🂢 🂮 🃍 three-of-a-kind
🃏 🂵 🃇 🂨 🃉 five-of-a-kind
🃏 🃂 🂣 🂤 🂥 straight-flush
🃏 🂳 🃂 🂣 🃃 one-pair
🃏 🃂 🂢 🂮 🃍 flush
🃏 🂵 🃇 🂨 🃉 straight
🃏 🃂 🂣 🂤 🂥 straight
🃏 🂳 🃂 🂣 🃃 straight-flush
🃏 🃂 🂢 🂮 🃍 four-of-a-kind
🃏 🂵 🃇 🂨 🃉 invalid
🃏 🃞 🃍 🃁 🃊 high-card
</pre>
 
=={{header|Scala}}==
Including jokers, but not special suit characters. Aiming for readability more than performance.
<langsyntaxhighlight lang="scala">val faces = "23456789TJQKA"
val suits = "CHSD"
sealed trait Card
Line 3,912 ⟶ 4,731:
val possibleHands = possibleRealHands(hand)
allHandTypes.find(t => possibleHands.exists(t.check)).map(_.name).getOrElse("high-card")
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="scala">val testHands = List(
"2H 2D 2S KS QD",
"2H 5H 7D 8S 9D",
Line 3,939 ⟶ 4,758:
for (hand <- testHands) {
println(s"$hand -> ${analyzeHand(parseHand(hand))}")
}</langsyntaxhighlight>
 
{{out}}
Line 3,965 ⟶ 4,784:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
Line 4,039 ⟶ 4,858:
analyze("4♥ 4♣ k♣ 5♦ t♣");
analyze("q♣ t♣ 7♣ 6♣ 4♣");
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,055 ⟶ 4,874:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">
<lang Standard ML>
local
val rec ins = fn x : int*'a => fn [] => [x]
Line 4,119 ⟶ 4,938:
end;
</syntaxhighlight>
</lang>
Example (interpreter):
<syntaxhighlight lang="standard ml">
<lang Standard ML>
val rec printio = fn
[] => ()
Line 4,149 ⟶ 4,968:
ac ah ac ad 10h : invalid
val it = (): unit
</syntaxhighlight>
</lang>
 
=={{header|Tcl}}==
{{works with|Tcl|8.6}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
namespace eval PokerHandAnalyser {
proc analyse {hand} {
Line 4,268 ⟶ 5,087:
return 0
}
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">foreach hand {
"2♥ 2♦ 2♣ k♣ q♦" "2♥ 5♥ 7♦ 8♣ 9♠" "a♥ 2♦ 3♣ 4♣ 5♦" "2♥ 3♥ 2♦ 3♣ 3♦"
"2♥ 7♥ 2♦ 3♣ 3♦" "2♥ 7♥ 7♦ 7♣ 7♠" "10♥ j♥ q♥ k♥ a♥" "4♥ 4♠ k♠ 5♦ 10♠"
Line 4,276 ⟶ 5,095:
} {
puts "${hand}: [PokerHandAnalyser::analyse $hand]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,288 ⟶ 5,107:
4♥ 4♠ k♠ 5♦ 10♠: one-pair
q♣ 10♣ 7♣ 6♣ 4♣: flush
</pre>
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
option explicit
class playingcard
dim suit
dim pips
'public property get gsuit():gsuit=suit:end property
'public property get gpips():gpips=pips:end property
public sub print
dim s,p
select case suit
case "S":s=chrW(&h2660)
case "D":s=chrW(&h2666)
case "C":s=chrW(&h2663)
case "H":s=chrW(&h2665)
end select
 
select case pips
case 1:p="A"
case 11:p="J"
case 12:p="Q"
case 13:p="K"
case else: p=""& pips
end select
 
wscript.stdout.write right(" "&p & s,3)&" "
end sub
end class
 
sub printhand(byref h,start)
dim i
for i =start to ubound(h)
h(i).print
next
'wscript.stdout.writeblanklines(1)
end sub
 
function checkhand(byval arr)
dim ss,i,max,last,uses,toppip,j,straight, flush,used,ace
redim nn(13)
ss=arr(0).suit '?????
straight=true:flush=true
max=0:last=0:used=0:toppip=0:ace=0
for i=0 to ubound(arr)
j=arr(i).pips
if arr(i).suit<>ss then flush=false
if j>toppip then toppip=j
if j=1 then ace=1
nn(j)=nn(j)+1
if nn(j)>max then max= nn(j)
if abs(j-toppip)>=4 then straight=0
next
for i=1 to ubound(nn)
if nn(i) then used=used+1
next
if max=1 then
if nn(1) and nn(10) and nn(11) and nn(12) and nn(13) then straight=1
end if
if flush and straight and max=1 then
checkhand= "straight-flush"
elseif flush then
checkhand= "flush"
elseif straight and max=1 then
checkhand= "straight"
elseif max=4 then
checkhand= "four-of-a-kind"
elseif max=3 then
if used=2 then
checkhand= "full-house"
else
checkhand= "three-of-a-kind"
end if
elseif max=2 then
if used=3 then
checkhand= "two-pair"
else
checkhand= "one-pair"
end if
else
checkhand= "Top "& toppip
End If
end function
 
 
function readhand(h)
dim i,b,c,p
redim a(4)
for i=0 to ubound(a)
b=h(i)
set c=new playingcard
p=left(b,1)
select case p
case "j": c.pips=11
case "q": c.pips=12
case "k": c.pips=13
case "t": c.pips=10
case "a": c.pips=1
case else c.pips=cint(p)
end select
c.suit=ucase(right(b,1))
set a(i)=c
next
readhand=a
erase a
end function
 
dim hands,hh,i
hands = Array(_
Array("2h", "5h", "7d", "8c", "9s"),_
Array("2h", "2d", "2c", "kc", "qd"),_
Array("ah", "2d", "3c", "4c", "5d"),_
Array("2h", "3h", "2d", "3c", "3d"),_
Array("2h", "7h", "2d", "3c", "3d"),_
Array("th", "jh", "qh", "kh", "ah"),_
Array("4h", "4s", "ks", "5d", "ts"),_
Array("qc", "tc", "7c", "6c", "4c"),_
Array("ah", "ah", "7c", "6c", "4c"))
 
for i=1 to ubound(hands)
hh=readhand(hands(i))
printhand hh,0
wscript.stdout.write vbtab & checkhand(hh)
wscript.stdout.writeblanklines(1)
'exit for
next
 
</syntaxhighlight>
{{out}}
<pre>
2♥ 2♦ 2♣ K♣ Q♦ three-of-a-kind
A♥ 2♦ 3♣ 4♣ 5♦ straight
2♥ 3♥ 2♦ 3♣ 3♦ full-house
2♥ 7♥ 2♦ 3♣ 3♦ two-pair
10♥ J♥ Q♥ K♥ A♥ straight-flush
4♥ 4♠ K♠ 5♦ 10♠ one-pair
Q♣ 10♣ 7♣ 6♣ 4♣ flush
A♥ A♥ 7♣ 6♣ 4♣ one-pair
</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-dynamic}}
{{libheader|Wren-sort}}
{{libheader|Wren-str}}
{{libheader|Wren-seq}}
===Basic Version===
<syntaxhighlight lang="wren">import "./dynamic" for Tuple
import "./sort" for Sort
import "./str" for Str
import "./seq" for Lst
 
var Card = Tuple.create("Card", ["face", "suit"])
 
var FACES = "23456789tjqka"
var SUITS = "shdc"
 
var isStraight = Fn.new { |cards|
var cmp = Fn.new { |i, j| (i.face - j.face).sign }
var sorted = Sort.merge(cards, cmp)
if (sorted[0].face + 4 == sorted[4].face) return true
if (sorted[4].face == 14 && sorted[0].face == 2 && sorted[3].face == 5) return true
return false
}
 
var isFlush = Fn.new { |cards|
var suit = cards[0].suit
if (cards.skip(1).all { |card| card.suit == suit }) return true
return false
}
 
var analyzeHand = Fn.new { |hand|
var h = Str.lower(hand)
var split = Lst.distinct(h.split(" ").where { |c| c != "" }.toList)
if (split.count != 5) return "invalid"
var cards = []
for (s in split) {
if (s.count != 2) return "invalid"
var fIndex = FACES.indexOf(s[0])
if (fIndex == -1) return "invalid"
var sIndex = SUITS.indexOf(s[1])
if (sIndex == -1) return "invalid"
cards.add(Card.new(fIndex + 2, s[1]))
}
var groups = Lst.groups(cards) { |card| card.face }
if (groups.count == 2) {
if (groups.any { |g| g[1].count == 4 }) return "four-of-a-kind"
return "full-house"
} else if (groups.count == 3) {
if (groups.any { |g| g[1].count == 3 }) return "three-of-a-kind"
return "two-pair"
} else if (groups.count == 4) {
return "one-pair"
} else {
var flush = isFlush.call(cards)
var straight = isStraight.call(cards)
if (flush && straight) return "straight-flush"
if (flush) return "flush"
if (straight) return "straight"
return "high-card"
}
}
 
var hands = [
"2h 2d 2c kc qd",
"2h 5h 7d 8c 9s",
"ah 2d 3c 4c 5d",
"2h 3h 2d 3c 3d",
"2h 7h 2d 3c 3d",
"2h 7h 7d 7c 7s",
"th jh qh kh ah",
"4h 4s ks 5d ts",
"qc tc 7c 6c 4c",
"ah ah 7c 6c 4c"
]
for (hand in hands) {
System.print("%(hand): %(analyzeHand.call(hand))")
}</syntaxhighlight>
 
{{out}}
<pre>
2h 2d 2c kc qd: three-of-a-kind
2h 5h 7d 8c 9s: high-card
ah 2d 3c 4c 5d: straight
2h 3h 2d 3c 3d: full-house
2h 7h 2d 3c 3d: two-pair
2h 7h 7d 7c 7s: four-of-a-kind
th jh qh kh ah: straight-flush
4h 4s ks 5d ts: one-pair
qc tc 7c 6c 4c: flush
ah ah 7c 6c 4c: invalid
</pre>
===Extra Credit Version===
<syntaxhighlight lang="wren">import "./dynamic" for Tuple
import "./sort" for Sort
import "./seq" for Lst
 
var Card = Tuple.create("Card", ["face", "suit"])
 
var cmp = Fn.new { |i, j| (i.face - j.face).sign }
 
var isStraight = Fn.new { |cards, jokers|
var sorted = Sort.merge(cards, cmp)
if (jokers == 0) {
if (sorted[0].face + 4 == sorted[4].face) return true
if (sorted[4].face == 14 && sorted[3].face == 5) return true
return false
} else if (jokers == 1) {
if (sorted[0].face + 3 == sorted[3].face) return true
if (sorted[0].face + 4 == sorted[3].face) return true
if (sorted[3].face == 14 && sorted[2].face == 4) return true
if (sorted[3].face == 14 && sorted[2].face == 5) return true
return false
} else {
if (sorted[0].face + 2 == sorted[2].face) return true
if (sorted[0].face + 3 == sorted[2].face) return true
if (sorted[0].face + 4 == sorted[2].face) return true
if (sorted[2].face == 14 && sorted[1].face == 3) return true
if (sorted[2].face == 14 && sorted[1].face == 4) return true
if (sorted[2].face == 14 && sorted[1].face == 5) return true
return false
}
}
 
var isFlush = Fn.new { |cards|
var sorted = Sort.merge(cards, cmp)
var suit = sorted[0].suit
if (sorted.skip(1).all { |card| card.suit == suit || card.suit == "j" }) return true
return false
}
 
var analyzeHand = Fn.new { |hand|
var split = Lst.distinct(hand.split(" ").where { |c| c != "" }.toList)
if (split.count != 5) return "invalid"
var cards = []
var jokers = 0
for (s in split) {
if (s.bytes.count != 4) return "invalid"
var cp = s.codePoints[0]
var card =
cp == 0x1f0a1 ? Card.new(14, "s") :
cp == 0x1f0b1 ? Card.new(14, "h") :
cp == 0x1f0c1 ? Card.new(14, "d") :
cp == 0x1f0d1 ? Card.new(14, "c") :
cp == 0x1f0cf ? Card.new(15, "j") : // black joker
cp == 0x1f0df ? Card.new(16, "j") : // white joker
(cp >= 0x1f0a2 && cp <= 0x1f0ab) ? Card.new(cp - 0x1f0a0, "s") :
(cp >= 0x1f0ad && cp <= 0x1f0ae) ? Card.new(cp - 0x1f0a1, "s") :
(cp >= 0x1f0b2 && cp <= 0x1f0bb) ? Card.new(cp - 0x1f0b0, "h") :
(cp >= 0x1f0bd && cp <= 0x1f0be) ? Card.new(cp - 0x1f0b1, "h") :
(cp >= 0x1f0c2 && cp <= 0x1f0cb) ? Card.new(cp - 0x1f0c0, "d") :
(cp >= 0x1f0cd && cp <= 0x1f0ce) ? Card.new(cp - 0x1f0c1, "d") :
(cp >= 0x1f0d2 && cp <= 0x1f0db) ? Card.new(cp - 0x1f0d0, "c") :
(cp >= 0x1f0dd && cp <= 0x1f0de) ? Card.new(cp - 0x1f0d1, "c") :
Card.new(0, "j") // invalid
if (card.face == 0) return "invalid"
if (card.suit == "j") jokers = jokers + 1
cards.add(card)
}
var groups = Lst.groups(cards) { |card| card.face }
if (groups.count == 2) {
if (groups.any { |g| g[1].count == 4 }) {
if (jokers == 0) return "four-of-a-kind"
return "five-of-a-kind"
}
return "full-house"
} else if (groups.count == 3) {
if (groups.any { |g| g[1].count == 3 }) {
if (jokers == 0) return "three-of-a-kind"
if (jokers == 1) return "four-of-a-kind"
return "five-of-a-kind"
}
return (jokers == 0) ? "two-pair" : "full-house"
} else if (groups.count == 4) {
if (jokers == 0) return "one-pair"
if (jokers == 1) return "three-of-a-kind"
return "four-of-a-kind"
} else {
var flush = isFlush.call(cards)
var straight = isStraight.call(cards, jokers)
if (flush && straight) return "straight-flush"
if (flush) return "flush"
if (straight) return "straight"
return (jokers == 0) ? "high-card" : "one-pair"
}
}
 
var hands = [
"🃏 🃂 🂢 🂮 🃍",
"🃏 🂵 🃇 🂨 🃉",
"🃏 🃂 🂣 🂤 🂥",
"🃏 🂳 🃂 🂣 🃃",
"🃏 🂷 🃂 🂣 🃃",
"🃏 🂷 🃇 🂧 🃗",
"🃏 🂻 🂽 🂾 🂱",
"🃏 🃔 🃞 🃅 🂪",
"🃏 🃞 🃗 🃖 🃔",
"🃏 🃂 🃟 🂤 🂥",
"🃏 🃍 🃟 🂡 🂪",
"🃏 🃍 🃟 🃁 🃊",
"🃏 🃂 🂢 🃟 🃍",
"🃏 🃂 🂢 🃍 🃍",
"🃂 🃞 🃍 🃁 🃊"
]
for (hand in hands) {
System.print("%(hand): %(analyzeHand.call(hand))")
}</syntaxhighlight>
 
{{out}}
<pre>
🃏 🃂 🂢 🂮 🃍: three-of-a-kind
🃏 🂵 🃇 🂨 🃉: straight
🃏 🃂 🂣 🂤 🂥: straight
🃏 🂳 🃂 🂣 🃃: four-of-a-kind
🃏 🂷 🃂 🂣 🃃: three-of-a-kind
🃏 🂷 🃇 🂧 🃗: five-of-a-kind
🃏 🂻 🂽 🂾 🂱: straight-flush
🃏 🃔 🃞 🃅 🂪: one-pair
🃏 🃞 🃗 🃖 🃔: flush
🃏 🃂 🃟 🂤 🂥: straight
🃏 🃍 🃟 🂡 🂪: straight
🃏 🃍 🃟 🃁 🃊: straight-flush
🃏 🃂 🂢 🃟 🃍: four-of-a-kind
🃏 🃂 🂢 🃍 🃍: invalid
🃂 🃞 🃍 🃁 🃊: high-card
</pre>
2,122

edits