โป๊กเกอร์คำใครชนะใคร


14

อินพุตจะเป็นคำสองห้าตัวอักษร จริง ๆ แล้วพวกเขาไม่จำเป็นต้องเป็นคำในพจนานุกรมเพียงห้าตัวอักษรแต่ละตัวพิมพ์เล็กหรือตัวพิมพ์ใหญ่ทั้งหมดที่คุณเลือก AZ เท่านั้นที่จะปรากฏในคำที่ป้อนและจะมีความยาว 5 ตัวอักษร

โปรแกรมของคุณคือให้คะแนนพวกเขาทั้งคู่ราวกับว่าพวกเขาเป็นมือโป๊กเกอร์และให้คะแนนที่สูงกว่า แน่นอนว่าไม่เหมาะกับการใช้งานที่นี่การจัดอันดับเท่านั้นจึงไม่มีการฟลัช

ระบบการจัดอันดับโป๊กเกอร์โดยทั่วไปจะ: '1 คู่', '2 คู่', '3 ของชนิด', 'ตรง', 'บ้านเต็ม', '4 ของชนิด', '5 ของชนิด' และแน่นอน มีความเป็นไปได้ที่มือ (หรือคำในกรณีนี้) จะไม่มีค่าอะไรเลย

ในกรณีของความสัมพันธ์จดหมายที่ใกล้ชิดกับ A ถือว่าสูงกว่าดังนั้นคู่ของ As เต้นคู่หนึ่ง Bs ในบางกรณีมือทั้งสองอาจเหมือนกัน แต่ในลำดับที่แตกต่างกัน (หรือไม่) ในกรณีนั้นเอาท์พุทมือหรือรุ่น resorted ของมัน

หน้าภายนอกนี้มีข้อมูลเกี่ยวกับวิธีการระบุผู้ชนะและโดยเฉพาะอย่างยิ่งความสัมพันธ์ภายในการจัดอันดับเฉพาะในกรณีที่คุณไม่คุ้นเคยกับวิธีการทำคะแนนโป๊กเกอร์

ในกรณีของ straights : ตัวอักษรจะต้องอยู่ติดกันในตัวอักษรและไม่ได้รับอนุญาตให้ล้อมรอบ ดังนั้น 'defgh' ในลำดับใด ๆ ก็คือ 'xyzab' ไม่ตรง

ตัวอย่างของวิธีการทำคะแนนในมือเดียว:

word   | scored as
---------------------
ccccc  | 5 of a kind     <-- highest ranking
woooo  | 4 of a kind
opopo  | full house
vurst  | straight
vovvu  | 3 of a kind
ppoww  | 2 pairs
upper  | 1 pair
kjsdf  | high card only (in this case D) <-- lowest ranking

ดังนั้นโปรแกรมจะสร้างผลลัพธ์เช่นนี้:

input        |  output
-----------------------
voviu,kjsdf  |  voviu     because a pair beats nothing 
opoqo,upper  |  opoqo     because 3 of a kind beats a pair
woooo,ggegg  |  ggegg     because 4 Gs beats 4 Os
queue,hopup  |  queue     because 2 pairs beats 1 pair
lodpl,ddkop  |  ddkop     because pair DD beats pair LL
huhyg,hijht  |  huhyg     both have pair HH, but G beats I
ddffh,ccyyz  |  ccyyz     both have 2 pairs, but CC(yyz) beats DD(ffh)
okaok,nkunk  |  nkunk     KK ties with KK, but NN beats OO
abcdf,bcdef  |  bcdef     because it is a straight
qtery,retyq  |  qtery     identical! so doesnt matter
abedc,vyxwz  |  abedc     because it is a "higher" straight
hhhij,hijkl  |  hijkl     because straight beats 3 of a kind
aaabb,zzzzz  |  zzzzz     because nothing beats 5 of a kind

ลำดับตัวอักษรทั้งอินพุตและเอาต์พุตไม่เกี่ยวข้องดังนั้นลำดับในเอาต์พุตของคุณอาจแตกต่างจากอินพุต แต่ต้องมีคลังรายการตัวอักษรเดียวกัน

ผลลัพธ์จะต้องประกอบด้วยตัวอักษรห้าตัว - ไม่มากไม่น้อย

ใช้กฎ codegolf ตามปกติ รหัสที่สั้นที่สุดชนะ

คำตอบ:


