Poker hand analyser: Difference between revisions

Content deleted Content added
→‎{{header|REXX}}: changed the order of WHENs to reflect the hierarchy of poker hands. -- ~~~~
m formatting
Line 2:
Create a program to parse a single 5 card poker hand and rank it according to this [[wp:List_of_poker_hands|list of poker hands]].
 
A poker hand is specified as a space separated list of 5 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'''
 
: Suits are: '''h''' (hearts), '''d''' (diamonds), '''c''' (clubs), and '''s''' (spades), or alternatively the unicode card-suit characters: ♥ ♦ ♣ ♠
 
Duplicate cards are illegal.
Line 24:
 
For extra credit:
:1.# use the playing card characters introduced with Unicode 6.0 (U+1F0A1 - U+1F0DE).
:2.# allow two jokers
::*   use the symbol '''j'''
::*   duplicates would be allowed   (for jokers only)
::*   five-of-a-kind would then be the highest hand
 
Examples:
 
2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
Line 154:
!! 'invalid';
say "$_: $rank";
}</lang>
}
{{out}}
</lang>
 
'''Output:'''
 
2♥ 2♦ 2♣ k♣ q♦: three-of-a-kind
2♥ 5♥ 7♦ 8♣ 9♠: high-card
Line 305 ⟶ 302:
return hand
 
 
if __name__ == '__main__':
Line 322 ⟶ 318:
r = rank(cards)
print("%-18r %-15s %r" % (cards, r[0], r[1]))</lang>
 
{{out}}
<pre>HAND CATEGORY TIE-BREAKER
Line 453 ⟶ 448:
===version 2===
This REXX version supports:
:::* &nbsp; upper/lower/mixed case for suits and pips
:::* &nbsp; allows commas or blanks for card separation
:::* &nbsp; alternate names for a aces and tens
:::* &nbsp; alphabetic letters for suits and/or glyphs
:::* &nbsp; specification of number of cards in a hand
:::* &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 &nbsp; (''';''')
<lang rexx>/*REXX program analyzes an N-card poker hand, displays what the hand is.*/
parse arg iFID .; if iFID=='' then iFID='POKERHAN.DAT'
Line 493 ⟶ 488:
otherwise return 'high-card'
end /*select*/</lang>
Programming note: &nbsp; some older REXXes don't have the &nbsp; '''countstr''' &nbsp; BIF, so that REXX statement (above, line 21) can be replaced with:
<lang rexx>pairs=13-length(space(translate(pips,,2),0)) /*count # of 2's in PIPS.*/</lang>
'''input file''':
Line 527 ⟶ 522:
===version 3 (with jokers)===
This REXX version has three additional features:
:::* &nbsp; "invalid" hands have additional diagnostic information
:::* &nbsp; supports up to two ''jokers''
:::* &nbsp; the ''joker'' card may be abbreviated (and in upper/lower/mixed case)
<lang rexx>/*REXX program analyzes an N-card poker hand, displays what the hand is.*/
/*─────────────────────────────── poker hands may contain up to 2 jokers*/
Line 587 ⟶ 582:
otherwise return 'high-card'
end /*select*/</lang>
Programming note: &nbsp; the method used for analyzing hands that contain jokers are limited to a maximum of two jokers.
<br>A different methodology would be needed for a generic number of jokers (and/or wild cards [such as deuces and one-eyed jacks]).<br><br>
 
'''input file''':
<pre>