Poker hand analyser: Difference between revisions

Added FreeBASIC
(Added FreeBASIC)
 
(6 intermediate revisions by 6 users not shown)
Line 87:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">F analyzeHandHelper(faceCount, suitCount)
V
p1 = 0B
Line 163:
‘4H 4C KC 5D TC’,
‘QC TC 7C 6C 4C’]
print(hand‘: ’analyzeHand(hand))</langsyntaxhighlight>
 
{{out}}
Line 179:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">PokerHand(hand){
StringUpper, hand, hand
Sort, hand, FCardSort D%A_Space%
Line 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 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 235:
=={{header|C}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <ctype.h>
#include <string.h>
Line 363:
}
return 0;
}</langsyntaxhighlight>
 
{{output}}
Line 381:
=={{header|C sharp}}==
{{works with|C sharp|8}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using static System.Linq.Enumerable;
Line 542:
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 570:
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">
<lang Cpp>
#include <iostream>
#include <sstream>
Line 645:
cout << p.analyze( "qc tc 7c 6c 4c" ) << endl << endl; return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 660:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(defn rank [card]
(let [[fst _] card]
(if (Character/isDigit fst)
Line 740:
["QC" "TC" "7C" "6C" "4C"]])
(run! println (map #(str % " : " (check-hand %)) hands))
</syntaxhighlight>
</lang>
 
{{out}}
Line 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 844:
"QC TC 7C 6C 4C"])
writeln(hand, ": ", hand.analyzeHand);
}</langsyntaxhighlight>
{{out}}
<pre>2H 2D 2S KS QD: three-of-a-kind
Line 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 985:
best = Enum.map(cards_list, &Hand.new &1) |> Enum.max
IO.puts "#{hand}:\t#{elem(best,3)}"
end)</langsyntaxhighlight>
 
{{out}}
Line 1,022:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
type Card = int * int
 
Line 1,122:
]
|> List.iter showHandRank
</syntaxhighlight>
</lang>
 
{{out}}
Line 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,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,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,305 ⟶ 1,451:
fmt.Printf("%s: %s\n", hand, analyzeHand(hand))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,322 ⟶ 1,468:
 
