JavaScript(ES6)、307 349
これは非常にかさばり、最善のアプローチであるかどうかはわかりません。おそらくまだ少しゴルフ可能。
h=>(r='_23456789TJQKAT',R=x=>r.search(x[0]),M=i=>[...'hcds'].some(s=>h.indexOf(j=h[i][0]+s)<0)&&j,[u,v,w,y]=h.sort((a,b)=>R(a)-R(b)).map(x=>R(x)),[,,d,e,f,g,k]=[...new Set(h+h)].sort(),q=10-u-v-w,s=(z=y>12)&q>0?q:y-u<5&&u*5+q-y,d>r?z?'Kh':'Ah':f>r?M((e>r?v<w:u<v)+1):s?r[s]+g:k?M(3):r[13-z-(w>11)-(v>10)]+g)
少ないゴルフ
h=>(
// card rank, 1 to 13, 0 unused
// fake rank 14 is T, to complete a straight JQKA?
// as I always try to complete a straight going up
r = '_23456789TJQKAT',
// R: rank a card
R = x => r.search(x[0]),
// M: find a missing card (to complete a same-rank set like a poker)
// look for a card with the same rank of the card at position i
// but with a suit not present in the hand
M = i => [...'hcds'].some(s => h.indexOf(j=h[i][0]+s) < 0) && j,
h.sort((a, b) => R(a)-R(b) ), // sort hand by rank
[u,v,w,y] = h.map(x=>R(x)), // rank of cards 0..3 in u,v,w,y
// Purpose: look for duplicate rank and/or duplicate suits
// Put values and suits in d,e,f,g,k, with no duplicates and sorted
// suits are lowercase and will be at right end
[,,d,e,f,g,k] = [...new Set(h+h)].sort(),
// Only if all ranks are different: find the missing value to get a straight
// or 0 if a straight cannot be obtained
// The first part manages the A before a 2
q = 10-u-v-w, s = y>12&q>0 ? q : y - u < 5 && u * 5 + q - y,
d > r // d is lowercase -> all cards have the same rank
? u < 13 ? 'Ah' : 'Kh' // add a K to a poker of A, else add an A
: e > r // e is lowercase -> 2 distinct ranks
? M(v<w ? 2 : 1) // go for a poker or a full house
: f > r // f is lowercase -> 3 distinct ranks, we have a pair
? M(u<v ? 2 : 1) // find the pair and go for 3 of a kind
: s // all different ranks, could it become a straight?
? r[s] + g // if there is only a suit, it will be a flush straight too
: k // if there are 2 or more different suits
? M(3) // go for a pair with the max rank
: r[13-(y>12)-(w>11)-(v>10)]+g // flush, find the max missing card
)
テスト
F=
h=>(r='_23456789TJQKAT',R=x=>r.search(x[0]),M=i=>[...'hcds'].some(s=>h.indexOf(j=h[i][0]+s)<0)&&j,[u,v,w,y]=h.sort((a,b)=>R(a)-R(b)).map(x=>R(x)),[,,d,e,f,g,k]=[...new Set(h+h)].sort(),q=10-u-v-w,s=(z=y>12)&q>0?q:y-u<5&&u*5+q-y,d>r?z?'Kh':'Ah':f>r?M((e>r?v<w:u<v)+1):s?r[s]+g:k?M(3):r[13-z-(w>11)-(v>10)]+g)
output=x=>O.textContent+=x+'\n'
;`Ah Kh Jh Th -> Qh
7d 8h Tc Jd -> 9d 9h 9c 9s
Js 6c Ts 8h -> Jc Jh Jd
Ac 4c 5d 3d -> 2h 2d 2c 2s
5s 9s Js As -> Ks
2h 3h 4h 5h -> 6h
Js Jc Ac Ah -> As Ad
Td 9d 5h 9c -> 9h 9s
Ah Ac Ad As -> Ks Kd Kh Kc
4d 5h 8c Jd -> Jc Js Jh`
.split('\n')
.forEach(s=>{
var o = s.match(/\w+/g) // input and output
var h = o.splice(0,4) // input in h, output in o
var hs = h+''
var r = F(h)
var ok = o.some(x => x==r)
output((ok?'OK ':'KO ')+ hs + ' -> ' + r)
})
<pre id=O></pre>