4

JavaScript ( 224 218 213 ไบต์)

s=>t=>(v=s=>(o={},l=n=0,z=3.5,[...s].sort().map(c=>(n+=o[c]=-~o[c],z*=!l|l+1==(l=c.charCodeAt()))),[n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]),w=v(s),x=v(t),w[0]>x[0]||w[0]==x[0]&&w[1]<x[1]?s:t)

Ungolfed:

s=>t=>(
  v=s=>(
    o={},
    l=n=0,
    z=3.5,
    [...s].sort().map(c=>(
      n+=o[c]=-~o[c],
      z*=!l|l+1==(l=c.charCodeAt())
    )),
    [n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]
  ),
  w=v(s),x=v(t),
  w[0]>x[0] || w[0]==x[0] && w[1]<x[1] ? s : t
)

หลังจากmap()วิ่งแล้วให้n + zกำหนดอันดับมือ:

ป้อนคำอธิบายรูปภาพที่นี่

(คุณสามารถเข้าใจว่าทำไมฉันzถึงเริ่มต้น3.5)

ในกรณีที่เสมอกันObject.keys(o).sort()จะใช้เพื่อกำหนดอันดับที่สูงกว่า

ตัวอย่างข้อมูล:

f=

s=>t=>(v=s=>(o={},l=n=0,z=3.5,[...s].sort().map(c=>(n+=o[c]=-~o[c],z*=!l|l+1==(l=c.charCodeAt()))),[n+z,Object.keys(o).sort((a,b)=>o[b]-o[a]||(o[b]>o[a]?1:-1))]),w=v(s),x=v(t),w[0]>x[0]||w[0]==x[0]&&w[1]<x[1]?s:t)

console.log(/voviu/.test(f('voviu')('kjsdf')))       //because a pair beats nothing 
console.log(/opoqo/.test(f('opoqo')('upper')))       //because 3 of a kind beats a pair
console.log(/ggegg/.test(f('woooo')('ggegg')))       //because 4 Gs beats 4 Os
console.log(/queue/.test(f('queue')('hopup')))       //because 2 pairs beats 1 pair
console.log(/ddkop/.test(f('lodpl')('ddkop')))       //because pair DD beats pair LL
console.log(/huhyg/.test(f('huhyg')('hijht')))       //both have pair HH, but G beats I
console.log(/ccyyz/.test(f('ddffh')('ccyyz')))       //both have 2 pairs, but CC(yyz) beats DD(ffh)
console.log(/nkunk/.test(f('okaok')('nkunk')))       //KK ties with KK, but NN beats OO
console.log(/bcdef/.test(f('abcdf')('bcdef')))       //because it is a straight
console.log(/qtery|retyq/.test(f('qtery')('retyq'))) //identical! so doesnt matter
console.log(/abedc/.test(f('abedc')('vyxwz')))       //because it is a "higher" straight
console.log(/hijkl/.test(f('hhhij')('hijkl')))       //because straight beats 3 of a kind
console.log(/zzzzz/.test(f('aaabb')('zzzzz')))       //because nothing beats 5 of a kind


3

เยลลี่ ,  28 27 29  27 ไบต์

+2 และจากนั้น -2 แก้ไขข้อผิดพลาดจากนั้นเล่นซ้ำอีกครั้ง

FI=1ȦḤW
OµNĠLÞṚịµL€+Ç,N
ÇÞṪ

ลิงค์ monadic รับรายการ "hands" และส่งคืนผู้ชนะ (หนึ่งใน)

ใช้งานได้กับอินพุตตัวพิมพ์ใหญ่หรือตัวพิมพ์เล็กทั้งหมด
(... แต่ไม่ผสมสำหรับการเติมบรรทัดสุดท้ายด้วยŒlหรือŒu)

ลองออนไลน์! หรือดูชุดทดสอบ

อย่างไร?

FI=1ȦḤW - Link 1, straight offset: grouped and reverse sorted hand ordinals
        -                     e.g. [[-101],[-100],[-99],[-98],[-97]]
