Ranking poker hands: Difference between revisions

Jump to navigation Jump to search
328 bytes added ,  20:01, 9 January 2020
m
Miraheze>Marshall
(Created page with "This article uses Dyalog APL to rank poker hands using the rules of [https://en.wikipedia.org/wiki/Texas_hold_%27em Texas hold 'em] and other popular poker variants. The f...")
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
This article uses [[Dyalog APL]] to rank poker hands using the rules of [https://en.wikipedia.org/wiki/Texas_hold_%27em Texas hold 'em] and other popular poker variants. The function <source lang=apl inline>handrank</source> below transforms a representation of a poker hand into a "rank" array so that the ordering of ranks under [[total array ordering]] is the same as the ordering of the initial poker hands. Poker hands can then by sorted or compared by calling <source lang=apl inline>handrank</source> on each one, then ordering with normal APL functions.
This article uses [[Dyalog APL]] to rank poker hands using the rules of [[wikipedia:Texas hold 'em|Texas hold 'em]] and other popular poker variants. The function <source lang=apl inline>handrank</source> below transforms a representation of a poker hand into a "rank" array so that the ordering of ranks under [[total array ordering]] is the same as the ordering of the initial poker hands. Poker hands can then by sorted or compared by calling <source lang=apl inline>handrank</source> on each one, then ordering with normal APL functions.


== Dyalog '19 version ==
== Dyalog '19 version ==


Marshall Lochbaum showed the following code in a presentation (D04: "Tacit Techniques with Dyalog version 18.0 Operators") at the [https://www.dyalog.com/user-meetings/dyalog19.htm Dyalog '19] user meeting.
[[Marshall Lochbaum]] showed the following code in a presentation (D04: "Tacit Techniques with Dyalog version 18.0 Operators"; video [https://dyalog.tv/Dyalog19/?v=czWC4tjwzOQ here]) at the [https://www.dyalog.com/user-meetings/dyalog19.htm Dyalog '19] user meeting.
<source lang=apl>
<source lang=apl>
handrank←{⊃{((8|@1⊢2-/⍺)⍵∧.=¨1,⊃⍵){(∧/⍺),(2=≢⍵),⍺,⊂⍵[⍒⍵;]⊖⍨⊃⍺},⍨∘≢⌸⍺}/↓⍉⍵[⍒⍵;]}
handrank←{⊃{((8|@1⊢2-/⍺)⍵∧.=¨1,⊃⍵){(∧/⍺),(2=≢⍵),⍺,⊂⍵[⍒⍵;]⊖⍨⊃⍺},⍨∘≢⌸⍺}/↓⍉⍵[⍒⍵;]}
cmp ← {⍺⍵-⍥(⊃⍋)⍵⍺}
cmp ← {⍺⍵-⍥(⊃⍋)⍵⍺}
cmphand ← cmp ⍥ handrank
cmphand ← cmp ⍥ handrank
 
</source>
The <source lang=apl inline>cmphand</source> function compares two hands to yield ¯1 if the left hand is better, 1 if the right is better, and 0 if they are tied. In the talk Marshall calls it by inverting a display function (a task which Dyalog's [[Inverse]] cannot yet handle):
<source lang=apl>
       I ← ⌷⍤0 99
       I ← ⌷⍤0 99
       disp ← I¨ ∘ ((2↓⎕D),'TJQKA')('♣♢♡♠') ⍤ 1
       disp ← I¨ ∘ ((2↓⎕D),'TJQKA')('♣♢♡♠') ⍤ 1
Line 153: Line 155:
== Examples ==
== Examples ==


To test our function, we can try it on the sample hands given in Wikipedia's page on Texas hold 'em [https://en.wikipedia.org/wiki/Texas_hold_%27em#Hand_values here]. The ranks for each of these hands are shown, as well as a [[grade up]] to demonstrate that they are in ascending order.
To test our function, we can try it on the sample hands given in Wikipedia's page on Texas hold 'em [[wikipedia:Texas hold 'em#Hand_values|here]]. The ranks for each of these hands are shown, as well as a [[grade up]] to demonstrate that they are in ascending order.
<source lang=apl>
<source lang=apl>
       hands ← ' '(≠⊆⊢)'A♣4♡7♢K♣2♠ K♣K♡7♢2♣5♠ K♣K♡7♢7♣5♠ K♣K♡K♢7♣5♠ 3♣4♡5♢6♣7♠ K♣Q♣9♣8♣2♣ K♣K♡K♢7♣7♠ K♣K♡K♢K♠5♠ 3♣4♣5♣6♣7♣ T♡J♡Q♡K♡A♡'
       hands ← ' '(≠⊆⊢)'A♣4♡7♢K♣2♠ K♣K♡7♢2♣5♠ K♣K♡7♢7♣5♠ K♣K♡K♢7♣5♠ 3♣4♡5♢6♣7♠ K♣Q♣9♣8♣2♣ K♣K♡K♢7♣7♠ K♣K♡K♢K♠5♠ 3♣4♣5♣6♣7♣ T♡J♡Q♡K♡A♡'

Navigation menu