Poker hand analyser: Difference between revisions
Content added Content deleted
m (used more consistent indentations in the task's preamble.) |
m (→{{header|REXX}}: added/changed comments, simplified the function.) |
||
Line 1,759:
2x 2h 3h 4h 5h Error: invalid suit x in 2x</pre>
===version 2 with suit glyphs===
This REXX version supports:
::* upper/lower/mixed case for suits and pips
::* allows commas or blanks for card separation
::* alternate names for
::* alphabetic letters for suits and/or glyphs
::* specification of number of cards in a hand
::* the dealt hands can be in a file (blank lines are ignored)
::* dealt hands in the file can have comments after a semicolon (''';''')
<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. */
do while lines(iFID)\==0; ox=linein(iFID); if ox='' then iterate
say right(ox, max(30, length(ox) ) ) ' ◄─── ' analyze(ox)
end /*while*/ /* [↑] analyze/validate the poker hand
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
analyze: procedure; arg x ';',mc; hand=translate(x, '♥♦♣♠1', "HDCSA,")
kinds=0; suit.=0; flush=0; pairs=0; @.=0; run=copies(0 ,13); pips=run
if mc=='' then mc=5; n=words(hand); if n\==mc then return 'invalid'
/* [↓] PIP can be 1 or 2
if pip==10 then pip='T' /*allow an alternate form for a "TEN". */
run=overlay(.,run,#); _=substr(pips,#,1)+1 /*convert run to series of dots. */
end /*i*/ /*keep track of N-of-a-kind. [↑] */▼
pips=overlay(_,pips,#); kinds=max(kinds,_) /*convert certain pips to their number.*/
pairs=countstr(2, pips) /*count
straight=pos(....., run || left(run, 1))\==0 /*does the RUN contains a straight? */
if flush==5 & straight then return 'straight-flush'
if kinds==4
if flush==5
if
if kinds==3
if kinds==2
otherwise return 'high-card'▼
Programming note: some older REXXes don't have the '''countstr''' 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>
Line 1,818:
ah 2h 3h 4h
</pre>
<pre>
2♥ 2♦ 2♠ k♠ q♦ ◄─── three-of-a-kind
Line 1,833:
</pre>
===version 3
This REXX version has three additional features:
::* "invalid" hands have additional diagnostic information
::* supports up to two ''jokers''
::* the ''joker'' card may be abbreviated (and can be in upper/lower/mixed case)
<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='
/* [↓] read the poker hands dealt. */
do while lines(iFID)\==0; ox=linein(iFID); if ox='' then iterate
say right(ox, max(30, length(ox) ) ) ' ◄─── ' analyze(ox)
end /*while*/ /* [↑] analyze/validate the poker hand
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
analyze: procedure; arg x ';',mc; hand=translate(x, '♥♦♣♠1', "HDCSA,")
kinds=0; suit.=0; flush=0; pairs=0; @.=0; run=copies(0 ,13); pips=run
if mc=='' then mc=5; n=words(hand) /*N is the
if n\==mc then return 'invalid number of cards, must be' mc
/* [↓] the PIP can be 1 or 2 chars. */
if pip==10
if abbrev('JOKER',_,1) then _="JK" /*allow altername forms of JOKER names.*/
if _=='JK' then
iterate
run=overlay(.,run,#); _=substr(pips,#,1)+1
pips=overlay(_,pips,#); kinds=max(kinds,_) /*convert certain pips to their number.*/
run=run || left(run, 1) /*
jok=@.jk; kinds=kinds+jok; flush=flush+jok /*
straight= pos(..... , run)\==0 |, /*does the RUN
(pos(.... , run)\==0 & jok>=1) |, /* " " " " " " */
(pos(..0.. , run)\==0 & jok>=1) |, /* " " " " " " */
(pos(...0. , run)\==0 & jok>=1) |, /* " " " " " " */
(pos(.0... , run)\==0 & jok>=1) |, /* " " " " " " */
(pos(... , run)\==0 & jok>=2) |, /* " " " " " " */
(pos(..0. , run)\==0 & jok>=2) |, /* " " " " " " */
(pos(.0.. , run)\==0 & jok>=2) |, /* " " " " " " */
(pos(.00.. , run)\==0 & jok>=2) |, /* " " " " " " */
(pos(..00. , run)\==0 & jok>=2) |, /* " " " " " " */
(pos(.0.0. , run)\==0 & jok>=2) /* " " " " " " */
pairs=countstr(2, pips) /*count
if jok\==0 then pairs=pairs-1 /*adjust
if kinds>=5 then return
if
if kinds>=4
if flush>=5
if
if kinds>=3
if kinds==2
Programming note: 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]).
'''input file''':
Line 1,924 ⟶ 1,922:
J♥ Q♦ K♠ A♠ jok
</pre>
<pre>
joker 2♦ 2♠ k♠ q♦ ◄─── three-of-a-kind
|