F       - flatten                  [-101,-100,-99,-98,-97]
 I      - increments               [1,1,1,1]
  =1    - equal 1? (vectorises)    [1,1,1,1]
    Ȧ   - any and all?             1
     Ḥ  - double                   2
      W - wrap in a list           [2]
        -   The purpose of this is so that when "a" from Link 2 represents a straight we
        -   get [2], whereas for any other hand we get [0]. Adding the [2] to [1,1,1,1,1]
        -   (the lengths of a straight's groups) yields [3,1,1,1,1], placing it between
        -   three of a kind, [3,1,1], and a full house, [3,2], as required.

OµNĠLÞṚịµL€+Ç,N - Link 2, hand rank key function: list of characters       e.g. "huhyg"
O               - cast to ordinals                                [104,117,104,121,103]
 µ              - monadic chain separation, call that o
  N             - negate (to give them a reverse-sort order) [-104,-117,-104,-121,-103]
   Ġ            - group indices by value                            [[4],[2],[1,3],[5]]
     Þ          - sort by key function:
    L           -   length                                          [[4],[2],[5],[1,3]]
      Ṛ         - reverse                                           [[1,3],[5],[2],[4]]
       ị        - index into o                       [[-104,-104],[-103],[-117],[-121]]
        µ       - monadic chain separation (call that a)
         L€     - length of €ach                                              [2,1,1,1]
            Ç   - call last link (2) as a monad -> [isStraight? * 2]                [0]
           +    - addition (vectorises)                                       [2,1,1,1]
              N - negate o                                [[104,104],[103],[117],[121]]
             ,  - pair                        [[2,1,1,1],[[104,104],[103],[117],[121]]]
                -   now sorting by this will first be comparing the hand class, and if
                -   and only if they match comparing the card values in the required order.

ÇÞḢ - Main link: list of lists of characters (list of hands)
 Þ  - sort by key function:
Ç   -   last link (2) as a monad
  Ṫ - tail (best or an equal-best hand)

เมื่อเปรียบเทียบกับสิ่งที่ฉันทำใน JS 0.o
Stephen

3
@StephenS ยินดีต้อนรับสู่ PPCG ที่คุณทำบางสิ่งบางอย่างในภาษาที่ไม่ใช่กอล์ฟและบางคนทำบางสิ่งบางอย่างใน Jelly, 05AB1E, Pyth, CJam ฯลฯ ที่สั้นกว่าชื่อภาษาของคุณ: I: P
HyperNeutrino

1
@StephenS - JS ควรแข่งขันกับ JS อย่าให้ภาษากอล์ฟขัดขวางคุณจากการส่งคำตอบที่ดีในภาษาอื่น!
Jonathan Allan

@ โจนาธานมันขัดขวางฉันจากการใช้ความพยายามมากเกินไปในการคิดมากและสรุปปัญหาที่สามารถแก้ไขได้ใน ~ 25 ตัวอักษรต่อไปนี้เป็นเรื่องที่ฉันทำอยู่ - ฉันเขียนต้นฉบับทั้งหมดและไม่มีรหัสจริง
Stephen

มันยอดเยี่ยม แต่ฉันเพิ่งเพิ่มกรณีทดสอบที่ไม่คำนวณโดยเฉพาะ ["hhhij", "hijkl"] ฉันคิดว่าเป็นเพราะวิธีที่คุณจัดอันดับตรงเป็น [3,1,1,1,1]?
Octopus

3

JavaScript ( 250 247 232 ไบต์)

S=d=>(x={},l=99,h=s=0,[...d].map(v=>x[v]=-~x[v]),Object.keys(x).map(v=>(c=91-v.charCodeAt(),t=x[v],s+=1e4**t,c<x[t]?0:x[t]=c,g=(h=c>h?c:h)-(l=c<l?c:l))),[5,4,3,2,1].map(v=>s+=0|x[v]**v),s+(s<5e7&&g<5?1e13:0)),C=a=>b=>(S(a)>S(b)?a:b)

โค้ดที่ไม่ได้บรรจุ & กรณีทดสอบใน JSFiddle: https://jsfiddle.net/CookieJon/8yq8ow1b/

บันทึกไบต์ด้วย @RickHitchcock @StephenS & @Arnauld


นี่คือสิ่งที่ฉันพยายามทำ แต่ไม่มีเงื่อนงำว่าจะทำอย่างไร
Stephen

ฉันไม่ได้จนกระทั่งหลังจากที่ฉันเริ่ม! :-)
Bumpy

s=0,h=0=> s=h=0ฉันเชื่อ
Stephen