===Extra Credit Version===
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,537 ⟶ 1,683:
fmt.Printf("%s: %s\n", hand, analyzeHand(hand))
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,560 ⟶ 1,706:
=={{header|Haskell}}==
===Basic Version===
<langsyntaxhighlight Haskelllang="haskell">{-# LANGUAGE TupleSections #-}
 
import Data.Function (on)
Line 1,654 ⟶ 1,800:
 
main :: IO ()
main = mapM_ (putStrLn . (fst <> const ": " <> nameHand . snd)) testHands</langsyntaxhighlight>
{{out}}
<pre>2♥ 2♦ 2♣ k♣ q♦: Three of a kind
Line 1,670 ⟶ 1,816:
=={{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,705 ⟶ 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,740 ⟶ 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,919 ⟶ 2,078:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,969 ⟶ 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,999 ⟶ 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 2,020 ⟶ 2,179:
];
for(hand of testHands) console.log(hand + ": " + analyzeHand(hand));</langsyntaxhighlight>
{{out}}
<pre>
Line 2,042 ⟶ 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 2,162 ⟶ 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 2,179 ⟶ 2,338:
=={{header|Kotlin}}==
===Basic Version===
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Card(val face: Int, val suit: Char)
Line 2,254 ⟶ 2,413:
println("$hand: ${analyzeHand(hand)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,271 ⟶ 2,430:
 
===Extra Credit Version===
<langsyntaxhighlight lang="scala">// version 1.1.2
 
class Card(val face: Int, val suit: Char)
Line 2,398 ⟶ 2,557:
println("$hand : ${analyzeHand(hand)}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,420 ⟶ 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,542 ⟶ 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,555 ⟶ 2,714:
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils, strutils, tables, unicode
 
type
Line 2,678 ⟶ 2,837:
for handString in HandStrings:
let hand = handString.split(' ').map(toCard)
echo hand.map(`$`).join(" "), " → ", hand.value</langsyntaxhighlight>
 
{{out}}
Line 2,695 ⟶ 2,854:
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,838 ⟶ 2,997:
say Hand::describe($_) for @cards;
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,867 ⟶ 3,026:
{{libheader|Phix/online}}
You can run this online [http://phix.x10.mx/p2js/poker_hand.htm here].
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
Line 2,964 ⟶ 3,123:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,992 ⟶ 3,151:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
Hands = [
[[2,h], [7,h], [2,d], [3,c], [3,d]], % two-pair
Line 3,139 ⟶ 3,298:
% high card
analyse1(_Hand,Value) =>
Value = high_card.</langsyntaxhighlight>
 
 
Line 3,181 ⟶ 3,340:
 
For generating and checking random hands:
<langsyntaxhighlight Picatlang="picat">go2 =>
_ = random2(),
Hand = random_hand(5),
Line 3,200 ⟶ 3,359:
M.put([oneof(Faces),oneof(Suites)],1)
end,
Hand = [C : C=_ in M].sort().</langsyntaxhighlight>
 
{{out}}
Line 3,209 ⟶ 3,368:
=={{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 3,244 ⟶ 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 3,319 ⟶ 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 3,333 ⟶ 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 3,482 ⟶ 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,496 ⟶ 3,655:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require (only-in srfi/1 car+cdr))
 
Line 3,660 ⟶ 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,690 ⟶ 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,725 ⟶ 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,755 ⟶ 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,799 ⟶ 3,958:
say "$_: $rank";
}
</syntaxhighlight>
</lang>
{{out}}
<pre>2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
Line 3,827 ⟶ 3,986:
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ---------------------------------------------------------------
* 10.12.2013 Walter Pachl
*--------------------------------------------------------------------*/
Line 3,924 ⟶ 4,083:
err:
Say deck 'Error:' arg(1)
Return 0</langsyntaxhighlight>
{{out}}
<pre>2h 2d 2s ks qd three-of-a-kind
Line 3,950 ⟶ 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,989 ⟶ 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 4,028 ⟶ 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 4,086 ⟶ 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 4,140 ⟶ 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 4,287 ⟶ 4,489:
best = all_tries.max
puts "#{line.strip}: #{best.rank}"
end</langsyntaxhighlight>
 
{{out}}
Line 4,324 ⟶ 4,526:
=={{header|Rust}}==
Unicode version with jokers. Also checks for Royal Flush (AKQJ10 suited).
<syntaxhighlight lang="rust">
<lang Rust>
fn main() {
let hands = vec![
Line 4,433 ⟶ 4,635:
(face, suit)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,456 ⟶ 4,658:
=={{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 4,529 ⟶ 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 4,556 ⟶ 4,758:
for (hand <- testHands) {
println(s"$hand -> ${analyzeHand(parseHand(hand))}")
}</langsyntaxhighlight>
 
{{out}}
Line 4,582 ⟶ 4,784:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "console.s7i";
 
Line 4,656 ⟶ 4,858:
analyze("4♥ 4♣ k♣ 5♦ t♣");
analyze("q♣ t♣ 7♣ 6♣ 4♣");
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,672 ⟶ 4,874:
 
=={{header|Standard ML}}==
<syntaxhighlight lang="standard ml">
<lang Standard ML>
local
val rec ins = fn x : int*'a => fn [] => [x]
Line 4,736 ⟶ 4,938:
end;
</syntaxhighlight>
</lang>
Example (interpreter):
<syntaxhighlight lang="standard ml">
<lang Standard ML>
val rec printio = fn
[] => ()
Line 4,766 ⟶ 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,885 ⟶ 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,893 ⟶ 5,095:
} {
puts "${hand}: [PokerHandAnalyser::analyse $hand]"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,907 ⟶ 5,109:
</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}}
Line 4,914 ⟶ 5,255:
{{libheader|Wren-seq}}
===Basic Version===
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Tuple
import "./sort" for Sort
import "./str" for Str
import "./seq" for Lst
 
var Card = Tuple.create("Card", ["face", "suit"])
Line 4,984 ⟶ 5,325:
for (hand in hands) {
System.print("%(hand): %(analyzeHand.call(hand))")
}</langsyntaxhighlight>
 
{{out}}
Line 5,000 ⟶ 5,341:
</pre>
===Extra Credit Version===
<langsyntaxhighlight ecmascriptlang="wren">import "./dynamic" for Tuple
import "./sort" for Sort
import "./seq" for Lst
 
var Card = Tuple.create("Card", ["face", "suit"])
Line 5,113 ⟶ 5,454:
for (hand in hands) {
System.print("%(hand): %(analyzeHand.call(hand))")
}</langsyntaxhighlight>
 
{{out}}
2,122

edits