1
แก้ไขทันทีหลังจากดึงผมมาก การกำหนดไทเบรกเกอร์ในกรณีที่มือเหมือนกันและตัวละครที่ต่ำที่สุดในกลุ่มที่ 1 และ 2 ที่ใหญ่ที่สุดก็เหมือนกันคือนักฆ่า (33 ไบต์หรือมากกว่านั้นเพียงแค่!!) :-(
Bumpy

x[v]=x[v]?++x[v]:1สามารถกลายเป็นx[v]=(x[v]|0)+1บันทึก 3 ไบต์
Rick Hitchcock

2

Python 2.7, 242 223 ไบต์

from collections import*
s=sorted
f=lambda x,y:s(map(lambda h:(lambda (r,n):((3,1.5)if len(r)==5 and ord(r[0])+4==ord(r[4])else n,[-ord(d) for d in r],h))(zip(*s(Counter(h).items(),key=lambda z:(-z[1],z[0])))),(x,y)))[1][2]

คล้ายกับแนวคิดพื้นฐานของตัวอย่างจาวาสคริปต์ (เรียงตามความแข็งแรงของมือยกเว้นสเตรจแล้วตามลำดับ) แต่การใช้ประโยชน์จากcollections.Counterโชค.most_commonไม่ดีไม่ได้มีพฤติกรรมที่ต้องการ; ดังนั้นต้องเพิ่มคีย์การเรียงลำดับที่กำหนดเอง

แก้ไข: การตีกอล์ฟอีกเล็กน้อยเพื่อลดขนาด 19 ไบต์

รหัสที่ไม่ตีกอล์ฟ

from collections import Counter

def convertHand(h):
    # first get item counts, appropriately ordered; e.g. cbabc -> (('b',2), ('c',2),('a',1))
    sortedPairs = sorted(Counter(h).items(),key=lambda x:(-x[1],x[0]))

    # 'unzip' the tuples to get (('b','c','a'), (2,2,1))
    ranks, numberFound = zip(*sortedPairs) 

    if len(ranks)==5:
        # no pairs; is it a straight? well, since they are in increasing order...
        if ord(ranks[0])+4 == ord(ranks[4]):
            # replace numberFound with something that will sort above 3 of a kind but below full house
            numberFound = (3,1.5)

    # invert the values of the ranks, so they are in decreasing, rather then increasing order
    ranks = [-ord(r) for r in ranks]

    # arrange tuples so we can sort by numberFound, and then ranks; and keep a reference to the hand
    return (numberFound, ranks, h)

# put it all together...
def f(x,y):
    hands = [convertHand(h) for h in (x,y)]
    rankedHands = sorted(hands)
    return rankedHands[1][2]

1

Mathematica, 635 ไบต์

H[x_]:=Block[{t},T=Sort@ToCharacterCode[x];L=Last/@Tally@T;t=0;S=Count;If[S[L,2]==1,t=1];If[S[L,2]==2,t=2];If[S[L,3]==1,t=3];If[S[Differences@T,1]==4,t=4];If[S[L,2]==1&&S[L,3]==1,t=5];If[S[L,4]==1,t=6];If[S[L,5]==1,t=7];t];F[K_,v_]:=First@Flatten@Cases[Tally@K,{_,v}];B=ToCharacterCode;(Z=Sort@B@#1;Y=Sort@B@#2;a=H[#1];b=H[#2];If[a>b,P@#1,If[a<b,P@#2]]If[a==b&&a==0,If[Z[[1]]<Y[[1]],P@#1,P@#2]];If[a==b&&(a==1||a==2),If[F[Z,2]<F[Y,2],P@#1,If[F[Z,2]==F[Y,2],If[F[Z,1]<F[Y,1],P@#1,P@#2],P@#2]]];If[a==b&&(a==3||a==5),If[F[Z,3]<F[Y,3],P@#1,P@#2]];If[a==b&&a==6,If[F[Z,4]<F[Y,4],P@#1,P@#2]];If[a==b&&(a==7||a==4),If[Tr@Z<Tr@Y,P@#1,P@#2]])&

.
.
แบบฟอร์มการป้อนข้อมูล

["abcde", "kkekk"]


มีวิธีทดสอบออนไลน์นี้หรือไม่
Octopus

2
sandbox.open.wolframcloud.com/app/objects วางด้วย ctrl + v เพิ่มอินพุตที่ส่วนท้ายของรหัสและรันด้วย shift + enter
J42161217
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.